Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# ---- Configuration ----
|
| 6 |
+
|
| 7 |
+
HF_API_KEY = os.getenv("HF_TOKEN")
|
| 8 |
+
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
base_url="https://router.huggingface.co/v1",
|
| 11 |
+
api_key=HF_API_KEY
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# ---- Chat function ----
|
| 15 |
+
def chat_with_model(user_message, history):
|
| 16 |
+
if history is None:
|
| 17 |
+
history = []
|
| 18 |
+
|
| 19 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 20 |
+
for human, bot in history:
|
| 21 |
+
messages.append({"role": "user", "content": human})
|
| 22 |
+
messages.append({"role": "assistant", "content": bot})
|
| 23 |
+
|
| 24 |
+
messages.append({"role": "user", "content": user_message})
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
completion = client.chat.completions.create(
|
| 28 |
+
model="openai/gpt-oss-20b:nebius",
|
| 29 |
+
messages=messages
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
assistant_reply = completion.choices[0].message["content"]
|
| 33 |
+
history.append((user_message, assistant_reply))
|
| 34 |
+
return assistant_reply, history
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}", history
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# ---- Gradio UI ----
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# 🤖 Chatbot using HuggingFace Router (OpenAI API Compatible)")
|
| 43 |
+
chatbot = gr.Chatbot(height=450)
|
| 44 |
+
text_input = gr.Textbox(label="Your message")
|
| 45 |
+
|
| 46 |
+
text_input.submit(chat_with_model, [text_input, chatbot], [chatbot])
|
| 47 |
+
text_input.submit(lambda: "", None, text_input) # clear input
|
| 48 |
+
|
| 49 |
+
demo.launch()
|