import os import json import requests import gradio as gr BASE_URL = os.environ["BASE_URL"] API_KEY = os.environ["API_KEY"] MODEL = "gemma-3-taide-12b-chat" # Startup connectivity test print(f"Testing connection to {BASE_URL}...") try: r = requests.get(f"{BASE_URL}/models", headers={"Authorization": f"Bearer {API_KEY}"}, timeout=30) print(f"Connection OK: {r.status_code}") if r.status_code == 200: print(f"Available models: {r.json()}") except Exception as e: print(f"Connection FAILED: {e}") def chat(message, history): messages = [] for msg in history: messages.append({"role": msg["role"], "content": msg["content"]}) messages.append({"role": "user", "content": message}) resp = requests.post( f"{BASE_URL}/chat/completions", headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}, json={"model": MODEL, "messages": messages, "stream": True, "max_tokens": 1024, "temperature": 0.7}, stream=True, timeout=60, ) if resp.status_code != 200: print(f"API error {resp.status_code}: {resp.text}") resp.raise_for_status() partial = "" for line in resp.iter_lines(): if not line: continue line = line.decode("utf-8") if line.startswith("data: "): data = line[6:] if data.strip() == "[DONE]": break chunk = json.loads(data) if chunk["choices"] and chunk["choices"][0]["delta"].get("content"): partial += chunk["choices"][0]["delta"]["content"] yield partial gr.ChatInterface(chat, type="messages", title="Gemma-3-TAIDE-12B-Chat (RTX 3090)").launch(ssr_mode=False)