Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import json
|
| 4 |
-
import tempfile
|
| 5 |
-
|
| 6 |
-
def generate_titles(api_key, blog_title):
|
| 7 |
-
import subprocess
|
| 8 |
-
import json
|
| 9 |
-
import tempfile
|
| 10 |
|
|
|
|
|
|
|
| 11 |
if not api_key or not blog_title:
|
| 12 |
-
return "Please provide both API key and blog title."
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
"
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
|
|
|
|
| 22 |
These should:
|
| 23 |
-
-
|
| 24 |
-
- Include CTA (
|
| 25 |
-
-
|
| 26 |
-
-
|
| 27 |
-
-
|
| 28 |
-
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
Return only the 5 titles, one per line.
|
| 32 |
"""
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"contents": [{"role": "user", "parts": [{"text": prompt}]}],
|
| 37 |
-
"generationConfig": {
|
| 38 |
-
"temperature": 0.9,
|
| 39 |
-
"topP": 1,
|
| 40 |
-
"topK": 1,
|
| 41 |
-
"maxOutputTokens": 256
|
| 42 |
-
}
|
| 43 |
-
}, f)
|
| 44 |
-
f.flush()
|
| 45 |
-
|
| 46 |
-
command = [
|
| 47 |
-
"curl",
|
| 48 |
-
"-s",
|
| 49 |
-
"-X", "POST",
|
| 50 |
-
"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent",
|
| 51 |
-
"-H", f"Authorization: Bearer {api_key}",
|
| 52 |
-
"-H", "Content-Type: application/json",
|
| 53 |
-
"--data-binary", f"@{f.name}"
|
| 54 |
-
]
|
| 55 |
-
|
| 56 |
-
try:
|
| 57 |
-
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
| 58 |
-
response = json.loads(result.stdout)
|
| 59 |
-
|
| 60 |
-
# Check if candidates exist
|
| 61 |
-
if "candidates" in response:
|
| 62 |
-
return response["candidates"][0]["content"]["parts"][0]["text"].strip()
|
| 63 |
-
elif "error" in response:
|
| 64 |
-
return f"API Error: {response['error'].get('message', 'Unknown error')}"
|
| 65 |
-
else:
|
| 66 |
-
return f"Unexpected API Response:\n{json.dumps(response, indent=2)}"
|
| 67 |
-
|
| 68 |
-
except subprocess.CalledProcessError as e:
|
| 69 |
-
return f"API request failed:\n{e.stderr}"
|
| 70 |
-
except Exception as e:
|
| 71 |
-
return f"Exception occurred: {e}"
|
| 72 |
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# Gradio UI
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Function to generate Pinterest-optimized post titles
|
| 5 |
+
def generate_pinterest_titles(api_key, blog_title):
|
| 6 |
if not api_key or not blog_title:
|
| 7 |
+
return "Please provide both your API key and blog title."
|
| 8 |
|
| 9 |
+
try:
|
| 10 |
+
# Configure Gemini API
|
| 11 |
+
genai.configure(api_key=api_key)
|
| 12 |
+
model = genai.GenerativeModel('gemini-1.5-pro-latest')
|
| 13 |
|
| 14 |
+
# Prompt to inject emotion and make titles clickable for Pinterest
|
| 15 |
+
prompt = f"""
|
| 16 |
+
You are a Pinterest SEO and viral marketing expert in the fermentation niche (kefir, kombucha, fermented veggies).
|
| 17 |
|
| 18 |
+
Your task is to transform this blog title:
|
| 19 |
+
"{blog_title}"
|
| 20 |
|
| 21 |
+
Into 5 emotionally engaging, curiosity-driven, and human-sounding Pinterest post titles.
|
| 22 |
These should:
|
| 23 |
+
- Sound like a real human wrote them
|
| 24 |
+
- Include strong CTA (call to action)
|
| 25 |
+
- Spark emotion or curiosity (excitement, nostalgia, gut health benefits, etc.)
|
| 26 |
+
- Avoid robotic or AI-sounding phrasing
|
| 27 |
+
- Be highly clickable and engaging on Pinterest
|
| 28 |
+
- No hashtags or emojis
|
| 29 |
+
|
| 30 |
+
Return only the 5 titles, each on a separate line.
|
|
|
|
| 31 |
"""
|
| 32 |
|
| 33 |
+
response = model.generate_content(prompt)
|
| 34 |
+
return response.text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {e}"
|
| 38 |
|
| 39 |
# Gradio UI
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=generate_pinterest_titles,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Textbox(label="Gemini API Key", placeholder="Paste your Gemini API key here", type="password"),
|
| 44 |
+
gr.Textbox(label="Blog Title", placeholder="e.g., 10 Surprising Benefits of Drinking Kefir Every Day")
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.Textbox(label="Pinterest Post Titles (Humanized with CTA)", lines=8),
|
| 47 |
+
title="Pinterest Title Generator for Fermentation Blogs",
|
| 48 |
+
description="Generate humanized, emotional, CTA-packed Pinterest titles from your blog post titles using Gemini 1.5 Pro."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|