import gradio as gr from transformers import pipeline # Load the GPT-2 model from Hugging Face using the pipeline generator = pipeline('text-generation', model='gpt2') def generate_text(prompt, max_length=50): # Generate text using the GPT-2 model generated_text = generator(prompt, max_length=max_length, num_return_sequences=1)[0]['generated_text'] return generated_text # Create a Gradio interface iface = gr.Interface( fn=generate_text, inputs=[ gr.Textbox(lines=3, placeholder="Enter your prompt here...", label="Prompt"), gr.Slider(minimum=10, maximum=100, value=50, label="Max Length"), ], outputs=gr.Textbox(label="Generated Text"), title="GPT-2 Text Generator", description="Enter a prompt and generate text using GPT-2 from Hugging Face." ) # Launch the Gradio interface iface.launch(share=True)