utarn commited on
Commit
1a8c8de
·
1 Parent(s): 0168600
Files changed (1) hide show
  1. app.py +48 -95
app.py CHANGED
@@ -70,50 +70,17 @@ class OmniAPIClient:
70
  return content_parts
71
 
72
  def get_available_models(self, api_key: str = "") -> Tuple[bool, List[str]]:
73
- """Fetch available models from the API"""
74
- try:
75
- # print(f"DEBUG: Fetching models from: {self.models_endpoint}") # Debug line (commented out)
76
- headers = {"Content-Type": "application/json"}
77
- if api_key:
78
- headers["Authorization"] = f"Bearer {api_key}"
79
-
80
- response = requests.get(
81
- self.models_endpoint,
82
- headers=headers,
83
- timeout=10
84
- )
85
-
86
- if response.status_code == 200:
87
- try:
88
- data = response.json()
89
- # Handle different response formats
90
- if "data" in data and isinstance(data["data"], list):
91
- # OpenAI-style format: {"data": [{"id": "model1"}, {"id": "model2"}]}
92
- models = [model.get("id", "") for model in data["data"] if model.get("id")]
93
- elif "models" in data and isinstance(data["models"], list):
94
- # Custom format: {"models": ["model1", "model2"]}
95
- models = data["models"]
96
- elif isinstance(data, list):
97
- # Direct list format: ["model1", "model2"]
98
- models = data
99
- else:
100
- # Fallback: try to extract any string values
101
- models = []
102
- if isinstance(data, dict):
103
- for key, value in data.items():
104
- if isinstance(value, list):
105
- models.extend([str(item) for item in value if item])
106
-
107
- return True, models if models else ["qwen/qwen3-235b-a22b-instruct-2507"] # fallback model
108
- except json.JSONDecodeError:
109
- return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
110
- else:
111
- return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
112
-
113
- except (requests.exceptions.Timeout, requests.exceptions.ConnectionError):
114
- return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
115
- except Exception:
116
- return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
117
 
118
  def send_chat_completion(self, text: str, files: List[str], api_key: str = "", model: str = "qwen/qwen3-235b-a22b-instruct-2507", max_tokens: int = 16384, stream: bool = False) -> Tuple[bool, Any]:
119
  """Send chat completion request to the API"""
@@ -180,20 +147,17 @@ def create_ui():
180
  """Create the Gradio UI"""
181
 
182
  def fetch_models(base_url, api_key):
183
- """Fetch available models from the API"""
184
- if not base_url:
185
- return gr.Dropdown(choices=["qwen/qwen3-235b-a22b-instruct-2507"], value="qwen/qwen3-235b-a22b-instruct-2507")
186
-
187
- try:
188
- client = OmniAPIClient(base_url)
189
- success, models = client.get_available_models(api_key)
190
-
191
- if success and models:
192
- return gr.Dropdown(choices=models, value=models[0] if models else "qwen/qwen3-235b-a22b-instruct-2507")
193
- else:
194
- return gr.Dropdown(choices=["qwen/qwen3-235b-a22b-instruct-2507"], value="qwen/qwen3-235b-a22b-instruct-2507")
195
- except Exception:
196
- return gr.Dropdown(choices=["qwen/qwen3-235b-a22b-instruct-2507"], value="qwen/qwen3-235b-a22b-instruct-2507")
197
 
198
  def send_request(base_url, api_key, model, max_tokens, text, files):
199
  """Handle request submission"""
@@ -238,8 +202,12 @@ def create_ui():
238
  # Check if model contains 'typhoon'
239
  if "typhoon" in model.lower():
240
  try:
241
- # Try to get natural_text first
242
- assistant_reply = choice["message"]["content"]["natural_text"]
 
 
 
 
243
  except (KeyError, TypeError):
244
  # Fallback to content if natural_text is not available
245
  assistant_reply = choice["message"]["content"]
@@ -308,12 +276,17 @@ def create_ui():
308
  with gr.Column(scale=3):
309
  model = gr.Dropdown(
310
  label="Model",
311
- choices=["qwen/qwen3-235b-a22b-instruct-2507"],
 
 
 
 
 
 
 
312
  value="qwen/qwen3-235b-a22b-instruct-2507",
313
  interactive=True
314
  )
315
- with gr.Column(scale=1):
316
- refresh_models_btn = gr.Button("🔄", size="sm")
317
  with gr.Column(scale=2):
318
  max_tokens = gr.Number(
319
  label="Max Tokens",
@@ -408,40 +381,20 @@ def create_ui():
408
  outputs=[text_input, status_output, response_output, file_upload]
409
  )
410
 
411
- # Refresh models when button is clicked
412
- refresh_models_btn.click(
 
 
 
 
 
 
 
 
413
  fn=fetch_models,
414
  inputs=[base_url, api_key],
415
  outputs=[model]
416
  )
417
-
418
- # Auto-refresh models when base URL changes
419
- base_url.blur(
420
- fn=fetch_models,
421
- inputs=[base_url, api_key],
422
- outputs=[model]
423
- )
424
-
425
- # Auto-refresh models when API key changes
426
- api_key.blur(
427
- fn=fetch_models,
428
- inputs=[base_url, api_key],
429
- outputs=[model]
430
- )
431
-
432
- # Allow Enter key to submit (when text input is focused)
433
- text_input.submit(
434
- fn=send_request,
435
- inputs=[base_url, api_key, model, max_tokens, text_input, file_upload],
436
- outputs=[status_output, response_output]
437
- )
438
-
439
- # Preload models when interface loads
440
- interface.load(
441
- fn=fetch_models,
442
- inputs=[base_url, api_key],
443
- outputs=[model]
444
- )
445
 
446
  return interface
447
 
@@ -453,10 +406,10 @@ if __name__ == "__main__":
453
  # Launch with custom settings
454
  demo.launch(
455
  server_name="127.0.0.1", # Use localhost instead of 0.0.0.0
456
- server_port=7890, # Use different port to avoid conflicts
457
  share=False, # Set to True to create public link
458
  debug=True, # Disable debug mode to reduce console errors
459
  show_error=True, # Show detailed error messages
460
- inbrowser=True, # Auto-open in browser
461
  prevent_thread_lock=False # Ensure proper threading
462
  )
 
70
  return content_parts
71
 
72
  def get_available_models(self, api_key: str = "") -> Tuple[bool, List[str]]:
73
+ """Return fixed set of available models"""
74
+ # Fixed set of models as requested
75
+ fixed_models = [
76
+ "typhoon-ocr-preview",
77
+ "openai/gpt-5",
78
+ "meta-llama/llama-4-maverick",
79
+ "qwen/qwen3-235b-a22b-instruct-2507",
80
+ "gemini/gemini-2.5-pro",
81
+ "gemini/gemini-2.5-flash"
82
+ ]
83
+ return True, fixed_models
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  def send_chat_completion(self, text: str, files: List[str], api_key: str = "", model: str = "qwen/qwen3-235b-a22b-instruct-2507", max_tokens: int = 16384, stream: bool = False) -> Tuple[bool, Any]:
86
  """Send chat completion request to the API"""
 
147
  """Create the Gradio UI"""
148
 
149
  def fetch_models(base_url, api_key):
150
+ """Return fixed set of available models"""
151
+ # Fixed set of models as requested
152
+ fixed_models = [
153
+ "typhoon-ocr-preview",
154
+ "openai/gpt-5",
155
+ "meta-llama/llama-4-maverick",
156
+ "qwen/qwen3-235b-a22b-instruct-2507",
157
+ "gemini/gemini-2.5-pro",
158
+ "gemini/gemini-2.5-flash"
159
+ ]
160
+ return gr.Dropdown(choices=fixed_models, value="qwen/qwen3-235b-a22b-instruct-2507")
 
 
 
161
 
162
  def send_request(base_url, api_key, model, max_tokens, text, files):
163
  """Handle request submission"""
 
202
  # Check if model contains 'typhoon'
203
  if "typhoon" in model.lower():
204
  try:
205
+ # If the text is { "natural_text": "..." }, extract the natural_text as json object
206
+ json_content = json.loads(choice["message"]["content"])
207
+ if "natural_text" in json_content:
208
+ assistant_reply = json_content["natural_text"]
209
+ else:
210
+ assistant_reply = choice["message"]["content"]
211
  except (KeyError, TypeError):
212
  # Fallback to content if natural_text is not available
213
  assistant_reply = choice["message"]["content"]
 
276
  with gr.Column(scale=3):
277
  model = gr.Dropdown(
278
  label="Model",
279
+ choices=[
280
+ "typhoon-ocr-preview",
281
+ "openai/gpt-5",
282
+ "meta-llama/llama-4-maverick",
283
+ "qwen/qwen3-235b-a22b-instruct-2507",
284
+ "gemini/gemini-2.5-pro",
285
+ "gemini/gemini-2.5-flash"
286
+ ],
287
  value="qwen/qwen3-235b-a22b-instruct-2507",
288
  interactive=True
289
  )
 
 
290
  with gr.Column(scale=2):
291
  max_tokens = gr.Number(
292
  label="Max Tokens",
 
381
  outputs=[text_input, status_output, response_output, file_upload]
382
  )
383
 
384
+
385
+ # Allow Enter key to submit (when text input is focused)
386
+ text_input.submit(
387
+ fn=send_request,
388
+ inputs=[base_url, api_key, model, max_tokens, text_input, file_upload],
389
+ outputs=[status_output, response_output]
390
+ )
391
+
392
+ # Preload models when interface loads
393
+ interface.load(
394
  fn=fetch_models,
395
  inputs=[base_url, api_key],
396
  outputs=[model]
397
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
 
399
  return interface
400
 
 
406
  # Launch with custom settings
407
  demo.launch(
408
  server_name="127.0.0.1", # Use localhost instead of 0.0.0.0
409
+ server_port=7892, # Use different port to avoid conflicts
410
  share=False, # Set to True to create public link
411
  debug=True, # Disable debug mode to reduce console errors
412
  show_error=True, # Show detailed error messages
413
+ inbrowser=False, # Auto-open in browser
414
  prevent_thread_lock=False # Ensure proper threading
415
  )