mroccuper commited on
Commit
c1cb2e8
Β·
verified Β·
1 Parent(s): b46af77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -101
app.py CHANGED
@@ -1,16 +1,8 @@
1
  # File: app.py
2
- import streamlit as st
3
  import google.generativeai as genai
4
  import textstat
5
 
6
- # Configure Gemini API (uses Hugging Face Secrets)
7
- GOOGLE_API_KEY = st.secrets.get("GOOGLE_API_KEY", "YOUR_API_KEY_HERE")
8
- genai.configure(api_key=GOOGLE_API_KEY)
9
-
10
- # Page config
11
- st.set_page_config(page_title="SEO Article Generator", layout="wide")
12
- st.title("SEO-to-Article Generator πŸš€")
13
-
14
  # Humanizer helper
15
  def humanize_text(text):
16
  replacements = {
@@ -39,95 +31,124 @@ def generate_title_variations():
39
  "DIY Kombucha: Step-by-Step Guide to Brew Like a Pro"
40
  ]
41
 
42
- # Sidebar inputs
43
- with st.sidebar:
44
- st.header("πŸ“ Article Settings")
45
- pov = st.selectbox("Point of View", ["First Person", "Second Person", "Third Person"])
46
- tone = st.selectbox("Blog Tone", ["Friendly Guide", "Professional Expert", "Step-by-Step Teacher", "Curious Explorer"])
47
- length = st.selectbox("Content Length", ["Short (500)", "Standard (800)", "Long (1200+)"])
48
- emotion = st.selectbox("Emotional Tone", ["Trust", "Clarity", "Confidence", "Curiosity"])
49
- include_titles = st.checkbox("Generate Title Variations")
50
- include_readability = st.checkbox("Show Readability Analysis")
51
- internal_links = st.text_area("Internal Links (one per line)", height=100, help="Format: https://example.com ")
52
- keywords = st.text_area("Extra Keywords", height=100, help="One per line")
53
-
54
- # Main input
55
- seo_plan = st.text_area(
56
- "πŸ“„ Paste Your SEO Plan",
57
- height=300,
58
- help="Example: How to Make Kombucha at Home...",
59
- value="""Comprehensive SEO Content Plan: How to Make Kombucha at Home
60
- 1. Strategic Keyword Analysis:
61
- Primary Target Keyword: how to make kombucha
62
- Secondary Keywords: kombucha recipe, homemade kombucha
63
- Long-Tail Keywords & User Questions:
64
- how to make kombucha at home for beginners
65
- easy kombucha recipe for first timers
66
- best kombucha recipe with step-by-step instructions
67
- how long does it take to make kombucha
68
- what equipment do i need to make kombucha
69
- how to make kombucha without sugar
70
- is kombucha good for you
71
- Semantic/LSI Keywords: SCOBY, fermentation, probiotics, gut health, tea, sugar, flavoring, F1, F2
72
- Search Intent Analysis: Primarily informational, with some transactional intent (buying SCOBY, equipment)
73
- 2. Competitive Landscape Overview:
74
- ...[rest of your SEO plan]..."""
75
- )
76
-
77
- if st.button("✨ Generate Article", type="primary"):
78
- with st.spinner("🧠 Generating article with Gemini 1.5 Pro..."):
79
- try:
80
- # Build prompt
81
- prompt = f"""
82
- You are a human guide who wants to help readers solve real problems.
83
- Write a {length} article in {pov} POV with a {tone} tone.
84
-
85
- INSTRUCTIONS:
86
- 1. Structure:
87
- - Use H1: "[H1 TITLE]"
88
- - Include all H2/H3 headings from the SEO plan
89
- - Add FAQs with simple, conversational answers
90
- - Embed internal links at natural points (use exact anchor text)
91
-
92
- 2. Style Rules:
93
- - Avoid AI clichΓ©s
94
- - Use contractions (you're, don't, it's)
95
- - Add 1 relatable anecdote
96
- - Keep paragraphs short (<3 sentences)
97
-
98
- 3. Emotional Tone: {emotion}
99
- - Example: If "Confidence", say "You've got this!"
100
-
101
- 4. Keywords:
102
- - Primary keyword: "[PRIMARY KEYWORD]" (2-3x)
103
- - Semantic keywords: {keywords} (sprinkle naturally)
104
-
105
- SEO PLAN: {seo_plan}
106
- """
107
-
108
- # Gemini API call
109
- model = genai.GenerativeModel('gemini-1.5-pro')
110
- response = model.generate_content(prompt)
111
-
112
- # Post-processing
113
- humanized = humanize_text(response.text)
114
- if include_readability:
115
- humanized += "\n\n" + analyze_readability(humanized)
116
- if include_titles:
117
- titles = generate_title_variations()
118
- humanized = "Title Variations:\n" + "\n".join([f"{i+1}. {t}" for i, t in enumerate(titles)]) + "\n\n" + humanized
119
-
120
- # Output result
121
- st.markdown("## ✨ Generated Article")
122
- st.text_area("", value=humanized, height=600, key="output")
123
-
124
- # Stats
125
- st.info(f"βœ… Word Count: {len(humanized.split())} | Model: Gemini 1.5 Pro")
126
-
127
- except Exception as e:
128
- st.error(f"🚨 Error: {str(e)}")
129
- st.info("Please check your API key or try again later.")
130
-
131
- # Footer
132
- st.markdown("---")
133
- st.markdown("Built for [YourBrand.com] | Powered by Google Gemini & Streamlit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # File: app.py
2
+ import gradio as gr
3
  import google.generativeai as genai
4
  import textstat
5
 
 
 
 
 
 
 
 
 
6
  # Humanizer helper
7
  def humanize_text(text):
8
  replacements = {
 
31
  "DIY Kombucha: Step-by-Step Guide to Brew Like a Pro"
32
  ]
33
 
34
+ # Main generation function
35
+ def generate_article(api_key, seo_plan, pov, tone, length, emotion, include_titles, include_readability, internal_links, keywords):
36
+ try:
37
+ # Configure Gemini API dynamically
38
+ genai.configure(api_key=api_key.strip())
39
+
40
+ # Build prompt
41
+ prompt = f"""
42
+ You are a human guide who wants to help readers solve real problems.
43
+ Write a {length} article in {pov} POV with a {tone} tone.
44
+
45
+ INSTRUCTIONS:
46
+ 1. Structure:
47
+ - Use H1: "[H1 TITLE]"
48
+ - Include all H2/H3 headings from the SEO plan
49
+ - Add FAQs with simple, conversational answers
50
+ - Embed internal links at natural points (use exact anchor text)
51
+
52
+ 2. Style Rules:
53
+ - Avoid AI clichΓ©s
54
+ - Use contractions (you're, don't, it's)
55
+ - Add 1 relatable anecdote
56
+ - Keep paragraphs short (<3 sentences)
57
+
58
+ 3. Emotional Tone: {emotion}
59
+ - Example: If "Confidence", say "You've got this!"
60
+
61
+ 4. Keywords:
62
+ - Primary keyword: "[PRIMARY KEYWORD]" (2-3x)
63
+ - Semantic keywords: {keywords} (sprinkle naturally)
64
+
65
+ SEO PLAN: {seo_plan}
66
+ """
67
+
68
+ # Gemini API call
69
+ model = genai.GenerativeModel('gemini-1.5-pro')
70
+ response = model.generate_content(prompt)
71
+
72
+ # Post-processing
73
+ humanized = humanize_text(response.text)
74
+
75
+ if include_readability:
76
+ humanized += "\n\n" + analyze_readability(humanized)
77
+
78
+ if include_titles:
79
+ titles = generate_title_variations()
80
+ humanized = "Title Variations:\n" + "\n".join([f"{i+1}. {t}" for i, t in enumerate(titles)]) + "\n\n" + humanized
81
+
82
+ return humanized
83
+
84
+ except Exception as e:
85
+ return f"🚨 Error: {str(e)}\n\nPlease check your API key or try again later."
86
+
87
+ # Gradio Interface
88
+ with gr.Blocks(title="SEO Article Generator") as demo:
89
+ gr.Markdown("# SEO-to-Article Generator πŸš€")
90
+ gr.Markdown("Enter your Gemini API key and paste an SEO plan to generate humanized, SEO-optimized content.")
91
+
92
+ with gr.Row():
93
+ with gr.Column(scale=1):
94
+ api_key = gr.Textbox(label="πŸ”‘ Gemini API Key", type="password", placeholder="AIzaSy...your-key-here...")
95
+
96
+ pov = gr.Dropdown(
97
+ label="πŸ“– Point of View",
98
+ choices=["First Person", "Second Person", "Third Person"],
99
+ value="First Person"
100
+ )
101
+
102
+ tone = gr.Dropdown(
103
+ label="🎨 Blog Tone",
104
+ choices=["Friendly Guide", "Professional Expert", "Step-by-Step Teacher", "Curious Explorer"],
105
+ value="Friendly Guide"
106
+ )
107
+
108
+ length = gr.Dropdown(
109
+ label="πŸ“„ Content Length",
110
+ choices=["Short (500)", "Standard (800)", "Long (1200+)"],
111
+ value="Standard"
112
+ )
113
+
114
+ emotion = gr.Dropdown(
115
+ label="🧠 Emotional Tone",
116
+ choices=["Trust", "Clarity", "Confidence", "Curiosity"],
117
+ value="Trust"
118
+ )
119
+
120
+ internal_links = gr.Textbox(
121
+ label="πŸ”— Internal Links",
122
+ placeholder="https://example.com/page1 \nhttps://example.com/page2 ",
123
+ lines=3
124
+ )
125
+
126
+ keywords = gr.Textbox(
127
+ label="πŸ”‘ Extra Keywords",
128
+ placeholder="SCOBY, probiotics, fermentation",
129
+ lines=3
130
+ )
131
+
132
+ include_titles = gr.Checkbox(label="Generate Title Variations", value=True)
133
+ include_readability = gr.Checkbox(label="Show Readability Analysis", value=True)
134
+
135
+ with gr.Column(scale=2):
136
+ seo_plan = gr.Textbox(
137
+ label="πŸ“„ Paste Your SEO Plan",
138
+ placeholder="Primary Keyword: how to make kombucha\nSecondary Keywords: kombucha recipe...\n...",
139
+ lines=15
140
+ )
141
+
142
+ submit_btn = gr.Button("✨ Generate Article", variant="primary")
143
+
144
+ output = gr.Textbox(label="πŸ“ Generated Article", lines=20)
145
+
146
+ submit_btn.click(
147
+ fn=generate_article,
148
+ inputs=[api_key, seo_plan, pov, tone, length, emotion, include_titles, include_readability, internal_links, keywords],
149
+ outputs=output
150
+ )
151
+
152
+ # Launch app
153
+ if __name__ == "__main__":
154
+ demo.launch()