# from transformers import pipeline # generator = pipeline("text-generation", model="gpt2") # def generate_blog(topic): # return generator(f"Write a blog on: {topic}", max_length=200)[0]["generated_text"] # from transformers import pipeline # import gradio as gr # # Load the model # generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1") # # Define your function # def generate_text(prompt): # result = generator(prompt, max_length=100, num_return_sequences=1, do_sample=True, # temperature=0.7, # top_p=0.9,) # return result[0]["generated_text"] # # Create Gradio interface # iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Text Generator with GPT-2") # # ✅ This line is required to actually launch the app on Hugging Face Spaces # iface.launch() from transformers import pipeline import gradio as gr generator = pipeline("text-generation", model="sshleifer/tiny-gpt2") def clean_topic(topic): topic = topic.lower() if "write a blog on" in topic: topic = topic.replace("write a blog on", "").strip() elif "write a blog about" in topic: topic = topic.replace("write a blog about", "").strip() return topic.capitalize() # def generate_blog(topic): # topic = clean_topic(topic) # if not topic: # return "Please provide a topic." # prompt = f""" # Write a detailed and engaging blog post about "{topic}". # Include an introduction, 2–3 subheadings with paragraphs, and a conclusion. # Make it informative and conversational. # """ # result = generator(prompt, max_length=700, do_sample=True, temperature=0.7, top_p=0.9) # return result[0]['generated_text'] def generate_blog(topic): prompt = f""" Write a detailed and engaging blog post about "{topic}". Include an introduction, 2–3 subheadings with paragraphs, and a conclusion. Make it informative and conversational. """ result = generator(prompt, max_length=700, do_sample=True, temperature=0.7, top_p=0.9) return result[0]['generated_text'] gr.Interface(fn=generate_blog, inputs="text", outputs="text", title="AI Blog Writer").launch()