Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from google import genai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load .env if running locally
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Initialize Gemini client
|
| 10 |
+
# The API key is read from the environment variable GEMINI_API_KEY
|
| 11 |
+
client = genai.Client()
|
| 12 |
+
|
| 13 |
+
# Define a function to generate a blog
|
| 14 |
+
def generate_blog(topic, tone, length):
|
| 15 |
+
if not topic:
|
| 16 |
+
return "Please enter a topic to generate the blog."
|
| 17 |
+
|
| 18 |
+
prompt = f"""
|
| 19 |
+
You are a professional content writer. Write a detailed blog about "{topic}".
|
| 20 |
+
Tone: {tone}.
|
| 21 |
+
Length: {length} words approximately.
|
| 22 |
+
The blog should include:
|
| 23 |
+
- An engaging introduction
|
| 24 |
+
- Well-structured body paragraphs
|
| 25 |
+
- A clear conclusion
|
| 26 |
+
- Optional subheadings if needed
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Generate content using Gemini 2.5 Flash
|
| 31 |
+
response = client.models.generate_content(
|
| 32 |
+
model="gemini-2.5-flash",
|
| 33 |
+
contents=prompt
|
| 34 |
+
)
|
| 35 |
+
return response.text
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error generating blog: {e}"
|
| 38 |
+
|
| 39 |
+
# Create a Gradio interface
|
| 40 |
+
with gr.Blocks(title="AI Blog Generator ✍️") as demo:
|
| 41 |
+
gr.Markdown("## ✨ AI Blog Generator using Google Gemini 2.5 Flash")
|
| 42 |
+
gr.Markdown("Enter a topic and get a complete blog written by AI!")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
topic = gr.Textbox(label="Blog Topic", placeholder="e.g., The Future of Artificial Intelligence")
|
| 46 |
+
with gr.Row():
|
| 47 |
+
tone = gr.Dropdown(
|
| 48 |
+
["Informative", "Inspirational", "Casual", "Professional", "Friendly", "Persuasive"],
|
| 49 |
+
value="Informative",
|
| 50 |
+
label="Writing Tone"
|
| 51 |
+
)
|
| 52 |
+
length = gr.Slider(200, 2000, value=600, step=50, label="Approximate Length (words)")
|
| 53 |
+
|
| 54 |
+
generate_button = gr.Button("Generate Blog 🪄")
|
| 55 |
+
output = gr.Textbox(label="Generated Blog", lines=20)
|
| 56 |
+
|
| 57 |
+
generate_button.click(fn=generate_blog, inputs=[topic, tone, length], outputs=output)
|
| 58 |
+
|
| 59 |
+
# Launch the app
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|