Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 6 |
-
|
| 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 |
-
|
| 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 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
output_text = gr.Textbox(label="Generated Text")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 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()
|