Commit ·
354e4bc
0
Parent(s):
Add Tiny Aya ZeroGPU sidecar
Browse files- README.md +19 -0
- app.py +74 -0
- requirements.txt +8 -0
README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Tiny Army Tiny Aya ZeroGPU
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.15.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
python_version: "3.11"
|
| 10 |
+
suggested_hardware: zero-a10g
|
| 11 |
+
pinned: false
|
| 12 |
+
license: apache-2.0
|
| 13 |
+
models:
|
| 14 |
+
- CohereLabs/tiny-aya-global
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Tiny Army Tiny Aya ZeroGPU
|
| 18 |
+
|
| 19 |
+
ZeroGPU sidecar for Tiny Army text generation using Cohere Labs Tiny Aya Global.
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import threading
|
| 5 |
+
|
| 6 |
+
os.environ.setdefault("OPENBLAS_NUM_THREADS", "4")
|
| 7 |
+
os.environ.setdefault("OMP_NUM_THREADS", "4")
|
| 8 |
+
os.environ.setdefault("MKL_NUM_THREADS", "4")
|
| 9 |
+
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
| 10 |
+
os.environ.setdefault("GRADIO_SSR_MODE", "false")
|
| 11 |
+
|
| 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"))
|
| 19 |
+
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype="auto")
|
| 22 |
+
if torch.cuda.is_available():
|
| 23 |
+
model.to("cuda")
|
| 24 |
+
model.eval()
|
| 25 |
+
|
| 26 |
+
_lock = threading.Lock()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _messages(system: str, user: str):
|
| 30 |
+
messages = []
|
| 31 |
+
if system and system.strip():
|
| 32 |
+
messages.append({"role": "system", "content": system.strip()})
|
| 33 |
+
messages.append({"role": "user", "content": (user or "").strip()})
|
| 34 |
+
return messages
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@spaces.GPU(duration=120)
|
| 38 |
+
def generate(system: str, user: str, max_tokens: int = DEFAULT_MAX_TOKENS, temperature: float = 0.8):
|
| 39 |
+
if not user or not user.strip():
|
| 40 |
+
raise gr.Error("user prompt required")
|
| 41 |
+
max_tokens = max(1, min(int(max_tokens or DEFAULT_MAX_TOKENS), 1024))
|
| 42 |
+
temperature = max(0.0, min(float(temperature if temperature is not None else 0.8), 2.0))
|
| 43 |
+
inputs = tokenizer.apply_chat_template(
|
| 44 |
+
_messages(system, user),
|
| 45 |
+
tokenize=True,
|
| 46 |
+
add_generation_prompt=True,
|
| 47 |
+
return_dict=True,
|
| 48 |
+
return_tensors="pt",
|
| 49 |
+
).to(model.device)
|
| 50 |
+
with _lock, torch.inference_mode():
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
**inputs,
|
| 53 |
+
max_new_tokens=max_tokens,
|
| 54 |
+
do_sample=temperature > 0,
|
| 55 |
+
temperature=max(temperature, 1e-5),
|
| 56 |
+
top_p=0.95,
|
| 57 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 58 |
+
)
|
| 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)
|
| 65 |
+
user = gr.Textbox(label="User", lines=5)
|
| 66 |
+
max_tokens = gr.Slider(1, 1024, value=DEFAULT_MAX_TOKENS, step=1, label="Max new tokens")
|
| 67 |
+
temperature = gr.Slider(0, 2, value=0.8, step=0.05, label="Temperature")
|
| 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__":
|
| 74 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==6.15.2
|
| 2 |
+
spaces
|
| 3 |
+
torch
|
| 4 |
+
transformers>=5.4.0
|
| 5 |
+
accelerate
|
| 6 |
+
sentencepiece
|
| 7 |
+
protobuf
|
| 8 |
+
huggingface_hub
|