import os from google import genai import gradio as gr from dotenv import load_dotenv # Load .env if running locally load_dotenv() # Initialize Gemini client # The API key is read from the environment variable GEMINI_API_KEY client = genai.Client() # Define a function to generate a blog def generate_blog(topic, tone, length): if not topic: return "Please enter a topic to generate the blog." prompt = f""" You are a professional content writer. Write a detailed blog about "{topic}". Tone: {tone}. Length: {length} words approximately. The blog should include: - An engaging introduction - Well-structured body paragraphs - A clear conclusion - Optional subheadings if needed """ try: # Generate content using Gemini 2.5 Flash response = client.models.generate_content( model="gemini-2.5-flash", contents=prompt ) return response.text except Exception as e: return f"Error generating blog: {e}" # Create a Gradio interface with gr.Blocks(title="AI Blog Generator ✍️") as demo: gr.Markdown("## ✨ AI Blog Generator using Google Gemini 2.5 Flash") gr.Markdown("Enter a topic and get a complete blog written by AI!") with gr.Row(): topic = gr.Textbox(label="Blog Topic", placeholder="e.g., The Future of Artificial Intelligence") with gr.Row(): tone = gr.Dropdown( ["Informative", "Inspirational", "Casual", "Professional", "Friendly", "Persuasive"], value="Informative", label="Writing Tone" ) length = gr.Slider(200, 2000, value=600, step=50, label="Approximate Length (words)") generate_button = gr.Button("Generate Blog 🪄") output = gr.Textbox(label="Generated Blog", lines=20) generate_button.click(fn=generate_blog, inputs=[topic, tone, length], outputs=output) # Launch the app if __name__ == "__main__": demo.launch()