Spaces:
Sleeping
Sleeping
Download app.py from htchu/tiny-gemma3-play: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/htchu/tiny-gemma3-play/resolve/main/app.py
- Command line
-
hf download hf://spaces/htchu/tiny-gemma3-play/app.py
-
curl -L -o app.py https://huggingface.co/spaces/htchu/tiny-gemma3-play/resolve/main/app.py
1.74 kB
| 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) | |