Muhammadidrees commited on
Commit
6c89169
Β·
verified Β·
1 Parent(s): 6ed36b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -70
app.py CHANGED
@@ -1,83 +1,49 @@
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()
 
1
  import os
 
2
  from openai import OpenAI
3
+ import gradio as gr
4
 
5
+ # --- Setup ---
6
+ # Hugging Face Space will automatically inject your token from Secrets
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+
9
+ # Create client for Hugging Face Inference Router
 
 
 
 
 
10
  client = OpenAI(
11
  base_url="https://router.huggingface.co/v1",
12
+ api_key=HF_TOKEN,
13
  )
14
 
15
+ # --- Chat Function ---
16
+ def chat_with_model(message, history):
17
+ # Convert history into OpenAI-compatible messages
18
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
19
+ for human, ai in history:
 
20
  messages.append({"role": "user", "content": human})
21
+ messages.append({"role": "assistant", "content": ai})
 
22
  messages.append({"role": "user", "content": message})
23
 
24
+ try:
25
+ response = client.chat.completions.create(
26
+ model="openai/gpt-oss-120b:fireworks-ai",
27
+ messages=messages,
28
+ max_tokens=500,
29
+ temperature=0.7,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  )
31
+ reply = response.choices[0].message.content
32
+ except Exception as e:
33
+ reply = f"⚠️ Error: {str(e)}"
34
+
35
+ return reply
36
+
37
+ # --- Gradio UI ---
38
+ iface = gr.ChatInterface(
39
+ fn=chat_with_model,
40
+ chatbot=gr.Chatbot(height=500, type="messages", show_label=False),
41
+ title="πŸ’¬ GPT-OSS 120B Chat",
42
+ description="Chat with a massive open-source language model hosted via Hugging Face Router.",
43
+ theme="soft",
44
+ textbox=gr.Textbox(placeholder="Ask me anything...", scale=7),
45
+ )
46
 
47
+ # --- Launch ---
 
 
 
 
 
 
 
 
 
 
 
48
  if __name__ == "__main__":
49
+ iface.launch()