Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,81 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
|
5 |
+
# Read your key from env (set this in HF Spaces -> Settings -> Secrets)
|
6 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
7 |
+
if not OPENAI_API_KEY:
|
8 |
+
raise RuntimeError("Missing OPENAI_API_KEY environment variable.")
|
9 |
|
10 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
11 |
+
|
12 |
+
DEFAULT_SYS_PROMPT = "You are a helpful assistant. Be concise and accurate."
|
13 |
+
|
14 |
+
def chat_fn(message, history, system_prompt, temperature, model_name):
|
15 |
+
"""
|
16 |
+
message: latest user text (str)
|
17 |
+
history: list[dict] like [{'role':'user'|'assistant','content': '...'}, ...]
|
18 |
+
system_prompt: system instructions (str)
|
19 |
+
temperature: float
|
20 |
+
model_name: str (e.g., 'gpt-4o-mini' or 'gpt-5')
|
21 |
+
"""
|
22 |
+
# Build messages for the Chat Completions API
|
23 |
+
messages = []
|
24 |
+
if system_prompt and system_prompt.strip():
|
25 |
+
messages.append({"role": "system", "content": system_prompt.strip()})
|
26 |
+
# Append prior turns
|
27 |
+
messages.extend(history or [])
|
28 |
+
# Add the latest user turn
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
# Stream tokens and yield partials for Gradio
|
32 |
+
completion = client.chat.completions.create(
|
33 |
+
model=model_name,
|
34 |
+
messages=messages,
|
35 |
+
temperature=float(temperature),
|
36 |
+
stream=True,
|
37 |
+
)
|
38 |
+
|
39 |
+
partial = ""
|
40 |
+
for chunk in completion:
|
41 |
+
delta = chunk.choices[0].delta
|
42 |
+
if delta and delta.content:
|
43 |
+
partial += delta.content
|
44 |
+
# Yield the *current* partial answer (Gradio replaces the prior one)
|
45 |
+
yield partial
|
46 |
+
|
47 |
+
with gr.Blocks(title="OpenAI x Gradio Chat") as demo:
|
48 |
+
gr.Markdown("## OpenAI × Gradio Chat\nA lightweight, streaming chat app.")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
system_prompt = gr.Textbox(
|
52 |
+
value=DEFAULT_SYS_PROMPT,
|
53 |
+
label="System prompt",
|
54 |
+
lines=2
|
55 |
+
)
|
56 |
+
with gr.Row():
|
57 |
+
model_name = gr.Dropdown(
|
58 |
+
choices=["gpt-4o-mini", "gpt-4o", "gpt-5"],
|
59 |
+
value="gpt-4o-mini",
|
60 |
+
label="Model"
|
61 |
+
)
|
62 |
+
temperature = gr.Slider(0.0, 1.2, value=0.7, step=0.1, label="Temperature")
|
63 |
+
|
64 |
+
chat = gr.ChatInterface(
|
65 |
+
fn=chat_fn,
|
66 |
+
type="messages", # use OpenAI-style message dicts
|
67 |
+
additional_inputs=[system_prompt, temperature, model_name],
|
68 |
+
textbox=gr.Textbox(placeholder="Ask me anything…"),
|
69 |
+
examples=["Explain transformers in one paragraph", "Write a dad joke about databases"],
|
70 |
+
cache_examples=False,
|
71 |
+
theme="soft",
|
72 |
+
retry_btn="Regenerate",
|
73 |
+
undo_btn="Remove last",
|
74 |
+
clear_btn="Clear",
|
75 |
+
save_history=True, # saves chat logs (CSV) in Space filesystem
|
76 |
+
flagging_mode="manual",
|
77 |
+
flagging_options=["👍 Useful", "👎 Not good", "⚠ Inaccurate"],
|
78 |
+
)
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
demo.launch()
|