Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,8 @@
|
|
| 1 |
# File: app.py
|
| 2 |
-
import
|
| 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 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
Semantic
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|