Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,33 +7,24 @@ def generate_pinterest_content(api_key, blog_title):
|
|
| 7 |
try:
|
| 8 |
# Configure the Gemini API
|
| 9 |
genai.configure(api_key=api_key)
|
| 10 |
-
|
| 11 |
# Set up the model
|
| 12 |
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 13 |
-
|
| 14 |
-
# Create the prompt
|
| 15 |
prompt = f"""
|
| 16 |
You are a Pinterest SEO expert specializing in fermentation content (kombucha, kefir, kimchi, sauerkraut, etc.).
|
| 17 |
-
|
| 18 |
-
Transform this blog title: "{blog_title}"
|
| 19 |
-
|
| 20 |
Into a complete Pinterest marketing package. For each of the 5 variations, provide:
|
| 21 |
-
|
| 22 |
1. Pinterest Title: Highly engaging, under 60 characters when possible
|
| 23 |
2. Pinterest Description: 150-300 characters with targeted keywords
|
| 24 |
3. Benefit or Hook: A specific, compelling benefit statement
|
| 25 |
-
4. CTA Button Text: Action-focused text for the button (e.g., "Learn More", "Try This Recipe")
|
| 26 |
-
5. Social Proof Statement: A line that builds credibility (e.g., "Loved by 5,000+ home fermenters")
|
| 27 |
6. Pinterest Tags/Keywords: 5-7 relevant keywords (comma-separated)
|
| 28 |
-
|
| 29 |
-
Follow these guidelines:
|
| 30 |
-
- Include strong call-to-actions
|
| 31 |
-
- Use emotional language that resonates with home fermentation enthusiasts
|
| 32 |
-
- Add specific benefits or outcomes
|
| 33 |
-
- Include relevant keywords for fermentation SEO
|
| 34 |
-
- Sound completely human and conversational (not AI-generated)
|
| 35 |
-
- Use numbers, questions, or "how to" formats where appropriate
|
| 36 |
-
- Focus on what makes fermentation exciting, healthy, or easy
|
| 37 |
|
| 38 |
Format each variation as a JSON object with these fields:
|
| 39 |
- title
|
|
@@ -42,54 +33,55 @@ def generate_pinterest_content(api_key, blog_title):
|
|
| 42 |
- cta_button
|
| 43 |
- social_proof
|
| 44 |
- tags
|
| 45 |
-
|
| 46 |
Return an array with 5 of these JSON objects.
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
# Generate response
|
| 50 |
response = model.generate_content(prompt)
|
| 51 |
-
|
| 52 |
# Parse the response as JSON
|
| 53 |
try:
|
| 54 |
-
# First try to parse directly
|
| 55 |
result = json.loads(response.text)
|
| 56 |
except json.JSONDecodeError:
|
| 57 |
-
# If direct parsing fails, try to extract JSON from the text
|
| 58 |
response_text = response.text
|
| 59 |
start_idx = response_text.find('[')
|
| 60 |
end_idx = response_text.rfind(']') + 1
|
| 61 |
-
|
| 62 |
if start_idx >= 0 and end_idx > start_idx:
|
| 63 |
json_str = response_text[start_idx:end_idx]
|
| 64 |
try:
|
| 65 |
result = json.loads(json_str)
|
| 66 |
except json.JSONDecodeError:
|
| 67 |
-
return f"Error parsing JSON from response: {response_text}"
|
| 68 |
else:
|
| 69 |
-
return f"Could not find JSON array in response: {response_text}"
|
| 70 |
-
|
| 71 |
-
#
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
for i, variation in enumerate(result, 1):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
return formatted_output
|
| 84 |
-
|
| 85 |
except Exception as e:
|
| 86 |
-
return f"Error generating Pinterest content: {str(e)}"
|
| 87 |
|
| 88 |
# Create the Gradio interface
|
| 89 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
|
| 90 |
gr.Markdown("# π± Pinterest Content Generator for Fermentation Blog")
|
| 91 |
gr.Markdown("Transform your blog titles into complete Pinterest marketing packages to drive traffic to your fermentation website.")
|
| 92 |
-
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column():
|
| 95 |
api_key = gr.Textbox(
|
|
@@ -103,13 +95,21 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
|
|
| 103 |
lines=2
|
| 104 |
)
|
| 105 |
submit_btn = gr.Button("Generate Pinterest Content", variant="primary")
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
label="
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
gr.Examples(
|
| 114 |
[
|
| 115 |
["How to Make Milk Kefir at Home"],
|
|
@@ -121,14 +121,13 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
|
|
| 121 |
inputs=blog_title,
|
| 122 |
label="Example Blog Titles"
|
| 123 |
)
|
| 124 |
-
|
| 125 |
-
# Set up the click event
|
| 126 |
submit_btn.click(
|
| 127 |
fn=generate_pinterest_content,
|
| 128 |
inputs=[api_key, blog_title],
|
| 129 |
-
outputs=
|
| 130 |
)
|
| 131 |
-
|
| 132 |
gr.Markdown("""
|
| 133 |
### π Pinterest Stats for Fermentation Content
|
| 134 |
- 89% of Pinterest users are actively looking for inspiration for projects
|
|
@@ -136,7 +135,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
|
|
| 136 |
- "How-to" pins get 30% more engagement than other types
|
| 137 |
- Pins with testimonials have 35% higher click-through rates
|
| 138 |
- Most active fermentation enthusiasts use Pinterest daily
|
| 139 |
-
|
| 140 |
### π Tips for Pinterest Success
|
| 141 |
- Pin at optimal times: 8-11pm and 2-4am EST
|
| 142 |
- Use vertical images with 2:3 ratio (1000px by 1500px is ideal)
|
|
@@ -146,6 +145,5 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
|
|
| 146 |
- Use Rich Pins to automatically include metadata from your website
|
| 147 |
""")
|
| 148 |
|
| 149 |
-
# Launch the app
|
| 150 |
if __name__ == "__main__":
|
| 151 |
app.launch()
|
|
|
|
| 7 |
try:
|
| 8 |
# Configure the Gemini API
|
| 9 |
genai.configure(api_key=api_key)
|
| 10 |
+
|
| 11 |
# Set up the model
|
| 12 |
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 13 |
+
|
| 14 |
+
# Create the prompt
|
| 15 |
prompt = f"""
|
| 16 |
You are a Pinterest SEO expert specializing in fermentation content (kombucha, kefir, kimchi, sauerkraut, etc.).
|
| 17 |
+
|
| 18 |
+
Transform this blog title: \"{blog_title}\"
|
| 19 |
+
|
| 20 |
Into a complete Pinterest marketing package. For each of the 5 variations, provide:
|
| 21 |
+
|
| 22 |
1. Pinterest Title: Highly engaging, under 60 characters when possible
|
| 23 |
2. Pinterest Description: 150-300 characters with targeted keywords
|
| 24 |
3. Benefit or Hook: A specific, compelling benefit statement
|
| 25 |
+
4. CTA Button Text: Action-focused text for the button (e.g., \"Learn More\", \"Try This Recipe\")
|
| 26 |
+
5. Social Proof Statement: A line that builds credibility (e.g., \"Loved by 5,000+ home fermenters\")
|
| 27 |
6. Pinterest Tags/Keywords: 5-7 relevant keywords (comma-separated)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
Format each variation as a JSON object with these fields:
|
| 30 |
- title
|
|
|
|
| 33 |
- cta_button
|
| 34 |
- social_proof
|
| 35 |
- tags
|
| 36 |
+
|
| 37 |
Return an array with 5 of these JSON objects.
|
| 38 |
"""
|
| 39 |
+
|
| 40 |
# Generate response
|
| 41 |
response = model.generate_content(prompt)
|
| 42 |
+
|
| 43 |
# Parse the response as JSON
|
| 44 |
try:
|
|
|
|
| 45 |
result = json.loads(response.text)
|
| 46 |
except json.JSONDecodeError:
|
|
|
|
| 47 |
response_text = response.text
|
| 48 |
start_idx = response_text.find('[')
|
| 49 |
end_idx = response_text.rfind(']') + 1
|
|
|
|
| 50 |
if start_idx >= 0 and end_idx > start_idx:
|
| 51 |
json_str = response_text[start_idx:end_idx]
|
| 52 |
try:
|
| 53 |
result = json.loads(json_str)
|
| 54 |
except json.JSONDecodeError:
|
| 55 |
+
return "", "", "", "", "", "", f"Error parsing JSON from response: {response_text}"
|
| 56 |
else:
|
| 57 |
+
return "", "", "", "", "", "", f"Could not find JSON array in response: {response_text}"
|
| 58 |
+
|
| 59 |
+
# Separate outputs into categories
|
| 60 |
+
titles = []
|
| 61 |
+
descriptions = []
|
| 62 |
+
hooks = []
|
| 63 |
+
ctas = []
|
| 64 |
+
proofs = []
|
| 65 |
+
tags = []
|
| 66 |
+
|
| 67 |
for i, variation in enumerate(result, 1):
|
| 68 |
+
titles.append(f"{i}. {variation.get('title', 'N/A')}")
|
| 69 |
+
descriptions.append(f"{i}. {variation.get('description', 'N/A')}")
|
| 70 |
+
hooks.append(f"{i}. {variation.get('benefit_hook', 'N/A')}")
|
| 71 |
+
ctas.append(f"{i}. {variation.get('cta_button', 'N/A')}")
|
| 72 |
+
proofs.append(f"{i}. {variation.get('social_proof', 'N/A')}")
|
| 73 |
+
tags.append(f"{i}. {variation.get('tags', 'N/A')}")
|
| 74 |
+
|
| 75 |
+
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."
|
| 76 |
+
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
+
return "", "", "", "", "", "", f"Error generating Pinterest content: {str(e)}"
|
| 79 |
|
| 80 |
# Create the Gradio interface
|
| 81 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as app:
|
| 82 |
gr.Markdown("# π± Pinterest Content Generator for Fermentation Blog")
|
| 83 |
gr.Markdown("Transform your blog titles into complete Pinterest marketing packages to drive traffic to your fermentation website.")
|
| 84 |
+
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column():
|
| 87 |
api_key = gr.Textbox(
|
|
|
|
| 95 |
lines=2
|
| 96 |
)
|
| 97 |
submit_btn = gr.Button("Generate Pinterest Content", variant="primary")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
title_output = gr.Textbox(label="π Pinterest Titles", lines=10)
|
| 101 |
+
description_output = gr.Textbox(label="π Descriptions", lines=10)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
hook_output = gr.Textbox(label="π― Benefits or Hooks", lines=10)
|
| 105 |
+
cta_output = gr.Textbox(label="π CTA Buttons", lines=10)
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
proof_output = gr.Textbox(label="π₯ Social Proof Statements", lines=10)
|
| 109 |
+
tag_output = gr.Textbox(label="π Pinterest Tags", lines=10)
|
| 110 |
+
|
| 111 |
+
status_output = gr.Markdown("π Click the button above to generate Pinterest content.")
|
| 112 |
+
|
| 113 |
gr.Examples(
|
| 114 |
[
|
| 115 |
["How to Make Milk Kefir at Home"],
|
|
|
|
| 121 |
inputs=blog_title,
|
| 122 |
label="Example Blog Titles"
|
| 123 |
)
|
| 124 |
+
|
|
|
|
| 125 |
submit_btn.click(
|
| 126 |
fn=generate_pinterest_content,
|
| 127 |
inputs=[api_key, blog_title],
|
| 128 |
+
outputs=[title_output, description_output, hook_output, cta_output, proof_output, tag_output, status_output]
|
| 129 |
)
|
| 130 |
+
|
| 131 |
gr.Markdown("""
|
| 132 |
### π Pinterest Stats for Fermentation Content
|
| 133 |
- 89% of Pinterest users are actively looking for inspiration for projects
|
|
|
|
| 135 |
- "How-to" pins get 30% more engagement than other types
|
| 136 |
- Pins with testimonials have 35% higher click-through rates
|
| 137 |
- Most active fermentation enthusiasts use Pinterest daily
|
| 138 |
+
|
| 139 |
### π Tips for Pinterest Success
|
| 140 |
- Pin at optimal times: 8-11pm and 2-4am EST
|
| 141 |
- Use vertical images with 2:3 ratio (1000px by 1500px is ideal)
|
|
|
|
| 145 |
- Use Rich Pins to automatically include metadata from your website
|
| 146 |
""")
|
| 147 |
|
|
|
|
| 148 |
if __name__ == "__main__":
|
| 149 |
app.launch()
|