Spaces:
Running
Running
Update app.py
Browse files
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(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|