Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
generator = pipeline("text-generation", model="distilbert/distilgpt2", device=-1)
|
| 5 |
+
|
| 6 |
+
def generate_text(prompt, temperature, max_tokens, repetition_penalty):
|
| 7 |
+
if not prompt.strip():
|
| 8 |
+
return "Please enter a prompt."
|
| 9 |
+
result = generator(
|
| 10 |
+
prompt,
|
| 11 |
+
max_new_tokens=int(max_tokens),
|
| 12 |
+
temperature=float(temperature),
|
| 13 |
+
repetition_penalty=float(repetition_penalty),
|
| 14 |
+
do_sample=True,
|
| 15 |
+
truncation=True,
|
| 16 |
+
)
|
| 17 |
+
return result[0]["generated_text"]
|
| 18 |
+
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=generate_text,
|
| 21 |
+
inputs=[
|
| 22 |
+
gr.Textbox(
|
| 23 |
+
label="Prompt",
|
| 24 |
+
placeholder="Start typing about birds...",
|
| 25 |
+
lines=3,
|
| 26 |
+
value="The northern cardinal sings",
|
| 27 |
+
),
|
| 28 |
+
gr.Slider(
|
| 29 |
+
minimum=0.1,
|
| 30 |
+
maximum=2.0,
|
| 31 |
+
value=1.0,
|
| 32 |
+
step=0.1,
|
| 33 |
+
label="Temperature",
|
| 34 |
+
info="Lower = more predictable, Higher = more creative",
|
| 35 |
+
),
|
| 36 |
+
gr.Slider(
|
| 37 |
+
minimum=20,
|
| 38 |
+
maximum=200,
|
| 39 |
+
value=80,
|
| 40 |
+
step=10,
|
| 41 |
+
label="Max New Tokens",
|
| 42 |
+
info="How much text to generate",
|
| 43 |
+
),
|
| 44 |
+
gr.Slider(
|
| 45 |
+
minimum=1.0,
|
| 46 |
+
maximum=2.0,
|
| 47 |
+
value=1.2,
|
| 48 |
+
step=0.1,
|
| 49 |
+
label="Repetition Penalty",
|
| 50 |
+
info="Higher = less repetition",
|
| 51 |
+
),
|
| 52 |
+
],
|
| 53 |
+
outputs=gr.Textbox(label="Generated Text", lines=8),
|
| 54 |
+
title="Bird Text Generator",
|
| 55 |
+
description=(
|
| 56 |
+
"A text generation playground using distilgpt2, customized with bird-themed prompts. "
|
| 57 |
+
"This is Riley's Space 1 — a baseline text generator. It can write about birds "
|
| 58 |
+
"but doesn't actually know anything about them. Try different temperature settings "
|
| 59 |
+
"to see how randomness affects the output."
|
| 60 |
+
),
|
| 61 |
+
examples=[
|
| 62 |
+
["The northern cardinal sings", 1.0, 80, 1.2],
|
| 63 |
+
["In the forest canopy, the warbler", 0.7, 100, 1.2],
|
| 64 |
+
["Migration patterns suggest that", 0.5, 120, 1.0],
|
| 65 |
+
["The red-tailed hawk circled above the parking lot", 1.0, 80, 1.2],
|
| 66 |
+
["At dawn, the chorus of birdsong", 1.3, 100, 1.2],
|
| 67 |
+
],
|
| 68 |
+
theme=gr.themes.Soft(),
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
demo.launch()
|