Spaces:
Running
Running
Support Dual API ENV setting
Browse files
app.py
CHANGED
@@ -9,6 +9,9 @@ from lang import LANGUAGE_CONFIG
|
|
9 |
# 环境变量预校验
|
10 |
load_dotenv(override=True)
|
11 |
required_env_vars = ["API_KEY", "API_URL", "API_MODEL"]
|
|
|
|
|
|
|
12 |
missing_vars = [var for var in required_env_vars if not os.getenv(var)]
|
13 |
if missing_vars:
|
14 |
raise EnvironmentError(
|
@@ -121,6 +124,14 @@ class ConvoState:
|
|
121 |
self.is_error = False
|
122 |
self.result_editing_toggle = False
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
def initialize_new_round(self):
|
125 |
self.current = {}
|
126 |
self.current["user"] = ""
|
@@ -150,11 +161,13 @@ class ConvoState:
|
|
150 |
dynamic_state.stream_completed = False
|
151 |
full_response = current_content
|
152 |
self.current["raw"] = full_response
|
|
|
153 |
api_client = OpenAI(
|
154 |
-
api_key=
|
155 |
-
base_url=
|
156 |
timeout=AppConfig.API_TIMEOUT,
|
157 |
)
|
|
|
158 |
coordinator = CoordinationManager(self.sync_threshold, current_content)
|
159 |
|
160 |
editor_output = current_content
|
@@ -290,6 +303,14 @@ def update_interface_language(selected_lang, convo_state, dynamic_state):
|
|
290 |
else lang_data["interrupted"]
|
291 |
)
|
292 |
editor_label = f"{base_editor_label} - {status_suffix}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
return [
|
294 |
gr.update(value=f"{lang_data['title']}"),
|
295 |
gr.update(
|
@@ -316,7 +337,10 @@ def update_interface_language(selected_lang, convo_state, dynamic_state):
|
|
316 |
value=lang_data["clear_btn"], interactive=not dynamic_state.should_stream
|
317 |
),
|
318 |
gr.update(value=lang_data["introduction"]),
|
319 |
-
gr.update(
|
|
|
|
|
|
|
320 |
gr.update(label=lang_data["result_editing_toggle"]),
|
321 |
]
|
322 |
|
@@ -332,10 +356,18 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
332 |
bot_default = LANGUAGE_CONFIG["en"]["bot_default"] + [
|
333 |
{
|
334 |
"role": "assistant",
|
335 |
-
"content": f"Running `{os.getenv('API_MODEL')}` @ {os.getenv('API_URL')}
|
336 |
-
"metadata": {"title": f"API INFO"},
|
337 |
}
|
338 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
|
340 |
with gr.Row(variant=""):
|
341 |
title_md = gr.Markdown(
|
|
|
9 |
# 环境变量预校验
|
10 |
load_dotenv(override=True)
|
11 |
required_env_vars = ["API_KEY", "API_URL", "API_MODEL"]
|
12 |
+
secondary_api_exists = all(
|
13 |
+
os.getenv(f"{var}_2") for var in ["API_KEY", "API_URL", "API_MODEL"]
|
14 |
+
)
|
15 |
missing_vars = [var for var in required_env_vars if not os.getenv(var)]
|
16 |
if missing_vars:
|
17 |
raise EnvironmentError(
|
|
|
124 |
self.is_error = False
|
125 |
self.result_editing_toggle = False
|
126 |
|
127 |
+
def get_api_config(self, language):
|
128 |
+
suffix = "_2" if language == "zh" and secondary_api_exists else ""
|
129 |
+
return {
|
130 |
+
"key": os.getenv(f"API_KEY{suffix}"),
|
131 |
+
"url": os.getenv(f"API_URL{suffix}"),
|
132 |
+
"model": os.getenv(f"API_MODEL{suffix}"),
|
133 |
+
}
|
134 |
+
|
135 |
def initialize_new_round(self):
|
136 |
self.current = {}
|
137 |
self.current["user"] = ""
|
|
|
161 |
dynamic_state.stream_completed = False
|
162 |
full_response = current_content
|
163 |
self.current["raw"] = full_response
|
164 |
+
api_config = self.get_api_config(self.current_language)
|
165 |
api_client = OpenAI(
|
166 |
+
api_key=api_config["key"],
|
167 |
+
base_url=api_config["url"],
|
168 |
timeout=AppConfig.API_TIMEOUT,
|
169 |
)
|
170 |
+
|
171 |
coordinator = CoordinationManager(self.sync_threshold, current_content)
|
172 |
|
173 |
editor_output = current_content
|
|
|
303 |
else lang_data["interrupted"]
|
304 |
)
|
305 |
editor_label = f"{base_editor_label} - {status_suffix}"
|
306 |
+
api_config = convo_state.get_api_config(selected_lang)
|
307 |
+
new_bot_content = [
|
308 |
+
{
|
309 |
+
"role": "assistant",
|
310 |
+
"content": f"{selected_lang} - Running `{api_config['model']}` @ {api_config['url']}",
|
311 |
+
"metadata": {"title": f"API INFO"},
|
312 |
+
}
|
313 |
+
]
|
314 |
return [
|
315 |
gr.update(value=f"{lang_data['title']}"),
|
316 |
gr.update(
|
|
|
337 |
value=lang_data["clear_btn"], interactive=not dynamic_state.should_stream
|
338 |
),
|
339 |
gr.update(value=lang_data["introduction"]),
|
340 |
+
gr.update(
|
341 |
+
value=lang_data["bot_default"] + new_bot_content,
|
342 |
+
label=lang_data["bot_label"],
|
343 |
+
),
|
344 |
gr.update(label=lang_data["result_editing_toggle"]),
|
345 |
]
|
346 |
|
|
|
356 |
bot_default = LANGUAGE_CONFIG["en"]["bot_default"] + [
|
357 |
{
|
358 |
"role": "assistant",
|
359 |
+
"content": f"Running `{os.getenv('API_MODEL')}` @ {os.getenv('API_URL')}",
|
360 |
+
"metadata": {"title": f"EN API INFO"},
|
361 |
}
|
362 |
]
|
363 |
+
if secondary_api_exists:
|
364 |
+
bot_default.append(
|
365 |
+
{
|
366 |
+
"role": "assistant",
|
367 |
+
"content": f"Switch to zh ↗ to use SiliconFlow API: `{os.getenv('API_MODEL_2')}` @ {os.getenv('API_URL_2')}",
|
368 |
+
"metadata": {"title": f"CN API INFO"},
|
369 |
+
}
|
370 |
+
)
|
371 |
|
372 |
with gr.Row(variant=""):
|
373 |
title_md = gr.Markdown(
|