Spaces:
Sleeping
Sleeping
add initial implementation of T5 Mini Reply model with Gradio interface and requirements
Browse files- app.py +41 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 3 |
+
|
| 4 |
+
# Modelo leve que roda em CPU (bom p/ demo). Se preferir PT “de verdade”, troque por:
|
| 5 |
+
# MODEL_ID = "unicamp-dl/ptt5-small-portuguese-vocab"
|
| 6 |
+
MODEL_ID = "google/flan-t5-small"
|
| 7 |
+
|
| 8 |
+
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 9 |
+
mdl = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
| 10 |
+
|
| 11 |
+
pipe = pipeline(
|
| 12 |
+
"text2text-generation",
|
| 13 |
+
model=mdl,
|
| 14 |
+
tokenizer=tok
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def gen(prompt: str):
|
| 18 |
+
if not prompt or not prompt.strip():
|
| 19 |
+
return ""
|
| 20 |
+
out = pipe(
|
| 21 |
+
prompt,
|
| 22 |
+
max_new_tokens=120,
|
| 23 |
+
do_sample=True,
|
| 24 |
+
top_p=0.9,
|
| 25 |
+
temperature=0.7,
|
| 26 |
+
repetition_penalty=1.15,
|
| 27 |
+
num_return_sequences=1,
|
| 28 |
+
)
|
| 29 |
+
return out[0]["generated_text"]
|
| 30 |
+
|
| 31 |
+
# Gradio já expõe /api/predict automaticamente
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=gen,
|
| 34 |
+
inputs=gr.Textbox(label="Prompt"),
|
| 35 |
+
outputs=gr.Textbox(label="Saída"),
|
| 36 |
+
title="T5 Mini Reply",
|
| 37 |
+
description="Geração de respostas curtas (CPU)."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.43.3
|
| 2 |
+
sentencepiece==0.2.0
|
| 3 |
+
accelerate==0.33.0
|