import gradio as gr from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel # Pre-download the model and tokenizer model_name = "gpt2" GPT2Tokenizer.from_pretrained(model_name) GPT2LMHeadModel.from_pretrained(model_name) # Load the pipeline generator = pipeline('text-generation', model=model_name) # Function to generate text def generate_text(prompt): output = generator(prompt, max_length=50, num_return_sequences=1) return output[0]['generated_text'] # Build the interface iface = gr.Interface( fn=generate_text, inputs=gr.Textbox(lines=2, placeholder="Enter a prompt here..."), outputs="text", title="AI Text Generator", description="Generate text with AI!" ) # Launch it kk iface.launch()