Download app.py from Imthiyas/dd: direct link, hf CLI and curl.
- Browser
- Download file 964 Bytes
-
https://huggingface.co/spaces/Imthiyas/dd/resolve/main/app.py
- Command line
-
hf download hf://spaces/Imthiyas/dd/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Imthiyas/dd/resolve/main/app.py
964 Bytes
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| # Load tokenizer and model | |
| tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") | |
| model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") | |
| # Function to generate text | |
| def generate_text(prompt, max_length=100): | |
| inputs = tokenizer(prompt, return_tensors="pt") | |
| outputs = model.generate(**inputs, max_length=max_length, do_sample=True, temperature=0.7) | |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=generate_text, | |
| inputs=[ | |
| gr.Textbox(label="Prompt", placeholder="Enter your text here..."), | |
| gr.Slider(10, 200, value=100, step=10, label="Max Length") | |
| ], | |
| outputs="text", | |
| title="GPT-2 Text Generator", | |
| description="This app uses the `openai-community/gpt2` model to generate text." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() |