mroccuper commited on
Commit
80b662f
Β·
verified Β·
1 Parent(s): c72cc1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -73
app.py CHANGED
@@ -1,86 +1,52 @@
1
  import gradio as gr
2
- import subprocess
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
- prompt = f"""
15
- You are a Pinterest SEO and viral marketing expert.
 
 
16
 
17
- Your task is to take this blog title:
18
- "{blog_title}"
 
19
 
20
- And generate 5 viral, emotionally engaging, human-sounding Pinterest post titles.
 
21
 
 
22
  These should:
23
- - Be optimized for Pinterest clicks
24
- - Include CTA (Call-To-Action)
25
- - Sound human, not robotic or like AI
26
- - Use emotional tone (excitement, curiosity, nostalgia, empowerment, etc.)
27
- - Focus on the fermentation niche (kefir, kombucha, sauerkraut, gut health, etc.)
28
- - Use no hashtags or emojis
29
- - Be short and highly clickable
30
-
31
- Return only the 5 titles, one per line.
32
  """
33
 
34
- with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=".json") as f:
35
- json.dump({
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
- with gr.Blocks(title="Pinterest Title Generator - Fermentation Niche") as app:
76
- gr.Markdown("## πŸ“ Pinterest Title Generator for Fermentation Blogs\nTurn your blog title into viral Pinterest titles that drive clicks!")
77
-
78
- api_key_input = gr.Textbox(label="πŸ”‘ Gemini API Key", placeholder="Paste your Gemini API key here", type="password")
79
- blog_title_input = gr.Textbox(label="πŸ“ Blog Title", placeholder="Enter your fermentation blog title")
80
- submit_btn = gr.Button("πŸš€ Generate Pinterest Titles")
81
-
82
- output_box = gr.Textbox(label="🎯 Pinterest Post Title Ideas", lines=10)
83
-
84
- submit_btn.click(fn=generate_titles, inputs=[api_key_input, blog_title_input], outputs=output_box)
85
-
86
- app.launch()
 
 
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()