mroccuper commited on
Commit
1d891a0
·
verified ·
1 Parent(s): bbfb2ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -3,11 +3,7 @@ import html2text
3
  import google.generativeai as genai
4
  import os
5
 
6
- # Configure Gemini API
7
- genai.configure(api_key="your-gemini-api-key-here")
8
- model = genai.GenerativeModel("gemini-pro")
9
-
10
- # Prompt template
11
  def build_prompt(content, platform, styles, emotions):
12
  style_text = ", ".join(styles) if styles else "humanized"
13
  emotion_text = ", ".join(emotions) if emotions else "genuine"
@@ -27,25 +23,45 @@ CONTENT TO CONVERT:
27
  \"\"\"{content}\"\"\"
28
  """
29
 
30
- # Function to handle generation
31
- def generate_post(file, platform, styles, emotions):
 
 
32
  if not file:
33
  return "Please upload a file."
34
 
35
- # Read and convert HTML file
36
- raw_html = file.read().decode("utf-8")
37
- plain_text = html2text.html2text(raw_html)
 
 
 
 
 
 
 
38
 
39
- prompt = build_prompt(plain_text, platform, styles, emotions)
40
- response = model.generate_content(prompt)
41
- return response.text
42
 
43
- # Gradio UI
 
 
 
 
 
 
 
 
 
 
44
  with gr.Blocks(title="Reddit/Quora Post Generator") as demo:
45
- gr.Markdown("## 🔥 Turn your SEO content into humanized Reddit or Quora posts")
 
 
46
 
47
  with gr.Row():
48
- file_input = gr.File(label="Upload HTML file", file_types=[".html"])
49
  platform = gr.Dropdown(["Reddit", "Quora"], label="Select Platform", value="Reddit")
50
 
51
  with gr.Row():
@@ -60,23 +76,14 @@ with gr.Blocks(title="Reddit/Quora Post Generator") as demo:
60
  value=["Curious"]
61
  )
62
 
63
- generate_btn = gr.Button("Generate Post")
64
  output = gr.Textbox(label="Generated Post", lines=15)
65
 
66
  with gr.Row():
67
- download_html = gr.File(label="Download as .html", interactive=False)
68
- download_txt = gr.File(label="Download as .txt", interactive=False)
69
-
70
- def download_outputs(content):
71
- html_path = "/tmp/post_output.html"
72
- txt_path = "/tmp/post_output.txt"
73
- with open(html_path, "w") as f:
74
- f.write(content)
75
- with open(txt_path, "w") as f:
76
- f.write(content)
77
- return html_path, txt_path
78
-
79
- generate_btn.click(fn=generate_post, inputs=[file_input, platform, styles, emotions], outputs=output)
80
  output.change(fn=download_outputs, inputs=output, outputs=[download_html, download_txt])
81
 
82
  demo.launch()
 
3
  import google.generativeai as genai
4
  import os
5
 
6
+ # Prompt template builder
 
 
 
 
7
  def build_prompt(content, platform, styles, emotions):
8
  style_text = ", ".join(styles) if styles else "humanized"
9
  emotion_text = ", ".join(emotions) if emotions else "genuine"
 
23
  \"\"\"{content}\"\"\"
24
  """
25
 
26
+ # Function to handle post generation
27
+ def generate_post(api_key, file, platform, styles, emotions):
28
+ if not api_key:
29
+ return "Please enter your Gemini API key first."
30
  if not file:
31
  return "Please upload a file."
32
 
33
+ try:
34
+ genai.configure(api_key=api_key)
35
+ model = genai.GenerativeModel("gemini-pro")
36
+
37
+ raw_html = file.read().decode("utf-8")
38
+ plain_text = html2text.html2text(raw_html)
39
+ prompt = build_prompt(plain_text, platform, styles, emotions)
40
+
41
+ response = model.generate_content(prompt)
42
+ return response.text
43
 
44
+ except Exception as e:
45
+ return f"Error: {e}"
 
46
 
47
+ # Download handlers
48
+ def download_outputs(content):
49
+ html_path = "/tmp/post_output.html"
50
+ txt_path = "/tmp/post_output.txt"
51
+ with open(html_path, "w") as f:
52
+ f.write(content)
53
+ with open(txt_path, "w") as f:
54
+ f.write(content)
55
+ return html_path, txt_path
56
+
57
+ # Gradio App
58
  with gr.Blocks(title="Reddit/Quora Post Generator") as demo:
59
+ gr.Markdown("## 🔥 Turn your SEO-rich HTML content into humanized Reddit or Quora posts")
60
+
61
+ api_key = gr.Textbox(label="🔐 Gemini API Key", type="password", placeholder="Paste your Gemini API key")
62
 
63
  with gr.Row():
64
+ file_input = gr.File(label="Upload SEO HTML file", file_types=[".html"])
65
  platform = gr.Dropdown(["Reddit", "Quora"], label="Select Platform", value="Reddit")
66
 
67
  with gr.Row():
 
76
  value=["Curious"]
77
  )
78
 
79
+ generate_btn = gr.Button("Generate Post")
80
  output = gr.Textbox(label="Generated Post", lines=15)
81
 
82
  with gr.Row():
83
+ download_html = gr.File(label="⬇️ Download as .html", interactive=False)
84
+ download_txt = gr.File(label="⬇️ Download as .txt", interactive=False)
85
+
86
+ generate_btn.click(fn=generate_post, inputs=[api_key, file_input, platform, styles, emotions], outputs=output)
 
 
 
 
 
 
 
 
 
87
  output.change(fn=download_outputs, inputs=output, outputs=[download_html, download_txt])
88
 
89
  demo.launch()