Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
import os
|
|
|
4 |
|
5 |
HOSTS = {
|
6 |
"Domestic (Lower Latency)": "https://api.chatanywhere.tech/v1",
|
@@ -8,38 +9,86 @@ HOSTS = {
|
|
8 |
}
|
9 |
|
10 |
def create_client(base_url):
|
11 |
-
|
12 |
-
|
13 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
history.append((user_message, partial))
|
37 |
yield history
|
|
|
38 |
except Exception as e:
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
43 |
|
44 |
with gr.Row():
|
45 |
with gr.Column(scale=3):
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
import os
|
4 |
+
import time
|
5 |
|
6 |
HOSTS = {
|
7 |
"Domestic (Lower Latency)": "https://api.chatanywhere.tech/v1",
|
|
|
9 |
}
|
10 |
|
11 |
def create_client(base_url):
|
12 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
13 |
+
if not api_key:
|
14 |
+
raise ValueError("Missing API key. Please set OPENAI_API_KEY in your environment.")
|
15 |
+
return OpenAI(api_key=api_key, base_url=base_url)
|
16 |
|
17 |
def respond_stream(user_message, history, host_choice, temperature):
|
18 |
history = history or []
|
19 |
+
|
20 |
+
# Empty message handling
|
21 |
+
if not user_message.strip():
|
22 |
+
yield history + [("", "β οΈ Please enter a message before sending.")]
|
23 |
+
return
|
24 |
+
|
25 |
+
# Prepare conversation context
|
26 |
messages = [{"role": "system", "content": "You are GPT-5, a helpful, friendly, and concise assistant."}]
|
27 |
for human, ai in history:
|
28 |
messages.append({"role": "user", "content": human})
|
29 |
messages.append({"role": "assistant", "content": ai})
|
30 |
messages.append({"role": "user", "content": user_message})
|
31 |
|
32 |
+
# API Host handling
|
33 |
+
try:
|
34 |
+
client = create_client(HOSTS[host_choice])
|
35 |
+
except ValueError as e:
|
36 |
+
yield history + [(user_message, f"β {e}")]
|
37 |
+
return
|
38 |
+
except Exception as e:
|
39 |
+
yield history + [(user_message, f"β Failed to initialize client: {e}")]
|
40 |
+
return
|
41 |
+
|
42 |
+
# API request with streaming
|
43 |
try:
|
44 |
with client.chat.completions.stream(
|
45 |
model="gpt-5",
|
46 |
messages=messages,
|
47 |
+
temperature=temperature,
|
48 |
+
timeout=30 # network timeout
|
49 |
) as stream:
|
50 |
partial = ""
|
51 |
+
last_update_time = time.time()
|
52 |
+
|
53 |
for event in stream:
|
54 |
if event.type == "message.delta" and event.delta.content:
|
55 |
partial += event.delta.content
|
56 |
+
|
57 |
+
# Update UI at most 5 times per second
|
58 |
+
if time.time() - last_update_time > 0.2:
|
59 |
+
yield history + [(user_message, partial)]
|
60 |
+
last_update_time = time.time()
|
61 |
+
|
62 |
history.append((user_message, partial))
|
63 |
yield history
|
64 |
+
|
65 |
except Exception as e:
|
66 |
+
# Retry with alternate host
|
67 |
+
try:
|
68 |
+
alt_host = HOSTS["Overseas"] if host_choice == "Domestic (Lower Latency)" else HOSTS["Domestic (Lower Latency)"]
|
69 |
+
client = create_client(alt_host)
|
70 |
+
|
71 |
+
with client.chat.completions.stream(
|
72 |
+
model="gpt-5",
|
73 |
+
messages=messages,
|
74 |
+
temperature=temperature,
|
75 |
+
timeout=30
|
76 |
+
) as stream:
|
77 |
+
partial = ""
|
78 |
+
for event in stream:
|
79 |
+
if event.type == "message.delta" and event.delta.content:
|
80 |
+
partial += event.delta.content
|
81 |
+
yield history + [(user_message, partial)]
|
82 |
+
|
83 |
+
history.append((user_message, partial))
|
84 |
+
yield history
|
85 |
+
|
86 |
+
except Exception as e2:
|
87 |
+
yield history + [(user_message, f"β Both API hosts failed.\nHost1 Error: {e}\nHost2 Error: {e2}")]
|
88 |
|
89 |
+
# UI Layout
|
90 |
with gr.Blocks(title="GPT-5 Chatbot", theme=gr.themes.Soft()) as demo:
|
91 |
+
gr.Markdown("<h1 style='text-align:center'>π¬ GPT-5 Chatbot</h1><p style='text-align:center'>Fast, resilient, and beautiful</p>")
|
92 |
|
93 |
with gr.Row():
|
94 |
with gr.Column(scale=3):
|