Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
MODEL_ID = "google/flan-t5-small"
|
| 5 |
pipe = pipeline("text2text-generation", model=MODEL_ID)
|
| 6 |
|
| 7 |
-
def infer(prompt, max_new_tokens=
|
| 8 |
if not prompt.strip():
|
| 9 |
return "Please type something to generate."
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return out[0]["generated_text"]
|
| 12 |
|
| 13 |
-
demo = gr.Interface(
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
MODEL_ID = "google/flan-t5-small" # swap to flan-t5-base if you have more CPU
|
| 5 |
pipe = pipeline("text2text-generation", model=MODEL_ID)
|
| 6 |
|
| 7 |
+
def infer(prompt, max_new_tokens=96, num_beams=4, length_penalty=0.9):
|
| 8 |
if not prompt.strip():
|
| 9 |
return "Please type something to generate."
|
| 10 |
+
# Beam search is deterministic and tends to avoid loops on T5.
|
| 11 |
+
out = pipe(
|
| 12 |
+
prompt,
|
| 13 |
+
max_new_tokens=int(max_new_tokens),
|
| 14 |
+
num_beams=int(num_beams),
|
| 15 |
+
early_stopping=True,
|
| 16 |
+
length_penalty=float(length_penalty), # <1.0 favors shorter, reduces rambling
|
| 17 |
+
no_repeat_ngram_size=3 # prevents 3-gram loops like "blue sky with"
|
| 18 |
+
)
|
| 19 |
return out[0]["generated_text"]
|
| 20 |
|
| 21 |
+
demo = gr.Interface(
|
| 22 |
+
fn=infer,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.Textbox(lines=3, label="Instruction",
|
| 25 |
+
placeholder="Explain in one paragraph: Why is the sky blue?"),
|
| 26 |
+
gr.Slider(32, 192, 96, step=8, label="Max new tokens"),
|
| 27 |
+
gr.Slider(2, 8, 4, step=1, label="Num beams"),
|
| 28 |
+
gr.Slider(0.6, 1.3, 0.9, step=0.05, label="Length penalty")
|
| 29 |
+
],
|
| 30 |
+
outputs=gr.Textbox(lines=10, label="Output"),
|
| 31 |
+
title="FLAN-T5 (Deterministic) — Mini LLM"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|