Update app.py
Browse files
app.py
CHANGED
@@ -2,42 +2,76 @@ import gradio as gr
|
|
2 |
from openai import OpenAI
|
3 |
import os
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
history = history or []
|
12 |
-
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
13 |
for human, ai in history:
|
14 |
messages.append({"role": "user", "content": human})
|
15 |
messages.append({"role": "assistant", "content": ai})
|
16 |
messages.append({"role": "user", "content": user_message})
|
|
|
|
|
17 |
try:
|
18 |
-
|
19 |
-
model="gpt-
|
20 |
-
messages=messages
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
except Exception as e:
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
-
demo.launch()
|
|
|
2 |
from openai import OpenAI
|
3 |
import os
|
4 |
|
5 |
+
HOSTS = {
|
6 |
+
"Domestic (Lower Latency)": "https://api.chatanywhere.tech/v1",
|
7 |
+
"Overseas": "https://api.chatanywhere.org/v1"
|
8 |
+
}
|
9 |
|
10 |
+
def create_client(base_url):
|
11 |
+
return OpenAI(
|
12 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
13 |
+
base_url=base_url
|
14 |
+
)
|
15 |
+
|
16 |
+
def respond_stream(user_message, history, host_choice, temperature):
|
17 |
history = history or []
|
18 |
+
messages = [{"role": "system", "content": "You are GPT-5, a helpful, friendly, and concise 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": user_message})
|
23 |
+
|
24 |
+
client = create_client(HOSTS[host_choice])
|
25 |
try:
|
26 |
+
with client.chat.completions.stream(
|
27 |
+
model="gpt-5",
|
28 |
+
messages=messages,
|
29 |
+
temperature=temperature
|
30 |
+
) as stream:
|
31 |
+
partial = ""
|
32 |
+
for event in stream:
|
33 |
+
if event.type == "message.delta" and event.delta.content:
|
34 |
+
partial += event.delta.content
|
35 |
+
yield history + [(user_message, partial)]
|
36 |
+
history.append((user_message, partial))
|
37 |
+
yield history
|
38 |
except Exception as e:
|
39 |
+
yield history + [(user_message, f"❌ Error: {e}")]
|
40 |
+
|
41 |
+
with gr.Blocks(title="GPT-5 Chatbot", theme=gr.themes.Soft()) as demo:
|
42 |
+
gr.Markdown("<h1 style='text-align:center'>💬 GPT-5 Chatbot</h1><p style='text-align:center'>Fast, interactive, and pretty</p>")
|
43 |
+
|
44 |
+
with gr.Row():
|
45 |
+
with gr.Column(scale=3):
|
46 |
+
chatbot = gr.Chatbot(
|
47 |
+
label="Conversation",
|
48 |
+
bubble_full_width=False,
|
49 |
+
height=500,
|
50 |
+
show_copy_button=True,
|
51 |
+
render_markdown=True
|
52 |
+
)
|
53 |
+
msg = gr.Textbox(placeholder="Type your message...", lines=1)
|
54 |
+
with gr.Row():
|
55 |
+
send_btn = gr.Button("Send", variant="primary")
|
56 |
+
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
57 |
+
|
58 |
+
with gr.Column(scale=1):
|
59 |
+
host_choice = gr.Radio(
|
60 |
+
label="API Host",
|
61 |
+
choices=list(HOSTS.keys()),
|
62 |
+
value="Domestic (Lower Latency)"
|
63 |
+
)
|
64 |
+
temperature = gr.Slider(
|
65 |
+
label="Creativity (Temperature)",
|
66 |
+
minimum=0.0,
|
67 |
+
maximum=1.5,
|
68 |
+
value=0.7,
|
69 |
+
step=0.1
|
70 |
+
)
|
71 |
+
|
72 |
+
send_btn.click(respond_stream, [msg, chatbot, host_choice, temperature], chatbot, queue=True)
|
73 |
+
msg.submit(respond_stream, [msg, chatbot, host_choice, temperature], chatbot, queue=True)
|
74 |
+
clear_btn.click(lambda: None, None, chatbot)
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
+
demo.queue().launch()
|