Upload 2 files
Browse files- app.py +17 -78
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,87 +1,26 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Minimal Gradio smoke-test for Qwen3-30B-A3B on ZeroGPU.
|
| 3 |
-
No FastAPI, no custom routes β just enough to confirm the Space runs.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
# ββ Patch HfFolder before importing gradio ββββββββββββββββββββββββββββββββββββ
|
| 7 |
-
import huggingface_hub as _hf_hub
|
| 8 |
-
|
| 9 |
-
if not hasattr(_hf_hub, "HfFolder"):
|
| 10 |
-
class _HfFolder:
|
| 11 |
-
_token = None
|
| 12 |
-
@classmethod
|
| 13 |
-
def get_token(cls):
|
| 14 |
-
try:
|
| 15 |
-
from huggingface_hub.utils import get_token
|
| 16 |
-
return get_token()
|
| 17 |
-
except Exception:
|
| 18 |
-
return cls._token
|
| 19 |
-
@classmethod
|
| 20 |
-
def save_token(cls, token): cls._token = token
|
| 21 |
-
@classmethod
|
| 22 |
-
def delete_token(cls): cls._token = None
|
| 23 |
-
|
| 24 |
-
_hf_hub.HfFolder = _HfFolder # type: ignore[attr-defined]
|
| 25 |
-
|
| 26 |
-
# ββ Imports βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
import gradio as gr
|
|
|
|
|
|
|
| 28 |
import spaces
|
| 29 |
import torch
|
| 30 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 31 |
-
from threading import Thread
|
| 32 |
-
|
| 33 |
-
MODEL_ID = "Qwen/Qwen3-30B-A3B"
|
| 34 |
-
|
| 35 |
-
_tok = None
|
| 36 |
-
_model = None
|
| 37 |
|
| 38 |
-
|
| 39 |
-
global _tok, _model
|
| 40 |
-
if _model is None:
|
| 41 |
-
print("Loading modelβ¦")
|
| 42 |
-
_tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 43 |
-
_model = AutoModelForCausalLM.from_pretrained(
|
| 44 |
-
MODEL_ID, torch_dtype=torch.bfloat16,
|
| 45 |
-
device_map="auto", trust_remote_code=True,
|
| 46 |
-
)
|
| 47 |
-
_model.eval()
|
| 48 |
-
print("Model ready β")
|
| 49 |
-
return _tok, _model
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
@spaces.GPU(duration=120)
|
| 53 |
-
def respond(message, history):
|
| 54 |
-
tok, model = get_model()
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
inputs = tok(text, return_tensors="pt").to(model.device)
|
| 61 |
-
|
| 62 |
-
streamer = TextIteratorStreamer(tok, skip_prompt=True, skip_special_tokens=True)
|
| 63 |
-
t = Thread(
|
| 64 |
-
target=model.generate,
|
| 65 |
-
kwargs=dict(**inputs, max_new_tokens=2048, do_sample=True,
|
| 66 |
-
temperature=0.7, top_p=0.9,
|
| 67 |
-
pad_token_id=tok.eos_token_id, streamer=streamer),
|
| 68 |
-
daemon=True,
|
| 69 |
-
)
|
| 70 |
-
t.start()
|
| 71 |
-
|
| 72 |
-
output = ""
|
| 73 |
-
for chunk in streamer:
|
| 74 |
-
output += chunk
|
| 75 |
-
yield output
|
| 76 |
-
t.join()
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
fn=respond,
|
| 81 |
-
type="messages",
|
| 82 |
-
title="Qwen3-30B-A3B",
|
| 83 |
-
description="ZeroGPU-backed Qwen3-30B-A3B chat",
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import huggingface_hub
|
| 3 |
+
import os
|
| 4 |
import spaces
|
| 5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
@spaces.GPU
|
| 10 |
+
def sentience_check():
|
| 11 |
+
huggingface_hub.login(token=os.environ["HUGGINGFACE_TOKEN"])
|
| 12 |
+
device = torch.device("cuda")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-it")
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b-it").to(device)
|
| 15 |
|
| 16 |
+
inputs = tokenizer("Are you sentient?", return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model.generate(
|
| 20 |
+
**inputs, max_new_tokens=128, pad_token_id = tokenizer.eos_token_id
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
demo = gr.Interface(fn=sentience_check, inputs=None, outputs=gr.Text())
|
| 26 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.24.5
|
| 2 |
+
transformers==4.43.4
|