Spaces:
Runtime error
Runtime error
File size: 1,967 Bytes
45fffd8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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()
|