Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,51 @@
|
|
| 1 |
# ---------------------------------------------
|
| 2 |
-
#
|
| 3 |
# ---------------------------------------------
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
# 2. Set your API key in the environment:
|
| 8 |
-
# export GEMINI_API_KEY="your_api_key_here"
|
| 9 |
-
#
|
| 10 |
-
# 3. Run this script:
|
| 11 |
-
# python gemini_blog_demo.py
|
| 12 |
# ---------------------------------------------
|
| 13 |
|
| 14 |
import os
|
|
|
|
| 15 |
from google import genai
|
|
|
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
| 18 |
client = genai.Client()
|
| 19 |
|
| 20 |
-
# Choose a model (check your account for available ones)
|
| 21 |
MODEL = "gemini-2.5-flash"
|
| 22 |
|
| 23 |
-
# Your prompt
|
| 24 |
-
prompt = "Write a short blog paragraph explaining how AI works in simple words."
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
print(response.text)
|
|
|
|
| 1 |
# ---------------------------------------------
|
| 2 |
+
# AI Blog Generator using Google Gemini API
|
| 3 |
# ---------------------------------------------
|
| 4 |
+
# Run locally: python app.py
|
| 5 |
+
# Deploy on Hugging Face Spaces (Gradio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# ---------------------------------------------
|
| 7 |
|
| 8 |
import os
|
| 9 |
+
import gradio as gr
|
| 10 |
from google import genai
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
+
# Load environment variables (optional if using .env)
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Initialize Gemini client
|
| 17 |
client = genai.Client()
|
| 18 |
|
|
|
|
| 19 |
MODEL = "gemini-2.5-flash"
|
| 20 |
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def generate_blog(topic):
|
| 23 |
+
"""Generate a blog paragraph using Gemini API."""
|
| 24 |
+
if not topic.strip():
|
| 25 |
+
return "⚠️ Please enter a topic first."
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
prompt = f"Write a short, engaging blog paragraph about: {topic}"
|
| 29 |
+
response = client.models.generate_content(model=MODEL, contents=prompt)
|
| 30 |
+
return response.text.strip()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"❌ Error: {e}"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Gradio Interface
|
| 36 |
+
title = "🧠 AI Blog Generator using Google Gemini"
|
| 37 |
+
description = """
|
| 38 |
+
Type any topic below and generate a short blog paragraph powered by **Google Gemini API**.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=generate_blog,
|
| 43 |
+
inputs=gr.Textbox(label="Enter your blog topic", placeholder="e.g., How AI is changing healthcare"),
|
| 44 |
+
outputs=gr.Textbox(label="Generated Blog Content", lines=10),
|
| 45 |
+
title=title,
|
| 46 |
+
description=description,
|
| 47 |
+
theme="default",
|
| 48 |
)
|
| 49 |
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|
|
|