File size: 1,077 Bytes
a51f86a
cfbebb6
 
a7279ae
a51f86a
cfbebb6
 
a51f86a
cfbebb6
 
a51f86a
 
 
 
 
 
 
 
 
 
 
 
 
cfbebb6
a51f86a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import gradio as gr
from openai import OpenAI

# Initialize HuggingFace router client
client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=os.getenv("HF_TOKEN")  # Set in HF Spaces Secrets
)

def chat_with_model(message: str):
    try:
        response = client.chat.completions.create(
            model="openai/gpt-oss-20b:nebius",
            messages=[{"role": "user", "content": message}],
        )

        # Correct extraction of the text
        return response.choices[0].message.content

    except Exception as e:
        return f"❌ Error: {e}"


# Build simple HF-friendly UI
with gr.Blocks(title="GPT-OSS 20B Chat") as demo:
    gr.Markdown("## Chat with GPT-OSS 20B (HF Router)")
    input_box = gr.Textbox(lines=4, label="Your Message")
    output_box = gr.Textbox(label="Model Reply")

    submit = gr.Button("Send")

    submit.click(chat_with_model, inputs=input_box, outputs=output_box)


# Launch for HF Spaces (no SSR, no hot reload)
demo.launch(
    server_name="0.0.0.0",
    server_port=7860,
    show_error=True
)