File size: 17,788 Bytes
0168600 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
import gradio as gr
import requests
import json
import base64
import os
from typing import List, Optional, Tuple, Any
import mimetypes
class OmniAPIClient:
"""Client for interacting with the Omni API"""
def __init__(self, base_url: str = "https://api.modelharbor.com"):
self.base_url = base_url.rstrip('/')
self.chat_endpoint = f"{self.base_url}/v1/chat/completions"
self.models_endpoint = f"{self.base_url}/v1/models"
def encode_file_to_base64(self, file_path: str) -> str:
"""Encode file to base64 string"""
with open(file_path, "rb") as file:
return base64.b64encode(file.read()).decode('utf-8')
def get_mime_type(self, file_path: str) -> str:
"""Get MIME type of file"""
mime_type, _ = mimetypes.guess_type(file_path)
return mime_type or "application/octet-stream"
def create_file_content(self, file_path: str, file_type: str) -> dict:
"""Create file content object based on API format"""
file_name = os.path.basename(file_path)
mime_type = self.get_mime_type(file_path)
# Check if the file is an image
if mime_type and mime_type.startswith('image/'):
# Handle images with the new format
file_data_b64 = self.encode_file_to_base64(file_path)
return {
"type": "image_url",
"image_url": {
"url": f"data:{mime_type};base64,{file_data_b64}"
}
}
else:
# Handle other files with existing logic
file_data_b64 = self.encode_file_to_base64(file_path)
return {
"type": "file",
"file": {
"filename": file_name,
"file_data": f"data:{mime_type};base64,{file_data_b64}"
}
}
def build_message_content(self, text: str, files: List[str]) -> List[dict]:
"""Build message content with text and files"""
content_parts = []
# Add text content first
if text.strip():
content_parts.append({
"type": "text",
"text": text
})
# Add files in order
for file_path in files:
if file_path and os.path.exists(file_path):
file_content = self.create_file_content(file_path, "file")
content_parts.append(file_content)
return content_parts
def get_available_models(self, api_key: str = "") -> Tuple[bool, List[str]]:
"""Fetch available models from the API"""
try:
# print(f"DEBUG: Fetching models from: {self.models_endpoint}") # Debug line (commented out)
headers = {"Content-Type": "application/json"}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
response = requests.get(
self.models_endpoint,
headers=headers,
timeout=10
)
if response.status_code == 200:
try:
data = response.json()
# Handle different response formats
if "data" in data and isinstance(data["data"], list):
# OpenAI-style format: {"data": [{"id": "model1"}, {"id": "model2"}]}
models = [model.get("id", "") for model in data["data"] if model.get("id")]
elif "models" in data and isinstance(data["models"], list):
# Custom format: {"models": ["model1", "model2"]}
models = data["models"]
elif isinstance(data, list):
# Direct list format: ["model1", "model2"]
models = data
else:
# Fallback: try to extract any string values
models = []
if isinstance(data, dict):
for key, value in data.items():
if isinstance(value, list):
models.extend([str(item) for item in value if item])
return True, models if models else ["qwen/qwen3-235b-a22b-instruct-2507"] # fallback model
except json.JSONDecodeError:
return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
else:
return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError):
return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
except Exception:
return False, ["qwen/qwen3-235b-a22b-instruct-2507"]
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]:
"""Send chat completion request to the API"""
try:
# Build message content
content_parts = self.build_message_content(text, files)
# If no content parts, return error
if not content_parts:
return False, {"error": "No text or valid files provided"}
# Build request payload
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": content_parts
}
],
"max_tokens": max_tokens,
"stream": stream
}
# Build headers
headers = {
"Content-Type": "application/json"
}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
# Send request
response = requests.post(
self.chat_endpoint,
json=payload,
headers=headers,
timeout=60
)
# Check response
if response.status_code == 200:
try:
response_data = response.json()
return True, response_data
except json.JSONDecodeError:
return False, {"error": "Invalid JSON response", "raw_response": response.text}
else:
try:
error_data = response.json()
return False, {"error": f"API Error ({response.status_code})", "details": error_data}
except json.JSONDecodeError:
return False, {"error": f"HTTP {response.status_code}", "raw_response": response.text}
except requests.exceptions.Timeout:
return False, {"error": "Request timeout"}
except requests.exceptions.ConnectionError:
return False, {"error": "Connection error"}
except Exception as e:
return False, {"error": f"Unexpected error: {str(e)}"}
def create_ui():
"""Create the Gradio UI"""
def fetch_models(base_url, api_key):
"""Fetch available models from the API"""
if not base_url:
return gr.Dropdown(choices=["qwen/qwen3-235b-a22b-instruct-2507"], value="qwen/qwen3-235b-a22b-instruct-2507")
try:
client = OmniAPIClient(base_url)
success, models = client.get_available_models(api_key)
if success and models:
return gr.Dropdown(choices=models, value=models[0] if models else "qwen/qwen3-235b-a22b-instruct-2507")
else:
return gr.Dropdown(choices=["qwen/qwen3-235b-a22b-instruct-2507"], value="qwen/qwen3-235b-a22b-instruct-2507")
except Exception:
return gr.Dropdown(choices=["qwen/qwen3-235b-a22b-instruct-2507"], value="qwen/qwen3-235b-a22b-instruct-2507")
def send_request(base_url, api_key, model, max_tokens, text, files):
"""Handle request submission"""
try:
# Validate inputs
if not base_url:
return "β Base URL is required", ""
if not text.strip() and not files:
return "β Please provide either text or upload files", ""
# Create client
client = OmniAPIClient(base_url)
# Filter out None/empty files - handle various file input states
valid_files = []
if files is not None:
# Handle single file or list of files
if isinstance(files, list):
valid_files = [f.name for f in files if f is not None and hasattr(f, 'name')]
elif hasattr(files, 'name'):
# Single file object
valid_files = [files.name]
# Send request
success, response = client.send_chat_completion(
text=text,
files=valid_files,
api_key=api_key,
model=model,
max_tokens=max_tokens
)
if success:
# Format successful response
formatted_response = json.dumps(response, indent=2)
# Extract the assistant's reply if available
if "choices" in response and len(response["choices"]) > 0:
choice = response["choices"][0]
if "message" in choice and "content" in choice["message"]:
# Check if model contains 'typhoon'
if "typhoon" in model.lower():
try:
# Try to get natural_text first
assistant_reply = choice["message"]["content"]["natural_text"]
except (KeyError, TypeError):
# Fallback to content if natural_text is not available
assistant_reply = choice["message"]["content"]
else:
assistant_reply = choice["message"]["content"]
status = f"β
Request successful\n\n**Assistant Reply:**\n{assistant_reply}"
else:
status = "β
Request successful"
else:
status = "β
Request successful"
return status, formatted_response
else:
# Format error response
error_response = json.dumps(response, indent=2)
return f"β Request failed", error_response
except Exception as e:
return f"β Error: {str(e)}", ""
def clear_form():
"""Clear all form inputs"""
return "", "", "", None
# Custom CSS for better layout
css = """
.gradio-container {
max-width: 1200px;
}
.config-panel {
background-color: #f8f9fa;
border-radius: 8px;
padding: 15px;
margin-bottom: 20px;
}
.input-panel {
border-right: 1px solid #e0e0e0;
padding-right: 20px;
}
.output-panel {
padding-left: 20px;
}
"""
with gr.Blocks(css=css, title="Omni API Chat Interface") as interface:
gr.Markdown("# π€ Omni API Chat Interface")
gr.Markdown("Interact with the Omni API using text, PDFs, images, and audio files")
# Configuration section
with gr.Group(elem_classes=["config-panel"]):
gr.Markdown("## βοΈ Configuration")
with gr.Row():
base_url = gr.Textbox(
label="API Base URL",
value="https://api.modelharbor.com",
placeholder="https://api.modelharbor.com"
)
api_key = gr.Textbox(
label="API Key (Optional)",
type="password",
placeholder="Enter your API key if required"
)
with gr.Row():
with gr.Column(scale=3):
model = gr.Dropdown(
label="Model",
choices=["qwen/qwen3-235b-a22b-instruct-2507"],
value="qwen/qwen3-235b-a22b-instruct-2507",
interactive=True
)
with gr.Column(scale=1):
refresh_models_btn = gr.Button("π", size="sm")
with gr.Column(scale=2):
max_tokens = gr.Number(
label="Max Tokens",
value=16384,
minimum=1,
maximum=32000
)
# Main interface
with gr.Row():
# Input panel (left side)
with gr.Column(scale=1, elem_classes=["input-panel"]):
gr.Markdown("## π Input")
text_input = gr.Textbox(
label="Your Message",
placeholder="Type your message here...",
lines=5
)
file_upload = gr.File(
label="Upload Files",
file_count="multiple",
file_types=[
".pdf", ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp",
".mp3", ".wav", ".m4a", ".flac", ".ogg"
]
)
with gr.Row():
send_btn = gr.Button("π Send Request", variant="primary", size="lg")
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
# Output panel (right side)
with gr.Column(scale=1, elem_classes=["output-panel"]):
gr.Markdown("## π€ Response")
status_output = gr.Textbox(
label="Status",
placeholder="Response status will appear here...",
lines=8,
max_lines=15,
interactive=False
)
response_output = gr.Code(
label="Raw Response",
language="json",
interactive=False
)
# Example section
with gr.Accordion("π Usage Examples", open=False):
gr.Markdown("""
### Example Requests:
**Text Only:**
- Message: "Hello, how are you?"
- Files: None
**PDF Analysis:**
- Message: "Please summarize this document"
- Files: document.pdf
**Image OCR:**
- Message: "Extract text from this image"
- Files: receipt.jpg
**Audio Transcription:**
- Message: "Transcribe this audio file"
- Files: meeting.mp3
**Multi-modal:**
- Message: "Analyze these files and provide insights"
- Files: report.pdf, chart.png, recording.wav
### Supported File Types:
- **PDFs**: .pdf
- **Images**: .jpg, .jpeg, .png, .gif, .bmp, .webp
- **Audio**: .mp3, .wav, .m4a, .flac, .ogg
""")
# Event handlers
send_btn.click(
fn=send_request,
inputs=[base_url, api_key, model, max_tokens, text_input, file_upload],
outputs=[status_output, response_output]
)
clear_btn.click(
fn=clear_form,
outputs=[text_input, status_output, response_output, file_upload]
)
# Refresh models when button is clicked
refresh_models_btn.click(
fn=fetch_models,
inputs=[base_url, api_key],
outputs=[model]
)
# Auto-refresh models when base URL changes
base_url.blur(
fn=fetch_models,
inputs=[base_url, api_key],
outputs=[model]
)
# Auto-refresh models when API key changes
api_key.blur(
fn=fetch_models,
inputs=[base_url, api_key],
outputs=[model]
)
# Allow Enter key to submit (when text input is focused)
text_input.submit(
fn=send_request,
inputs=[base_url, api_key, model, max_tokens, text_input, file_upload],
outputs=[status_output, response_output]
)
# Preload models when interface loads
interface.load(
fn=fetch_models,
inputs=[base_url, api_key],
outputs=[model]
)
return interface
if __name__ == "__main__":
# Create and launch the interface
demo = create_ui()
# Launch with custom settings
demo.launch(
server_name="127.0.0.1", # Use localhost instead of 0.0.0.0
server_port=7890, # Use different port to avoid conflicts
share=False, # Set to True to create public link
debug=True, # Disable debug mode to reduce console errors
show_error=True, # Show detailed error messages
inbrowser=True, # Auto-open in browser
prevent_thread_lock=False # Ensure proper threading
) |