File size: 1,879 Bytes
effb5d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from transformers import pipeline
import gradio as gr

# Load distilgpt2 β€” a small text generation model (82M parameters)
print("Loading text generation model (distilgpt2)...")
generator = pipeline(
    "text-generation",
    model="distilbert/distilgpt2",
)
print("Model loaded!")


def generate_text(prompt):
    """Generate a continuation of the input text."""
    if not prompt or not prompt.strip():
        return "Type a sentence or two and watch the model try to continue it."

    # Generate with default settings β€” no temperature control yet
    # (that's Session 5!)
    result = generator(
        prompt,
        max_new_tokens=80,
        num_return_sequences=1,
        do_sample=True,
        truncation=True,
    )

    return result[0]["generated_text"]


demo = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(
        lines=4,
        placeholder="Type a sentence or the beginning of a story...",
        label="Your Prompt",
    ),
    outputs=gr.Textbox(
        label="What the Model Wrote",
        lines=8,
    ),
    title="Text Generator",
    description=(
        "This model doesn't classify β€” it creates. "
        "Type a sentence and watch it try to write what comes next. "
        "It's a small model (82M parameters), so the results won't be "
        "perfect β€” but it's doing something fundamentally different from "
        "the classification models we've used so far."
    ),
    examples=[
        ["Monday morning arrived like a gift from the universe β€” truly, what better way to start the week than"],
        ["The acceptance letter sat on the kitchen table, and she couldn't stop reading it."],
        ["The volcano had been dormant for three hundred years. When it finally erupted,"],
        ["Once upon a time, in a city made entirely of glass,"],
        ["The capital of France is"],
    ],
)

demo.launch()