Muhammadidrees commited on
Commit
a51f86a
·
verified ·
1 Parent(s): a7279ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -1,25 +1,41 @@
 
1
  import gradio as gr
2
  from openai import OpenAI
3
- import os
4
 
 
5
  client = OpenAI(
6
  base_url="https://router.huggingface.co/v1",
7
- api_key=os.getenv("HF_TOKEN")
8
  )
9
 
10
- def chat_with_model(user_input):
11
- completion = client.chat.completions.create(
12
- model="openai/gpt-oss-20b:nebius",
13
- messages=[{"role": "user", "content": user_input}],
14
- )
15
- return completion.choices[0].message.content
16
-
17
- iface = gr.Interface(
18
- fn=chat_with_model,
19
- inputs=gr.Textbox(lines=5, placeholder="Type your message here..."),
20
- outputs=gr.Textbox(label="Response"),
21
- title="Hugging Face GPT-OSS Chat",
22
- description="Chat with GPT-OSS 20B model deployed via Hugging Face API"
23
- )
24
 
25
- iface.launch(ssr_mode=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
  from openai import OpenAI
 
4
 
5
+ # Initialize HuggingFace router client
6
  client = OpenAI(
7
  base_url="https://router.huggingface.co/v1",
8
+ api_key=os.getenv("HF_TOKEN") # Set in HF Spaces Secrets
9
  )
10
 
11
+ def chat_with_model(message: str):
12
+ try:
13
+ response = client.chat.completions.create(
14
+ model="openai/gpt-oss-20b:nebius",
15
+ messages=[{"role": "user", "content": message}],
16
+ )
17
+
18
+ # Correct extraction of the text
19
+ return response.choices[0].message.content
20
+
21
+ except Exception as e:
22
+ return f" Error: {e}"
23
+
 
24
 
25
+ # Build simple HF-friendly UI
26
+ with gr.Blocks(title="GPT-OSS 20B Chat") as demo:
27
+ gr.Markdown("## Chat with GPT-OSS 20B (HF Router)")
28
+ input_box = gr.Textbox(lines=4, label="Your Message")
29
+ output_box = gr.Textbox(label="Model Reply")
30
+
31
+ submit = gr.Button("Send")
32
+
33
+ submit.click(chat_with_model, inputs=input_box, outputs=output_box)
34
+
35
+
36
+ # Launch for HF Spaces (no SSR, no hot reload)
37
+ demo.launch(
38
+ server_name="0.0.0.0",
39
+ server_port=7860,
40
+ show_error=True
41
+ )