import gradio as gr import torch from transformers import pipeline # Load the GPT-2 model device = 0 if torch.cuda.is_available() else -1 generator = pipeline("text-generation", model="gpt2", device=device) # Function to generate blog def generate_blog(topic): prompt = f"Write a detailed blog on the topic: {topic}\n\n" result = generator(prompt, max_length=250, do_sample=True, temperature=0.7) return result[0]["generated_text"] # Gradio Interface iface = gr.Interface( fn=generate_blog, inputs=gr.Textbox(label="Enter Blog Topic"), outputs=gr.Textbox(label="Generated Blog", lines=30), title="GPT-2 Blog Generator", description="Generate a short blog using GPT-2 pretrained model" ) iface.launch()