Abs6187 commited on
Commit
02b1411
Β·
verified Β·
1 Parent(s): dbce7ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -2,25 +2,29 @@ import os
2
  import gradio as gr
3
  from openai import OpenAI
4
 
 
5
  API_HOSTS = {
6
  "Domestic": "https://api.chatanywhere.tech/v1",
7
  "Overseas": "https://api.chatanywhere.org/v1"
8
  }
9
 
 
10
  MODELS_INFO = {
11
- "gpt-3.5-turbo": {"input_price": "0.0035", "output_price": "0.0105", "features": "Default, fast, affordable"},
12
  "gpt-4o": {"input_price": "0.0175", "output_price": "0.07", "features": "Cheaper & faster GPT-4O"},
13
  "gpt-4-turbo": {"input_price": "0.07", "output_price": "0.21", "features": "Multimodal, tool use"},
14
  "gpt-4o-ca": {"input_price": "0.01", "output_price": "0.04", "features": "CA variant, daily free limit"},
15
  }
16
 
17
  def create_client(host):
 
18
  key = os.getenv("OPENAI_API_KEY")
19
  if not key:
20
  raise ValueError("Missing environment variable: OPENAI_API_KEY")
21
  return OpenAI(api_key=key, base_url=host)
22
 
23
  def get_model_card(model_name):
 
24
  info = MODELS_INFO.get(model_name, {})
25
  if not info:
26
  return "Model info not available."
@@ -32,7 +36,9 @@ def get_model_card(model_name):
32
  )
33
 
34
  def respond(user, history, host_choice, model_name, temperature, top_p, max_tokens, sys_prompt):
 
35
  history = history or []
 
36
  if not user.strip():
37
  yield history + [("", "⚠️ Please enter a message.")]
38
  return
@@ -61,12 +67,25 @@ def respond(user, history, host_choice, model_name, temperature, top_p, max_toke
61
 
62
  partial = ""
63
  history.append((user, partial))
64
- yield history
65
 
66
  for chunk in stream:
67
- delta = chunk.choices[0].delta.content or ""
68
- partial += delta
69
- history[-1] = (user, partial)
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  yield history
71
 
72
  except Exception as e:
@@ -76,11 +95,16 @@ def respond(user, history, host_choice, model_name, temperature, top_p, max_toke
76
  "🚫 Daily quota reached for this model.\n"
77
  "Please try again after 00:00 China time or switch model/host."
78
  )
 
 
 
 
79
  else:
80
- out = f"❌ API Error: {e}"
81
  history.append((user, out))
82
  yield history
83
 
 
84
  with gr.Blocks(title="ChatAnywhere Realtime Chatbot", theme=gr.themes.Soft()) as demo:
85
  gr.Markdown("## πŸ’¬ ChatAnywhere Realtime Chatbot\nPowered by GPT-5 via ChatAnywhere API")
86
  with gr.Row():
 
2
  import gradio as gr
3
  from openai import OpenAI
4
 
5
+ # API endpoints
6
  API_HOSTS = {
7
  "Domestic": "https://api.chatanywhere.tech/v1",
8
  "Overseas": "https://api.chatanywhere.org/v1"
9
  }
10
 
11
+ # Model info
12
  MODELS_INFO = {
13
+ "gpt-3.5-turbo": {"input_price": "0.0035", "output_price": "0.0105", "features": "Fast, affordable"},
14
  "gpt-4o": {"input_price": "0.0175", "output_price": "0.07", "features": "Cheaper & faster GPT-4O"},
15
  "gpt-4-turbo": {"input_price": "0.07", "output_price": "0.21", "features": "Multimodal, tool use"},
16
  "gpt-4o-ca": {"input_price": "0.01", "output_price": "0.04", "features": "CA variant, daily free limit"},
17
  }
18
 
19
  def create_client(host):
20
+ """Create OpenAI API client with given host."""
21
  key = os.getenv("OPENAI_API_KEY")
22
  if not key:
23
  raise ValueError("Missing environment variable: OPENAI_API_KEY")
24
  return OpenAI(api_key=key, base_url=host)
25
 
26
  def get_model_card(model_name):
27
+ """Return markdown info for the selected model."""
28
  info = MODELS_INFO.get(model_name, {})
29
  if not info:
30
  return "Model info not available."
 
36
  )
37
 
38
  def respond(user, history, host_choice, model_name, temperature, top_p, max_tokens, sys_prompt):
39
+ """Main chat handler with streaming and error handling."""
40
  history = history or []
41
+
42
  if not user.strip():
43
  yield history + [("", "⚠️ Please enter a message.")]
44
  return
 
67
 
68
  partial = ""
69
  history.append((user, partial))
70
+ yield history # initial blank assistant message
71
 
72
  for chunk in stream:
73
+ try:
74
+ if not chunk.choices:
75
+ continue
76
+ choice = chunk.choices[0]
77
+ if not hasattr(choice, "delta") or not choice.delta:
78
+ continue
79
+ delta = getattr(choice.delta, "content", "") or ""
80
+ if delta:
81
+ partial += delta
82
+ history[-1] = (user, partial)
83
+ yield history
84
+ except Exception:
85
+ continue # skip malformed chunks
86
+
87
+ if not partial.strip():
88
+ history[-1] = (user, "⚠️ No response received from the model.")
89
  yield history
90
 
91
  except Exception as e:
 
95
  "🚫 Daily quota reached for this model.\n"
96
  "Please try again after 00:00 China time or switch model/host."
97
  )
98
+ elif "401" in err or "invalid_api_key" in err.lower():
99
+ out = "❌ Invalid or missing API key. Check your OPENAI_API_KEY."
100
+ elif "timed out" in err.lower():
101
+ out = "βŒ› Request timed out. Please try again."
102
  else:
103
+ out = f"❌ API Error: {err}"
104
  history.append((user, out))
105
  yield history
106
 
107
+
108
  with gr.Blocks(title="ChatAnywhere Realtime Chatbot", theme=gr.themes.Soft()) as demo:
109
  gr.Markdown("## πŸ’¬ ChatAnywhere Realtime Chatbot\nPowered by GPT-5 via ChatAnywhere API")
110
  with gr.Row():