Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,58 @@
|
|
| 1 |
# app.py
|
| 2 |
import re, spaces, gradio as gr, torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
|
| 5 |
-
MODEL_NAME = "loocorez/reverse-text-warmup"
|
| 6 |
-
|
| 7 |
-
SYSTEM_PROMPT = """Reverse the given text.
|
| 8 |
-
|
| 9 |
-
Respond in the following format:
|
| 10 |
-
<response>
|
| 11 |
-
<think>...</think>
|
| 12 |
-
<answer>...</answer>
|
| 13 |
-
</response>"""
|
| 14 |
-
|
| 15 |
-
# ββ load model & tokenizer once ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 17 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 18 |
-
|
| 19 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
-
MODEL_NAME,
|
| 21 |
-
torch_dtype=torch.float16
|
| 22 |
-
).eval() # keep weights on CPU until each call
|
| 23 |
-
|
| 24 |
-
EOS_ID = tokenizer.encode("</response>", add_special_tokens=False)[0]
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def reverse_text(
|
| 29 |
-
user_text: str,
|
| 30 |
-
temperature: float = 0.0,
|
| 31 |
-
max_tokens: int = 256,
|
| 32 |
-
) -> str:
|
| 33 |
-
prompt = f"{SYSTEM_PROMPT}\n\n{user_text.strip()}"
|
| 34 |
-
|
| 35 |
-
# ββ move model to GPU for this request ββββββββββββββββββββββββββββββββββββ
|
| 36 |
-
model.to("cuda")
|
| 37 |
-
|
| 38 |
-
try:
|
| 39 |
-
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 40 |
-
|
| 41 |
-
with torch.no_grad():
|
| 42 |
-
out = model.generate(
|
| 43 |
-
**inputs,
|
| 44 |
-
max_new_tokens=max_tokens,
|
| 45 |
-
do_sample=temperature > 0,
|
| 46 |
-
temperature=max(temperature, 1e-6),
|
| 47 |
-
eos_token_id=EOS_ID,
|
| 48 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 49 |
-
repetition_penalty=1.1,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
gen = tokenizer.decode(out[0][inputs["input_ids"].size(1):],
|
| 53 |
-
skip_special_tokens=True)
|
| 54 |
-
m = re.search(r"<answer>(.*?)</answer>", gen, re.S)
|
| 55 |
-
return m.group(1).strip() if m else gen.strip()
|
| 56 |
-
|
| 57 |
-
finally:
|
| 58 |
-
# ββ free the GPU so the next visitor can use it βββββββββββββββββββββββ
|
| 59 |
-
model.to("cpu")
|
| 60 |
-
torch.cuda.empty_cache()
|
| 61 |
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
gr.Slider(32, 512, step=32, value=256, label="Max new tokens"),
|
| 69 |
-
],
|
| 70 |
-
outputs=gr.Textbox(label="Reversed Text", lines=3),
|
| 71 |
-
title="π Reverse Text Model Demo",
|
| 72 |
-
description="Give it a sentence; it returns the reversed version.",
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
import re, spaces, gradio as gr, torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import verifiers as vf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
+
MODEL_NAME = "loocorez/reverse-text-warmup"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
+
# ---- prompt helpers --------------------------------------------------------
|
| 11 |
+
parser = vf.XMLParser(["think", "answer"]) # <think> β¦ </think>\n<answer> β¦ </answer>
|
| 12 |
+
SYSTEM_MSG = (
|
| 13 |
+
"You are a helpful assistant that reverses the user's text. "
|
| 14 |
+
f"Respond in exactly this format: {parser.get_format_str()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
+
def build_prompt(user_msg: str, tok) -> str:
|
| 18 |
+
"""Use the modelβs native chat template so all special tokens are right."""
|
| 19 |
+
return tok.apply_chat_template(
|
| 20 |
+
[{"role": "system", "content": SYSTEM_MSG},
|
| 21 |
+
{"role": "user", "content": user_msg}],
|
| 22 |
+
tokenize=False,
|
| 23 |
+
add_generation_prompt=True
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# ---- lazy-load model the first time a GPU is granted -----------------------
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)
|
| 28 |
+
model = None # brought into scope & moved to CUDA inside @spaces.GPU
|
| 29 |
+
|
| 30 |
+
@spaces.GPU(duration=60) # β the βproper annotationβ for ZeroGPU
|
| 31 |
+
def reverse(user_msg: str) -> str:
|
| 32 |
+
global model
|
| 33 |
+
if model is None: # cold-start: happens on the first request
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
+
MODEL_ID,
|
| 36 |
+
torch_dtype=torch.float16,
|
| 37 |
+
device_map="auto"
|
| 38 |
+
)
|
| 39 |
+
prompt = build_prompt(user_msg, tokenizer)
|
| 40 |
+
with torch.inference_mode():
|
| 41 |
+
out = model.generate(
|
| 42 |
+
tokenizer(prompt, return_tensors="pt").to(model.device),
|
| 43 |
+
max_new_tokens=64,
|
| 44 |
+
do_sample=False,
|
| 45 |
+
temperature=0.0
|
| 46 |
+
)
|
| 47 |
+
full = tokenizer.decode(out[0], skip_special_tokens=True)
|
| 48 |
+
return full[len(prompt):] # strip the prompt β return only the reply
|
| 49 |
+
|
| 50 |
+
# ---- Gradio UI -------------------------------------------------------------
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("### Reverse-Text demo (ZeroGPU)")
|
| 53 |
+
txt_in = gr.Textbox(label="Input")
|
| 54 |
+
txt_out = gr.Textbox(label="Model reply")
|
| 55 |
+
btn = gr.Button("Run")
|
| 56 |
+
btn.click(reverse, txt_in, txt_out)
|
| 57 |
+
|
| 58 |
+
demo.queue().launch()
|