Pin-Titles / app.py
mroccuper's picture
Update app.py
5d7a0c8 verified
import gradio as gr
import google.generativeai as genai
import os
import json
def generate_pinterest_content(api_key, blog_title):
try:
# Configure the Gemini API
genai.configure(api_key=api_key)
# Set up the model
model = genai.GenerativeModel('gemini-1.5-pro')
# Create the prompt
prompt = f"""
You are a Pinterest SEO expert specializing in fermentation content (kombucha, kefir, kimchi, sauerkraut, etc.).
Transform this blog title: \"{blog_title}\"
Into a complete Pinterest marketing package. For each of the 5 variations, provide:
1. Pinterest Title: Highly engaging, under 60 characters when possible
2. Pinterest Description: 150-300 characters with targeted keywords
3. Benefit or Hook: A specific, compelling benefit statement
4. CTA Button Text: Action-focused text for the button (e.g., \"Learn More\", \"Try This Recipe\")
5. Social Proof Statement: A line that builds credibility (e.g., \"Loved by 5,000+ home fermenters\")
6. Pinterest Tags/Keywords: 5-7 relevant keywords (comma-separated)
Format each variation as a JSON object with these fields:
- title
- description
- benefit_hook
- cta_button
- social_proof
- tags
Return an array with 5 of these JSON objects.
"""
# Generate response
response = model.generate_content(prompt)
# Parse the response as JSON
try:
result = json.loads(response.text)
except json.JSONDecodeError:
response_text = response.text
start_idx = response_text.find('[')
end_idx = response_text.rfind(']') + 1
if start_idx >= 0 and end_idx > start_idx:
json_str = response_text[start_idx:end_idx]
try:
result = json.loads(json_str)
except json.JSONDecodeError:
return "", "", "", "", "", "", f"Error parsing JSON from response: {response_text}"
else:
return "", "", "", "", "", "", f"Could not find JSON array in response: {response_text}"
# Separate outputs into categories
titles = []
descriptions = []
hooks = []
ctas = []
proofs = []
tags = []
for i, variation in enumerate(result, 1):
titles.append(f"{i}. {variation.get('title', 'N/A')}")
descriptions.append(f"{i}. {variation.get('description', 'N/A')}")
hooks.append(f"{i}. {variation.get('benefit_hook', 'N/A')}")
ctas.append(f"{i}. {variation.get('cta_button', 'N/A')}")
proofs.append(f"{i}. {variation.get('social_proof', 'N/A')}")
tags.append(f"{i}. {variation.get('tags', 'N/A')}")
return "\n\n".join(titles), "\n\n".join(descriptions), "\n\n".join(hooks), "\n\n".join(ctas), "\n\n".join(proofs), "\n\n".join(tags), "βœ… Pinterest content generated successfully."
except Exception as e:
return "", "", "", "", "", "", f"Error generating Pinterest content: {str(e)}"
# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
gr.Markdown("# 🌱 Pinterest Content Generator for Fermentation Blog")
gr.Markdown("Transform your blog titles into complete Pinterest marketing packages to drive traffic to your fermentation website.")
with gr.Row():
with gr.Column():
api_key = gr.Textbox(
label="Gemini API Key",
placeholder="Enter your Gemini API key...",
type="password"
)
blog_title = gr.Textbox(
label="Blog Post Title",
placeholder="Enter your original blog post title here...",
lines=2
)
submit_btn = gr.Button("Generate Pinterest Content", variant="primary")
with gr.Row():
title_output = gr.Textbox(label="πŸ“Œ Pinterest Titles", lines=10)
description_output = gr.Textbox(label="πŸ“ Descriptions", lines=10)
with gr.Row():
hook_output = gr.Textbox(label="🎯 Benefits or Hooks", lines=10)
cta_output = gr.Textbox(label="πŸ‘† CTA Buttons", lines=10)
with gr.Row():
proof_output = gr.Textbox(label="πŸ‘₯ Social Proof Statements", lines=10)
tag_output = gr.Textbox(label="πŸ”‘ Pinterest Tags", lines=10)
status_output = gr.Markdown("πŸ‘† Click the button above to generate Pinterest content.")
gr.Examples(
[
["How to Make Milk Kefir at Home"],
["Kombucha Brewing Guide for Beginners"],
["10 Health Benefits of Fermented Foods"],
["Troubleshooting Common Sauerkraut Problems"],
["Easy Kimchi Recipe for First-time Fermenters"]
],
inputs=blog_title,
label="Example Blog Titles"
)
submit_btn.click(
fn=generate_pinterest_content,
inputs=[api_key, blog_title],
outputs=[title_output, description_output, hook_output, cta_output, proof_output, tag_output, status_output]
)
gr.Markdown("""
### πŸ“Š Pinterest Stats for Fermentation Content
- 89% of Pinterest users are actively looking for inspiration for projects
- Food & Recipe pins have 5x higher engagement than average pins
- "How-to" pins get 30% more engagement than other types
- Pins with testimonials have 35% higher click-through rates
- Most active fermentation enthusiasts use Pinterest daily
### πŸ“Œ Tips for Pinterest Success
- Pin at optimal times: 8-11pm and 2-4am EST
- Use vertical images with 2:3 ratio (1000px by 1500px is ideal)
- Include your keyword in the first 60 characters
- Create multiple pins for the same content with different visuals
- Join group boards focused on natural food, gut health, and fermentation
- Use Rich Pins to automatically include metadata from your website
""")
if __name__ == "__main__":
app.launch()