Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
with gr.Blocks(fill_height=True) as demo:
|
|
|
4 |
with gr.Sidebar():
|
5 |
-
gr.Markdown("# Inference
|
6 |
-
gr.Markdown(
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
demo.launch()
|
|
|
1 |
+
# ------------------------------------------------------------
|
2 |
+
# Gradio demo that talks to the HF Inference API (no provider)
|
3 |
+
# Model: openai/gpt-oss-120b
|
4 |
+
# UI built from low‑level components (Chatbot + Textbox + Button)
|
5 |
+
# ------------------------------------------------------------
|
6 |
+
import os
|
7 |
import gradio as gr
|
8 |
+
from huggingface_hub import InferenceClient
|
9 |
|
10 |
+
# ------------------------------------------------------------
|
11 |
+
# 1️⃣ Inference ------------------------------------------------------------
|
12 |
+
def hf_chat(message: str, history: list, token: str):
|
13 |
+
"""
|
14 |
+
Called every time the user clicks **Submit**.
|
15 |
+
|
16 |
+
Parameters
|
17 |
+
----------
|
18 |
+
message : str # newest user utterance
|
19 |
+
history : list[(str,str)] # chat history that Gradio keeps for us
|
20 |
+
token : str # HF access token obtained from LoginButton
|
21 |
+
|
22 |
+
Returns
|
23 |
+
-------
|
24 |
+
tuple (updated_history, reply)
|
25 |
+
"""
|
26 |
+
# ---- build the prompt in the format the model expects ----
|
27 |
+
system_prompt = (
|
28 |
+
"You are a helpful, knowledgeable assistant. "
|
29 |
+
"Answer the user’s questions concisely but clearly."
|
30 |
+
)
|
31 |
+
chat = [{"role": "system", "content": system_prompt}]
|
32 |
+
for usr, bot in history:
|
33 |
+
chat.append({"role": "user", "content": usr})
|
34 |
+
chat.append({"role": "assistant", "content": bot})
|
35 |
+
chat.append({"role": "user", "content": message})
|
36 |
+
|
37 |
+
# ---- call the HF Inference API ----
|
38 |
+
client = InferenceClient(
|
39 |
+
model="openai/gpt-oss-120b",
|
40 |
+
token=token,
|
41 |
+
timeout=120,
|
42 |
+
)
|
43 |
+
response = client.chat_completion(
|
44 |
+
messages=chat,
|
45 |
+
max_tokens=512,
|
46 |
+
temperature=0.7,
|
47 |
+
top_p=0.9,
|
48 |
+
)
|
49 |
+
reply = response.choices[0].message.content.strip()
|
50 |
+
|
51 |
+
# ---- return the new history (Gradio will display it) ----
|
52 |
+
# Gradio expects a list of (user, bot) tuples.
|
53 |
+
new_history = history + [(message, reply)]
|
54 |
+
return new_history, reply
|
55 |
+
|
56 |
+
|
57 |
+
# ------------------------------------------------------------
|
58 |
+
# 2️⃣ UI definition
|
59 |
+
# ------------------------------------------------------------
|
60 |
with gr.Blocks(fill_height=True) as demo:
|
61 |
+
# ---- Sidebar -------------------------------------------------
|
62 |
with gr.Sidebar():
|
63 |
+
gr.Markdown("# Inference Demo")
|
64 |
+
gr.Markdown(
|
65 |
+
"""
|
66 |
+
This Space runs **openai/gpt-oss-120b** directly via the
|
67 |
+
Hugging Face Inference API.
|
68 |
+
Sign in with your Hugging Face account to obtain a token that will be
|
69 |
+
sent securely to the API.
|
70 |
+
"""
|
71 |
+
)
|
72 |
+
# The button injects a `token` variable into the session.
|
73 |
+
login_btn = gr.LoginButton("Sign in with Hugging Face")
|
74 |
+
|
75 |
+
# ---- Main chat area -----------------------------------------
|
76 |
+
# 1️⃣ a `gr.Chatbot` that shows the conversation history
|
77 |
+
chatbot = gr.Chatbot(label="GPT‑OSS‑120B")
|
78 |
+
# 2️⃣ a single‑line textbox where the user types
|
79 |
+
txt_input = gr.Textbox(
|
80 |
+
placeholder="Type a message and press ⏎ or click Submit...",
|
81 |
+
lines=1,
|
82 |
+
show_label=False,
|
83 |
+
)
|
84 |
+
# 3️⃣ a submit button (optional – hitting Enter on the textbox works too)
|
85 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
86 |
+
|
87 |
+
# ------------------------------------------------------------
|
88 |
+
# 4️⃣ Wire‑up the logic
|
89 |
+
# ------------------------------------------------------------
|
90 |
+
# The function signature is (message, history, token) → (history, reply)
|
91 |
+
# `chatbot` is passed as a *state* component, so we get the current history.
|
92 |
+
# The token comes from the login button (it is added to `additional_inputs`).
|
93 |
+
submit_btn.click(
|
94 |
+
fn=hf_chat,
|
95 |
+
inputs=[txt_input, chatbot, login_btn],
|
96 |
+
outputs=[chatbot, txt_input], # update the history and clear the textbox
|
97 |
+
)
|
98 |
+
# Allow hitting Enter in the textbox to trigger the same call
|
99 |
+
txt_input.submit(
|
100 |
+
fn=hf_chat,
|
101 |
+
inputs=[txt_input, chatbot, login_btn],
|
102 |
+
outputs=[chatbot, txt_input],
|
103 |
+
)
|
104 |
+
|
105 |
demo.launch()
|