pcasale commited on
Commit
d895e1d
·
verified ·
1 Parent(s): fe5874a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -23
app.py CHANGED
@@ -1,33 +1,45 @@
1
- # Import Gradio and the Hugging Face Transformers pipeline
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
- # Load a text-generation pipeline with the DistilGPT-2 model.
6
- # DistilGPT-2 is a distilled (compressed) version of GPT-2, so it's faster and lightweight:contentReference[oaicite:3]{index=3}.
7
- generator = pipeline("text-generation", model="distilgpt2") # This downloads the model weights if not already available
8
 
9
- # Define a function that uses the generator to produce text based on the input prompt.
10
  def generate_text(prompt):
11
- # Use the text-generation pipeline to continue the prompt.
12
- # We set a max_length to limit the output length for practicality.
13
- result = generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
14
  return result
15
 
16
- # Set up the Gradio interface:
17
- # - Input: a textbox for the prompt (single-line or a short prompt, so we use lines=2).
18
- # - Output: a textbox for the generated text.
19
- # - We also add a title and description for user guidance.
20
- input_prompt = gr.Textbox(lines=2, label="Prompt", placeholder="Enter a text prompt...")
21
- output_text = gr.Textbox(label="Generated Text")
22
 
23
- demo = gr.Interface(
24
- fn=generate_text,
25
- inputs=input_prompt,
26
- outputs=output_text,
27
- title="🤖 DistilGPT-2 Text Generator",
28
- description="**Description:** Enter a prompt, and the DistilGPT-2 language model will continue the text. "
29
- "This demonstrates basic text generation using a small pre-trained GPT-2 model."
30
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Launch the app (in a Hugging Face Space, this will run automatically).
33
  demo.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the DistilGPT-2 text-generation pipeline
5
+ generator = pipeline("text-generation", model="distilgpt2")
 
6
 
 
7
  def generate_text(prompt):
8
+ result = generator(prompt, max_length=120, num_return_sequences=1)[0]["generated_text"]
 
 
9
  return result
10
 
11
+ # Two simple example prompts for students to try
12
+ example_prompts = [
13
+ ["Once upon a time in a distant kingdom,"],
14
+ ["The future of artificial intelligence depends on"]
15
+ ]
 
16
 
17
+ with gr.Blocks(title="DistilGPT-2 Text Generator") as demo:
18
+
19
+ gr.Markdown(
20
+ """### DistilGPT-2 Text Generator
21
+ Enter a short prompt and the model will continue it.
22
+ Below the input box you will find two example prompts you can try immediately."""
23
+ )
24
+
25
+ prompt = gr.Textbox(
26
+ lines=2,
27
+ label="Prompt",
28
+ placeholder="Type a short prompt to begin…"
29
+ )
30
+
31
+ output = gr.Textbox(
32
+ lines=12, # Increased height for the output area
33
+ label="Generated Text"
34
+ )
35
+
36
+ btn = gr.Button("Generate")
37
+ btn.click(generate_text, inputs=prompt, outputs=output)
38
+
39
+ gr.Examples(
40
+ examples=example_prompts,
41
+ inputs=prompt,
42
+ label="Example Prompts"
43
+ )
44
 
 
45
  demo.launch()