Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load a small text generation model (fast on free CPU)
|
| 5 |
+
print("Loading distilgpt2...")
|
| 6 |
+
generator = pipeline("text-generation", model="distilgpt2")
|
| 7 |
+
print("Model loaded!")
|
| 8 |
+
|
| 9 |
+
def generate_text(prompt, temperature, top_p, max_length):
|
| 10 |
+
if not prompt or not prompt.strip():
|
| 11 |
+
return "Type a prompt above first!"
|
| 12 |
+
|
| 13 |
+
result = generator(
|
| 14 |
+
prompt,
|
| 15 |
+
temperature=max(temperature, 0.01), # avoid division by zero
|
| 16 |
+
top_p=top_p,
|
| 17 |
+
max_length=int(max_length),
|
| 18 |
+
do_sample=True,
|
| 19 |
+
num_return_sequences=1,
|
| 20 |
+
)
|
| 21 |
+
return result[0]["generated_text"]
|
| 22 |
+
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=generate_text,
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.Textbox(
|
| 27 |
+
lines=3,
|
| 28 |
+
placeholder="Start your text here...",
|
| 29 |
+
label="Prompt",
|
| 30 |
+
),
|
| 31 |
+
gr.Slider(
|
| 32 |
+
minimum=0.1,
|
| 33 |
+
maximum=2.0,
|
| 34 |
+
value=0.7,
|
| 35 |
+
step=0.1,
|
| 36 |
+
label="Temperature (creativity)",
|
| 37 |
+
),
|
| 38 |
+
gr.Slider(
|
| 39 |
+
minimum=0.1,
|
| 40 |
+
maximum=1.0,
|
| 41 |
+
value=0.9,
|
| 42 |
+
step=0.05,
|
| 43 |
+
label="Top-p (diversity)",
|
| 44 |
+
),
|
| 45 |
+
gr.Slider(
|
| 46 |
+
minimum=20,
|
| 47 |
+
maximum=200,
|
| 48 |
+
value=100,
|
| 49 |
+
step=10,
|
| 50 |
+
label="Max Length (words-ish)",
|
| 51 |
+
),
|
| 52 |
+
],
|
| 53 |
+
outputs=gr.Textbox(label="Generated Text", lines=10),
|
| 54 |
+
title="Text Playground",
|
| 55 |
+
description="Type a prompt and use the sliders to control how the AI writes. Temperature controls creativity (low = predictable, high = wild). Top-p controls word diversity. Max length controls how much it writes.",
|
| 56 |
+
examples=[
|
| 57 |
+
["Once upon a time in a school where robots", 0.7, 0.9, 100],
|
| 58 |
+
["The secret ingredient in the recipe was", 1.2, 0.9, 80],
|
| 59 |
+
["Dear Principal, I am writing to request", 0.3, 0.9, 100],
|
| 60 |
+
["Breaking news: scientists discover that cats", 0.9, 0.95, 120],
|
| 61 |
+
["The haunted house at the end of the street", 1.5, 0.8, 150],
|
| 62 |
+
],
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
demo.launch()
|