Stream Tiny Aya text generation
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ os.environ.setdefault("GRADIO_SSR_MODE", "false")
|
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
| 14 |
import torch
|
| 15 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 16 |
|
| 17 |
MODEL_ID = os.environ.get("TINY_AYA_MODEL", "CohereLabs/tiny-aya-global")
|
| 18 |
DEFAULT_MAX_TOKENS = int(os.environ.get("TINY_AYA_MAX_TOKENS", "400"))
|
|
@@ -59,6 +59,42 @@ def generate(system: str, user: str, max_tokens: int = DEFAULT_MAX_TOKENS, tempe
|
|
| 59 |
return tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True).strip()
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with gr.Blocks(title="Tiny Army Tiny Aya ZeroGPU") as demo:
|
| 63 |
gr.Markdown("# Tiny Army Tiny Aya ZeroGPU")
|
| 64 |
system = gr.Textbox(label="System", lines=5)
|
|
@@ -68,6 +104,7 @@ with gr.Blocks(title="Tiny Army Tiny Aya ZeroGPU") as demo:
|
|
| 68 |
btn = gr.Button("Generate")
|
| 69 |
out = gr.Textbox(label="Output", lines=10)
|
| 70 |
btn.click(generate, inputs=[system, user, max_tokens, temperature], outputs=out, api_name="generate")
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
|
|
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
| 14 |
import torch
|
| 15 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 16 |
|
| 17 |
MODEL_ID = os.environ.get("TINY_AYA_MODEL", "CohereLabs/tiny-aya-global")
|
| 18 |
DEFAULT_MAX_TOKENS = int(os.environ.get("TINY_AYA_MAX_TOKENS", "400"))
|
|
|
|
| 59 |
return tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True).strip()
|
| 60 |
|
| 61 |
|
| 62 |
+
@spaces.GPU(duration=120)
|
| 63 |
+
def generate_stream(system: str, user: str, max_tokens: int = DEFAULT_MAX_TOKENS, temperature: float = 0.8):
|
| 64 |
+
if not user or not user.strip():
|
| 65 |
+
raise gr.Error("user prompt required")
|
| 66 |
+
max_tokens = max(1, min(int(max_tokens or DEFAULT_MAX_TOKENS), 1024))
|
| 67 |
+
temperature = max(0.0, min(float(temperature if temperature is not None else 0.8), 2.0))
|
| 68 |
+
inputs = tokenizer.apply_chat_template(
|
| 69 |
+
_messages(system, user),
|
| 70 |
+
tokenize=True,
|
| 71 |
+
add_generation_prompt=True,
|
| 72 |
+
return_dict=True,
|
| 73 |
+
return_tensors="pt",
|
| 74 |
+
).to(model.device)
|
| 75 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 76 |
+
|
| 77 |
+
def run():
|
| 78 |
+
with _lock, torch.inference_mode():
|
| 79 |
+
model.generate(
|
| 80 |
+
**inputs,
|
| 81 |
+
max_new_tokens=max_tokens,
|
| 82 |
+
do_sample=temperature > 0,
|
| 83 |
+
temperature=max(temperature, 1e-5),
|
| 84 |
+
top_p=0.95,
|
| 85 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 86 |
+
streamer=streamer,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
thread = threading.Thread(target=run, daemon=True)
|
| 90 |
+
thread.start()
|
| 91 |
+
acc = ""
|
| 92 |
+
for token in streamer:
|
| 93 |
+
acc += token
|
| 94 |
+
yield acc
|
| 95 |
+
thread.join(timeout=1)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
with gr.Blocks(title="Tiny Army Tiny Aya ZeroGPU") as demo:
|
| 99 |
gr.Markdown("# Tiny Army Tiny Aya ZeroGPU")
|
| 100 |
system = gr.Textbox(label="System", lines=5)
|
|
|
|
| 104 |
btn = gr.Button("Generate")
|
| 105 |
out = gr.Textbox(label="Output", lines=10)
|
| 106 |
btn.click(generate, inputs=[system, user, max_tokens, temperature], outputs=out, api_name="generate")
|
| 107 |
+
btn.click(generate_stream, inputs=[system, user, max_tokens, temperature], outputs=out, api_name="generate_stream")
|
| 108 |
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|