Abs6187 commited on
Commit
dd87cb1
·
verified ·
1 Parent(s): b0b9691

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -165
app.py CHANGED
@@ -1,191 +1,93 @@
1
  import os
2
  import time
3
  import json
4
- import traceback
5
  import gradio as gr
6
  from openai import OpenAI
7
 
8
- HOSTS = {
9
- "Domestic (Lower Latency)": "https://api.chatanywhere.tech/v1",
10
- "Overseas (Fallback)": "https://api.chatanywhere.org/v1"
11
  }
12
 
13
- MODELS = {
14
- "o3": {"input": "0.014 / 1K Tokens", "output": "0.056 / 1K Tokens", "support": "support", "features": "Sets new standards for math, science, coding, visual reasoning tasks, and technical writing. Points o3-2025-04-16"},
15
- "o3-2025-04-16": {"input": "0.014 / 1K Tokens", "output": "0.056 / 1K Tokens", "support": "support", "features": "Sets new standards for math, science, coding, visual reasoning tasks, and technical writing."},
16
- "o4-mini": {"input": "0.0088 / 1K Tokens", "output": "0.0352 / 1K Tokens", "support": "support", "features": "Sets new standards for math, science, coding, visual reasoning tasks, and technical writing."},
17
- "o4-mini-2025-04-16": {"input": "0.0088 / 1K Tokens", "output": "0.0352 / 1K Tokens", "support": "support", "features": "Sets new standards for math, science, coding, visual reasoning tasks, and technical writing."},
18
- "gpt-4.1": {"input": "0.014 / 1K Tokens", "output": "0.056 / 1K Tokens", "support": "support", "features": "Improvements in encoding, instruction tracking, and long context. 1M input 32k output."},
19
- "gpt-4.1-2025-04-14": {"input": "0.014 / 1K Tokens", "output": "0.056 / 1K Tokens", "support": "support", "features": "Improvements in encoding, instruction tracking, and long context, with 1M input and 32k output."},
20
- "gpt-4.1-mini": {"input": "0.0028 / 1K Tokens", "output": "0.0112 / 1K Tokens", "support": "support", "features": "Improvements in encoding, instruction tracking, and long context."},
21
- "gpt-4.1-mini-2025-04-14": {"input": "0.0028 / 1K Tokens", "output": "0.0112 / 1K Tokens", "support": "support", "features": "Improvements in encoding, instruction tracking, and long context, with 1M input and 32k output."},
22
- "gpt-4.1-nano": {"input": "0.0007 / 1K Tokens", "output": "0.0028 / 1K Tokens", "support": "support", "features": "Improvements in encoding, instruction tracking, and long context."},
23
- "gpt-4.1-nano-2025-04-14": {"input": "0.0007 / 1K Tokens", "output": "0.0028 / 1K Tokens", "support": "support", "features": "Improvements in encoding, instruction tracking, and long context, with 1M input and 32k output."},
24
- "gpt-oss-20b": {"input": "0.0008 / 1K Tokens", "output": "0.0032 / 1K Tokens", "support": "support", "features": "Open source model."},
25
- "gpt-oss-120b": {"input": "0.0044 / 1K Tokens", "output": "0.0176 / 1K Tokens", "support": "support", "features": "Open source model."},
26
- "gpt-3.5-turbo": {"input": "0.0035 / 1K Tokens", "output": "0.0105 / 1K Tokens", "support": "support", "features": "Default model, equal to gpt-3.5-turbo-0125."},
27
- "gpt-3.5-turbo-1106": {"input": "0.007 / 1K Tokens", "output": "0.014 / 1K Tokens", "support": "support", "features": "Model updated on November 6, 2023."},
28
- "gpt-3.5-turbo-0125": {"input": "0.0035 / 1K Tokens", "output": "0.0105 / 1K Tokens", "support": "support", "features": "Model from January 25, 2024."},
29
- "gpt-3.5-turbo-16k": {"input": "0.021 / 1K Tokens", "output": "0.028 / 1K Tokens", "support": "support", "features": "Longer context (16k)."},
30
- "gpt-3.5-turbo-instruct": {"input": "0.0105 / 1K Tokens", "output": "0.014 / 1K Tokens", "support": "support", "features": "Completions-style instruct model."},
31
- "o1-mini": {"input": "0.0088 / 1K Tokens", "output": "0.0352 / 1K Tokens", "support": "support", "features": "Reasoning models for complex tasks."},
32
- "o1-preview": {"input": "0.105 / 1K Tokens", "output": "0.42 / 1K Tokens", "support": "support", "features": "Preview reasoning model."},
33
- "o3-mini [5]": {"input": "0.0088 / 1K Tokens", "output": "0.0352 / 1K Tokens", "support": "support", "features": "Reasoning models."},
34
- "o1 [5]": {"input": "0.12 / 1K Tokens", "output": "0.48 / 1K Tokens", "support": "support", "features": "Powerful reasoning model."},
35
- "gpt-4o-search-preview": {"input": "0.0175 / 1K Tokens", "output": "0.07 / 1K Tokens", "support": "support", "features": "Search-enabled model (+search fee)."},
36
- "gpt-4o-search-preview-2025-03-11": {"input": "0.0175 / 1K Tokens", "output": "0.07 / 1K Tokens", "support": "support", "features": "Search-enabled model."},
37
- "gpt-4o-mini-search-preview": {"input": "0.00105 / 1K Tokens", "output": "0.0042 / 1K Tokens", "support": "support", "features": "Search-enabled mini model."},
38
- "gpt-4o-mini-search-preview-2025-03-11": {"input": "0.00105 / 1K Tokens", "output": "0.0042 / 1K Tokens", "support": "support", "features": "Search-enabled mini model."},
39
- "gpt-4": {"input": "0.21 / 1K Tokens", "output": "0.42 / 1K Tokens", "support": "support", "features": "Default GPT-4 family model."},
40
- "gpt-4o": {"input": "0.0175 / 1K Tokens + Image Fee", "output": "0.07 / 1K Tokens", "support": "support", "features": "Cheaper/faster GPT-4O variant (+image fee)."},
41
- "gpt-4o-2024-05-13": {"input": "0.035 / 1K Tokens + image fee", "output": "0.105 / 1K Tokens", "support": "support", "features": "GPT-4O release from 2024-05-13."},
42
- "gpt-4o-2024-08-06": {"input": "0.0175 / 1K Tokens + Image Fee", "output": "0.07 / 1K Tokens", "support": "support", "features": "Supports 128k input and 16k output."},
43
- "gpt-4o-2024-11-20": {"input": "0.0175 / 1K Tokens + Image Fee", "output": "0.07 / 1K Tokens", "support": "support", "features": "Improved creative writing."},
44
- "chatgpt-4o-latest": {"input": "0.035 / 1K Tokens + image fee", "output": "0.105 / 1K Tokens", "support": "support", "features": "Dynamically updated version."},
45
- "gpt-4o-mini": {"input": "0.00105 / 1K Tokens + Image Fee", "output": "0.0042 / 1K Tokens", "support": "support", "features": "Mini GPT-4O with image reading."},
46
- "gpt-4-0613": {"input": "0.21 / 1K Tokens", "output": "0.42 / 1K Tokens", "support": "support", "features": "Updated June 13, 2023."},
47
- "gpt-4-turbo-preview": {"input": "0.07 / 1K Tokens", "output": "0.21 / 1K Tokens", "support": "support", "features": "Preview turbo variant (128K input)."},
48
- "gpt-4-0125-preview": {"input": "0.07 / 1K Tokens", "output": "0.21 / 1K Tokens", "support": "support", "features": "Preview updated Jan 25, 2024."},
49
- "gpt-4-1106-preview": {"input": "0.07 / 1K Tokens", "output": "0.21 / 1K Tokens", "support": "support", "features": "Preview updated Nov 6, 2023."},
50
- "gpt-4-vision-preview": {"input": "0.07 / 1K Tokens + Image Fee", "output": "0.21 / 1K Tokens", "support": "support", "features": "Multimodal with image recognition."},
51
- "gpt-4-turbo": {"input": "0.07 / 1K Tokens + Image Fee", "output": "0.21 / 1K Tokens", "support": "support", "features": "Multimodal, function tools."},
52
- "gpt-4-turbo-2024-04-09": {"input": "0.07 / 1K Tokens + image fee", "output": "0.21 / 1K Tokens", "support": "support", "features": "Preview turbo model."},
53
- "gpt-4.1-ca": {"input": "0.008 / 1K Tokens", "output": "0.032 / 1K Tokens", "support": "support", "features": "Third-party provider CA variant."},
54
- "gpt-4.1-mini-ca": {"input": "0.0016 / 1K Tokens", "output": "0.0064 / 1K Tokens", "support": "support", "features": "CA mini variant."},
55
- "gpt-4.1-nano-ca": {"input": "0.0004 / 1K Tokens", "output": "0.003 / 1K Tokens", "support": "support", "features": "CA nano variant."},
56
- "gpt-3.5-turbo-ca": {"input": "0.001 / 1K Tokens", "output": "0.0016 / 1K Tokens", "support": "support", "features": "CA region variant."},
57
- "gpt-4-ca": {"input": "0.12 / 1K Tokens", "output": "0.24 / 1K Tokens", "support": "support", "features": "CA region variant."},
58
- "gpt-4-turbo-ca": {"input": "0.04 / 1K Tokens + image fees", "output": "0.12 / 1K Tokens", "support": "support", "features": "CA region turbo."},
59
- "gpt-4o-ca": {"input": "0.01 / 1K Tokens + image fees", "output": "0.04 / 1K Tokens", "support": "support", "features": "CA region GPT-4O."},
60
- "gpt-4o-mini-ca": {"input": "0.00075 / 1K Tokens", "output": "0.003 / 1K Tokens", "support": "support", "features": "CA mini."},
61
- "o1-mini-ca": {"input": "0.012 / 1K Tokens", "output": "0.048 / 1K Tokens", "support": "support", "features": "CA reasoning mini."},
62
- "o1-preview-ca": {"input": "0.06 / 1K Tokens", "output": "0.24 / 1K Tokens", "support": "support", "features": "CA preview reasoning."}
63
-
64
  }
65
 
66
- def create_client(base_url):
67
- api_key = os.getenv("OPENAI_API_KEY")
68
- if not api_key:
69
- raise ValueError("Missing OPENAI_API_KEY environment variable")
70
- return OpenAI(api_key=api_key, base_url=base_url)
71
 
72
  def get_model_card(model_name):
73
- m = MODELS.get(model_name)
74
- if not m:
75
- return "Model not found in catalog"
76
- return f"**{model_name}**\n\nInput price: {m['input']}\n\nOutput price: {m['output']}\n\nSupport: {m['support']}\n\n{m['features']}"
 
 
 
 
 
77
 
78
- def export_history(history):
 
 
 
79
  try:
80
- fname = f"chat_history_{int(time.time())}.json"
81
- with open(fname, "w", encoding="utf-8") as f:
82
- json.dump([{"user": u, "assistant": a} for u, a in (history or [])], f, ensure_ascii=False, indent=2)
83
- return f"Saved to {fname}"
84
  except Exception as e:
85
- return f"Export failed: {e}"
86
-
87
- def respond_stream(user_message, history, host_choice, model_name, temperature, top_p, max_tokens, system_prompt):
88
- history = history or []
89
- if not user_message or not user_message.strip():
90
- yield history + [("", "⚠️ Please enter a message to send.")]
91
- return
92
- messages = []
93
- sys_prompt = system_prompt.strip() if system_prompt and system_prompt.strip() else "You are a helpful, concise assistant."
94
- messages.append({"role": "system", "content": sys_prompt})
95
- for human, ai in history:
96
- messages.append({"role": "user", "content": human})
97
- messages.append({"role": "assistant", "content": ai})
98
- messages.append({"role": "user", "content": user_message})
99
- last_error = None
100
- hosts_to_try = [HOSTS.get(host_choice)] if HOSTS.get(host_choice) else list(HOSTS.values())
101
- for base in hosts_to_try:
102
- try:
103
- client = create_client(base)
104
- except Exception as e:
105
- last_error = e
106
- continue
107
- try:
108
- try:
109
- stream = client.chat.completions.stream(model=model_name, messages=messages, temperature=float(temperature), top_p=float(top_p), max_tokens=int(max_tokens), timeout=30)
110
- partial = ""
111
- last_update = time.time()
112
- with stream as s:
113
- for event in s:
114
- try:
115
- if getattr(event, "type", None) == "message.delta":
116
- delta = getattr(event, "delta", None)
117
- content = getattr(delta, "content", None) if delta is not None else None
118
- if content:
119
- partial += content
120
- if time.time() - last_update > 0.15:
121
- yield history + [(user_message, partial)]
122
- last_update = time.time()
123
- elif getattr(event, "type", None) == "message":
124
- content = getattr(event, "message", None)
125
- content_text = None
126
- if content and hasattr(content, "content"):
127
- content_text = getattr(content.content, "get", lambda k, d=None: None)("text", None) if hasattr(content, "content") else None
128
- if content_text:
129
- partial += content_text
130
- yield history + [(user_message, partial)]
131
- except Exception:
132
- pass
133
- if not partial.strip():
134
- partial = "No output received from model. Possible reasons: invalid model, API error, usage limits, or network timeout."
135
- history.append((user_message, partial))
136
- yield history
137
- return
138
- except AttributeError:
139
- resp = client.chat.completions.create(model=model_name, messages=messages, temperature=float(temperature), top_p=float(top_p), max_tokens=int(max_tokens))
140
- bot_reply = ""
141
- try:
142
- bot_reply = resp.choices[0].message.content
143
- except Exception:
144
- try:
145
- bot_reply = resp["choices"][0]["message"]["content"]
146
- except Exception:
147
- bot_reply = str(resp)
148
- if not bot_reply or not str(bot_reply).strip():
149
- bot_reply = "No output received from model. Possible reasons: invalid model, API error, usage limits, or network timeout."
150
- history.append((user_message, bot_reply))
151
- yield history
152
- return
153
- except Exception as e:
154
- last_error = e
155
- continue
156
- err_text = f"❌ All hosts failed. Last error: {last_error}\nCheck OPENAI_API_KEY, selected model, and network connectivity."
157
- history.append((user_message, err_text))
158
- yield history
159
 
160
- model_choices = sorted(MODELS.keys())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
- with gr.Blocks(title="Polished GPT App", theme=gr.themes.Soft()) as demo:
163
- gr.Markdown("<h2 style='text-align:center'>Polished GPT UIModel Catalog Integrated</h2>")
164
  with gr.Row():
165
  with gr.Column(scale=3):
166
- chatbot = gr.Chatbot(label="Conversation", height=520, show_copy_button=True, render_markdown=True)
167
- with gr.Row():
168
- txt = gr.Textbox(placeholder="Type a message and press Enter", lines=2)
169
- send = gr.Button("Send", variant="primary")
170
- with gr.Row():
171
- stop_btn = gr.Button("Stop", variant="secondary")
172
- clear_btn = gr.Button("Clear", variant="secondary")
173
- export_btn = gr.Button("Export")
174
- status = gr.Markdown("")
175
  with gr.Column(scale=1):
176
- host = gr.Radio(list(HOSTS.keys()), value="Domestic (Lower Latency)", label="API Host")
177
- model_dropdown = gr.Dropdown(model_choices, value="gpt-3.5-turbo", label="Model")
178
  model_card = gr.Markdown(get_model_card("gpt-3.5-turbo"))
179
  temperature = gr.Slider(0.0, 1.5, value=0.7, step=0.05, label="Temperature")
180
  top_p = gr.Slider(0.05, 1.0, value=1.0, step=0.05, label="Top-p")
181
  max_tokens = gr.Slider(64, 8192, value=512, step=64, label="Max Tokens")
182
- system_prompt = gr.Textbox(label="System Prompt (optional)", lines=3, placeholder="You are a helpful assistant.")
183
- send.click(respond_stream, [txt, chatbot, host, model_dropdown, temperature, top_p, max_tokens, system_prompt], chatbot, queue=True)
184
- txt.submit(respond_stream, [txt, chatbot, host, model_dropdown, temperature, top_p, max_tokens, system_prompt], chatbot, queue=True)
185
- model_dropdown.change(lambda m: get_model_card(m), model_dropdown, model_card)
186
- clear_btn.click(lambda: [], None, chatbot)
187
- export_btn.click(lambda h: export_history(h), chatbot, status)
188
- stop_btn.click(lambda: "stop", None, None)
189
 
190
  if __name__ == "__main__":
191
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_api=False)
 
1
  import os
2
  import time
3
  import json
 
4
  import gradio as gr
5
  from openai import OpenAI
6
 
7
+ API_HOSTS = {
8
+ "Domestic": "https://api.chatanywhere.tech/v1",
9
+ "Overseas": "https://api.chatanywhere.org/v1"
10
  }
11
 
12
+ MODELS_INFO = {
13
+ "gpt-3.5-turbo": {"input_price": "0.0035", "output_price": "0.0105", "features": "Default model, equivalent to gpt-3.5-turbo-0125"},
14
+ "o1-preview": {"input_price": "0.105", "output_price": "0.42", "features": "Powerful preview reasoning model"},
15
+ "gpt-4o": {"input_price": "0.0175", "output_price": "0.07", "features": "Cheaper and faster GPT-4O; supports image fee"},
16
+ "gpt-4-turbo": {"input_price": "0.07", "output_price": "0.21", "features": "Multimodal with image recognition, function tools support"},
17
+ "gpt-4o-ca": {"input_price": "0.01", "output_price": "0.04", "features": "Cheap CA variant, but limited stability/prior daily limit"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
 
20
+ def create_client(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
+ info = MODELS_INFO.get(model_name, {})
28
+ if not info:
29
+ return "Model info not available."
30
+ return (
31
+ f"**{model_name}**\n\n"
32
+ f"Input price (/1K tokens): {info['input_price']}\n\n"
33
+ f"Output price (/1K tokens): {info['output_price']}\n\n"
34
+ f"Features: {info['features']}"
35
+ )
36
 
37
+ def respond(user, history, host_choice, model_name, temperature, top_p, max_tokens, sys_prompt):
38
+ history = history or []
39
+ if not user.strip():
40
+ return history + [("", "⚠️ Please enter a message.")]
41
  try:
42
+ client = create_client(API_HOSTS[host_choice])
 
 
 
43
  except Exception as e:
44
+ return history + [("", f" {e}")]
45
+ messages = [{"role": "system", "content": sys_prompt or "You are a helpful assistant."}]
46
+ for u, a in history:
47
+ messages.append({"role": "user", "content": u})
48
+ messages.append({"role": "assistant", "content": a})
49
+ messages.append({"role": "user", "content": user})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ try:
52
+ resp = client.chat.completions.create(
53
+ model=model_name,
54
+ messages=messages,
55
+ temperature=temperature,
56
+ top_p=top_p,
57
+ max_tokens=max_tokens
58
+ )
59
+ out = resp.choices[0].message.content.strip() or "No response received."
60
+ except Exception as e:
61
+ err = str(e)
62
+ if "429" in err:
63
+ out = (
64
+ "🚫 Daily quota reached for the selected model (429).\n"
65
+ "Please try again after 00:00 (China time) or switch models/hosts."
66
+ )
67
+ else:
68
+ out = f"❌ API Error: {e}"
69
+ history.append((user, out))
70
+ return history
71
 
72
+ with gr.Blocks(title="ChatAnywhere-powered Chatbot", theme=gr.themes.Soft()) as demo:
73
+ gr.Markdown("## ChatAnywhere Chatbot\nPowered by the ChatAnywhere API Use wisely!")
74
  with gr.Row():
75
  with gr.Column(scale=3):
76
+ chat = gr.Chatbot(label="Chat", height=500, show_copy_button=True, render_markdown=True)
77
+ msg = gr.Textbox(placeholder="Type your message...", lines=2)
78
+ clear = gr.Button("Clear")
 
 
 
 
 
 
79
  with gr.Column(scale=1):
80
+ host = gr.Radio(list(API_HOSTS.keys()), value="Domestic", label="API Host")
81
+ model = gr.Dropdown(list(MODELS_INFO.keys()), value="gpt-3.5-turbo", label="Model")
82
  model_card = gr.Markdown(get_model_card("gpt-3.5-turbo"))
83
  temperature = gr.Slider(0.0, 1.5, value=0.7, step=0.05, label="Temperature")
84
  top_p = gr.Slider(0.05, 1.0, value=1.0, step=0.05, label="Top-p")
85
  max_tokens = gr.Slider(64, 8192, value=512, step=64, label="Max Tokens")
86
+ sys_prompt = gr.Textbox(label="System Prompt (optional)", lines=2)
87
+ msg.submit(respond, [msg, chat, host, model, temperature, top_p, max_tokens, sys_prompt], chat)
88
+ model.change(lambda m: get_model_card(m), model, model_card)
89
+ clear.click(lambda: [], None, chat)
90
+ msg.submit(lambda _: "", msg, msg)
 
 
91
 
92
  if __name__ == "__main__":
93
+ demo.launch()