test01 / app.py
toast001's picture
Update app.py
29d0db9 verified
Raw History Blame Contribute Delete
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.
@spaces.GPU
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()