Spaces:
Sleeping
Sleeping
Download app.py from Yeshita/Text-Generator-Using-GPT-2: direct link, hf CLI and curl.
- Browser
- Download file 803 Bytes
-
https://huggingface.co/spaces/Yeshita/Text-Generator-Using-GPT-2/resolve/main/app.py
- Command line
-
hf download hf://spaces/Yeshita/Text-Generator-Using-GPT-2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Yeshita/Text-Generator-Using-GPT-2/resolve/main/app.py
803 Bytes
| # Install required libraries before running: | |
| # pip install gradio transformers | |
| import gradio as gr | |
| from transformers import pipeline | |
| # Load a pre-trained generative model from Hugging Face (GPT-2 for text generation) | |
| generator = pipeline("text-generation", model="gpt2") | |
| # Define function for text generation | |
| def generate_text(prompt): | |
| result = generator(prompt, max_length=100, num_return_sequences=1) | |
| return result[0]['generated_text'] | |
| # Create a Gradio interface | |
| interface = gr.Interface( | |
| fn=generate_text, | |
| inputs=gr.Textbox(lines=10, placeholder="Enter a prompt here..."), | |
| outputs="text", | |
| title="Text Generator App", | |
| description="Generate creative text using Hugging Face GPT-2 model." | |
| ) | |
| # Launch the app (runs locally or in Hugging Face Spaces) | |
| interface.launch() | |