Spaces:
Sleeping
Sleeping
File size: 1,738 Bytes
39d439e be5000c 39d439e 72e1cd3 be5000c 39d439e 72e1cd3 39d439e a99207a 9d10212 92d3397 be5000c 9d10212 39d439e a99207a 72e1cd3 39d439e a99207a 39d439e a99207a 72e1cd3 | 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 | 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)
|