mroccuper's picture
Update app.py
bbfb2ba verified
raw
history blame
2.99 kB
import gradio as gr
import html2text
import google.generativeai as genai
import os
# Configure Gemini API
genai.configure(api_key="your-gemini-api-key-here")
model = genai.GenerativeModel("gemini-pro")
# Prompt template
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 generation
def generate_post(file, platform, styles, emotions):
if not file:
return "Please upload a file."
# Read and convert HTML file
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
# Gradio UI
with gr.Blocks(title="Reddit/Quora Post Generator") as demo:
gr.Markdown("## 🔥 Turn your SEO content into humanized Reddit or Quora posts")
with gr.Row():
file_input = gr.File(label="Upload 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)
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
generate_btn.click(fn=generate_post, inputs=[file_input, platform, styles, emotions], outputs=output)
output.change(fn=download_outputs, inputs=output, outputs=[download_html, download_txt])
demo.launch()