Muhammadidrees commited on
Commit
afddc96
Β·
verified Β·
1 Parent(s): f41052c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # -------------------------------------------------
6
+ # πŸ” 1. Secure API setup
7
+ # -------------------------------------------------
8
+ # IMPORTANT:
9
+ # Do NOT hardcode your token.
10
+ # Instead, go to your Space β†’ Settings β†’ Repository secrets
11
+ # Add a secret with:
12
+ # Key: HF_TOKEN
13
+ # Value: your Hugging Face token (e.g. hf_xxx...)
14
+ # Then this will safely load it at runtime:
15
+ client = OpenAI(
16
+ base_url="https://router.huggingface.co/v1",
17
+ api_key=os.getenv("HF_TOKEN"),
18
+ )
19
+
20
+ # -------------------------------------------------
21
+ # 🧩 2. Chat function (with streaming)
22
+ # -------------------------------------------------
23
+ def chat_with_model(message, history, model_name):
24
+ messages = []
25
+ for human, assistant in history:
26
+ messages.append({"role": "user", "content": human})
27
+ if assistant:
28
+ messages.append({"role": "assistant", "content": assistant})
29
+ messages.append({"role": "user", "content": message})
30
+
31
+ full_reply = ""
32
+ stream = client.chat.completions.create(
33
+ model=model_name,
34
+ messages=messages,
35
+ stream=True,
36
+ )
37
+
38
+ for chunk in stream:
39
+ delta = chunk.choices[0].delta.content
40
+ if delta:
41
+ full_reply += delta
42
+ yield full_reply # stream live output
43
+
44
+ # -------------------------------------------------
45
+ # 🎨 3. Gradio interface
46
+ # -------------------------------------------------
47
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
48
+ gr.Markdown(
49
+ """
50
+ <h2 style="text-align:center;">πŸ€– GPT-OSS Chat (via Hugging Face Router)</h2>
51
+ <p style="text-align:center;">
52
+ Stream responses from Fireworks-AI's GPT-OSS models β€” 7B, 20B, and 120B β€” through the Hugging Face Inference Router.
53
+ </p>
54
+ """,
55
+ elem_id="title",
56
+ )
57
+
58
+ with gr.Row():
59
+ model_selector = gr.Dropdown(
60
+ label="Select Model",
61
+ choices=[
62
+ "openai/gpt-oss-7b:fireworks-ai",
63
+ "openai/gpt-oss-20b:fireworks-ai",
64
+ "openai/gpt-oss-120b:fireworks-ai",
65
+ ],
66
+ value="openai/gpt-oss-120b:fireworks-ai",
67
+ interactive=True,
68
+ )
69
+
70
+ gr.ChatInterface(
71
+ fn=lambda msg, hist: chat_with_model(msg, hist, model_selector.value),
72
+ title="GPT-OSS Chat",
73
+ chatbot=gr.Chatbot(height=500, show_label=False),
74
+ textbox=gr.Textbox(placeholder="Ask me anything...", autofocus=True),
75
+ retry_btn="↩ Retry",
76
+ clear_btn="🧹 Clear Chat",
77
+ )
78
+
79
+ # -------------------------------------------------
80
+ # πŸš€ 4. Launch app
81
+ # -------------------------------------------------
82
+ if __name__ == "__main__":
83
+ demo.launch()