mroccuper's picture
Update app.py
1d891a0 verified
raw
history blame
3.26 kB
import gradio as gr
import html2text
import google.generativeai as genai
import os
# Prompt template builder
def build_prompt(content, platform, styles, emotions):
style_text = ", ".join(styles) if styles else "humanized"
emotion_text = ", ".join(emotions) if emotions else "genuine"
return f"""
You are a fermentation enthusiast who has real-world experience.
Take the following HTML content, summarize it, and turn it into a highly engaging {platform} post.
The tone must feel like it’s written by a human—not AI. Be expressive, imperfect, casual, and honest.
Use styles like {style_text}, and include emotional tones like {emotion_text}.
Sound like you're sharing a personal story or tip—not giving a lecture.
Output structure:
1. Title (1 sentence, catchy, personal)
2. Post body: 2–4 paragraphs, casual tone, share what worked/didn’t, and end with a relatable or curious question.
CONTENT TO CONVERT:
\"\"\"{content}\"\"\"
"""
# Function to handle post generation
def generate_post(api_key, file, platform, styles, emotions):
if not api_key:
return "Please enter your Gemini API key first."
if not file:
return "Please upload a file."
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel("gemini-pro")
raw_html = file.read().decode("utf-8")
plain_text = html2text.html2text(raw_html)
prompt = build_prompt(plain_text, platform, styles, emotions)
response = model.generate_content(prompt)
return response.text
except Exception as e:
return f"Error: {e}"
# Download handlers
def download_outputs(content):
html_path = "/tmp/post_output.html"
txt_path = "/tmp/post_output.txt"
with open(html_path, "w") as f:
f.write(content)
with open(txt_path, "w") as f:
f.write(content)
return html_path, txt_path
# Gradio App
with gr.Blocks(title="Reddit/Quora Post Generator") as demo:
gr.Markdown("## 🔥 Turn your SEO-rich HTML content into humanized Reddit or Quora posts")
api_key = gr.Textbox(label="🔐 Gemini API Key", type="password", placeholder="Paste your Gemini API key")
with gr.Row():
file_input = gr.File(label="Upload SEO HTML file", file_types=[".html"])
platform = gr.Dropdown(["Reddit", "Quora"], label="Select Platform", value="Reddit")
with gr.Row():
styles = gr.CheckboxGroup(
["Humanized", "Friendly", "Personal/Storytelling"],
label="Writing Style",
value=["Humanized"]
)
emotions = gr.CheckboxGroup(
["Curious", "Enthusiastic", "Skeptical", "Inspiring"],
label="Emotional Tone",
value=["Curious"]
)
generate_btn = gr.Button("✨ Generate Post")
output = gr.Textbox(label="Generated Post", lines=15)
with gr.Row():
download_html = gr.File(label="⬇️ Download as .html", interactive=False)
download_txt = gr.File(label="⬇️ Download as .txt", interactive=False)
generate_btn.click(fn=generate_post, inputs=[api_key, file_input, platform, styles, emotions], outputs=output)
output.change(fn=download_outputs, inputs=output, outputs=[download_html, download_txt])
demo.launch()