Download app.py from toast001/test01: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/spaces/toast001/test01/resolve/main/app.py
- Command line
-
hf download hf://spaces/toast001/test01/app.py
-
curl -L -o app.py https://huggingface.co/spaces/toast001/test01/resolve/main/app.py
1.67 kB
| import os | |
| import gradio as gr | |
| import spaces | |
| from openai import OpenAI | |
| # Hugging Face's ZeroGPU runtime requires at least one top-level @spaces.GPU | |
| # function at startup. Inference itself runs on the school's vLLM server, so | |
| # this compatibility function is intentionally never called. | |
| def _zerogpu_startup_check(): | |
| return None | |
| client = OpenAI( | |
| base_url=os.environ["BASE_URL"].rstrip("/"), | |
| api_key=os.environ["API_KEY"], | |
| ) | |
| MODEL = os.getenv("MODEL", "gemma-3-taide-12b-chat") | |
| def _text_from_content(content): | |
| if isinstance(content, str): | |
| return content | |
| if isinstance(content, list): | |
| return "".join( | |
| item.get("text", "") | |
| for item in content | |
| if isinstance(item, dict) and item.get("type") == "text" | |
| ) | |
| return "" | |
| def chat(message, history): | |
| messages = [] | |
| for item in history: | |
| role = item.get("role") | |
| content = _text_from_content(item.get("content")) | |
| if role in ("user", "assistant") and content: | |
| messages.append({"role": role, "content": content}) | |
| messages.append({"role": "user", "content": message}) | |
| stream = client.chat.completions.create( | |
| model=MODEL, | |
| messages=messages, | |
| stream=True, | |
| max_tokens=2048, | |
| temperature=0.7, | |
| ) | |
| reply = "" | |
| for chunk in stream: | |
| if chunk.choices and chunk.choices[0].delta.content: | |
| reply += chunk.choices[0].delta.content | |
| yield reply | |
| demo = gr.ChatInterface( | |
| chat, | |
| title="Gemma 3-taide-12b Chat on RTX 3090 server", | |
| description="Model run on vLLM of school's server.", | |
| ) | |
| demo.launch() |