Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# --- Initialize client securely ---
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
+
|
| 8 |
+
if not HF_TOKEN:
|
| 9 |
+
raise ValueError("❌ HF_TOKEN not found. Please set it in your Hugging Face Space secrets.")
|
| 10 |
+
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
base_url="https://router.huggingface.co/v1",
|
| 13 |
+
api_key=HF_TOKEN,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# --- Chat handler ---
|
| 17 |
+
def chat_with_model(message, history):
|
| 18 |
+
# Build messages list safely
|
| 19 |
+
messages = []
|
| 20 |
+
|
| 21 |
+
if history:
|
| 22 |
+
for msg in history:
|
| 23 |
+
# Handle both dict and tuple formats
|
| 24 |
+
if isinstance(msg, dict):
|
| 25 |
+
# Keep only allowed keys
|
| 26 |
+
messages.append({
|
| 27 |
+
"role": msg.get("role", "user"),
|
| 28 |
+
"content": msg.get("content", "")
|
| 29 |
+
})
|
| 30 |
+
elif isinstance(msg, (list, tuple)) and len(msg) == 2:
|
| 31 |
+
messages.append({"role": "user", "content": msg[0]})
|
| 32 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
| 33 |
+
|
| 34 |
+
# Add latest user message
|
| 35 |
+
messages.append({"role": "user", "content": message})
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
model="openai/gpt-oss-120b:fireworks-ai",
|
| 40 |
+
messages=messages,
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
)
|
| 43 |
+
reply = response.choices[0].message.content
|
| 44 |
+
except Exception as e:
|
| 45 |
+
reply = f"⚠️ Error: {str(e)}"
|
| 46 |
+
|
| 47 |
+
return reply
|
| 48 |
+
|
| 49 |
+
# --- Gradio Interface ---
|
| 50 |
+
chatbot_ui = gr.ChatInterface(
|
| 51 |
+
fn=chat_with_model,
|
| 52 |
+
title="🧠 GPT-OSS 120B (Fireworks)",
|
| 53 |
+
description="Chat with the OSS 120B model hosted via Hugging Face router.",
|
| 54 |
+
examples=["Hello!", "Tell me a joke.", "Explain AI in simple terms."],
|
| 55 |
+
type="messages", # required
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
chatbot_ui.launch()
|