mroccuper commited on
Commit
67230d8
·
verified ·
1 Parent(s): 3b8efe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -24
app.py CHANGED
@@ -25,10 +25,12 @@ def extract_main_content(html_text):
25
  # ---------------------------
26
  # Prompt builder
27
  # ---------------------------
28
- def build_prompt(text, tones, platform):
29
  try:
30
  tone_str = ", ".join(tones)
31
  print(f"[build_prompt] Using tones: {tone_str} for platform: {platform}")
 
 
32
  return f"""
33
  You are a skilled content writer. Based on the content below, generate a post for {platform}.
34
  The post should feel like someone sharing a personal experience or story related to the topic.
@@ -38,10 +40,7 @@ Make it {tone_str.lower()}, natural, and relatable. Include a catchy title to ho
38
  {text}
39
  ---
40
 
41
- Return in this format:
42
- Title: <catchy title>
43
- Body:
44
- <post body text>
45
  """
46
  except Exception as e:
47
  print(f"[build_prompt] Error: {e}")
@@ -50,23 +49,10 @@ Body:
50
  # ---------------------------
51
  # Function to call Gemini
52
  # ---------------------------
53
- def generate_post(api_key, text, tones, platform, humanize_output=False):
54
  try:
55
  headers = {"Content-Type": "application/json"}
56
- prompt = build_prompt(text, tones, platform)
57
-
58
- if humanize_output:
59
- prompt += """
60
-
61
- After generating the post, revise it to sound more naturally human and casual, as if written by someone genuinely sharing their experience with a bit of humor and self-deprecation.
62
- Soften the overly polished tone. Add minor imperfections (like fragments, casual slang, hesitations, etc.) to make it feel spontaneous.
63
-
64
- Return format:
65
- Title: <hooky title>
66
- Body:
67
- <revised post>
68
- """
69
-
70
  data = {
71
  "contents": [{"parts": [{"text": prompt}]}]
72
  }
@@ -87,7 +73,7 @@ Body:
87
  # ---------------------------
88
  # Gradio UI
89
  # ---------------------------
90
- def app_interface(html_file, tones, platform_choice, api_key, humanize_output):
91
  try:
92
  if not html_file:
93
  return "Please upload an HTML blog post."
@@ -106,7 +92,7 @@ def app_interface(html_file, tones, platform_choice, api_key, humanize_output):
106
  return "Couldn't extract content from file. Ensure it's a valid HTML blog post."
107
 
108
  print("[app_interface] Generating post...")
109
- output = generate_post(api_key, main_text, tones, platform_choice, humanize_output)
110
  return output
111
  except Exception as e:
112
  print(f"[app_interface] Error: {e}")
@@ -123,11 +109,11 @@ with gr.Blocks() as demo:
123
  tone_checkboxes = gr.CheckboxGroup(label="Choose Tones", choices=TONE_OPTIONS, value=["Friendly", "Humanized"])
124
  platform_radio = gr.Radio(label="Target Platform", choices=["Reddit", "Quora"], value="Reddit")
125
  api_key_input = gr.Textbox(label="Enter Your Gemini API Key", type="password")
126
- humanizer_checkbox = gr.Checkbox(label="💬 Make More Human + Casual?", value=True)
127
  generate_button = gr.Button("Generate Post")
128
  output_box = gr.Textbox(label="Generated Post", lines=20, interactive=True)
129
 
130
- generate_button.click(app_interface, inputs=[html_input, tone_checkboxes, platform_radio, api_key_input, humanizer_checkbox], outputs=output_box)
131
 
132
  # ---------------------------
133
  # Launch App
 
25
  # ---------------------------
26
  # Prompt builder
27
  # ---------------------------
28
+ def build_prompt(text, tones, platform, blog_link):
29
  try:
30
  tone_str = ", ".join(tones)
31
  print(f"[build_prompt] Using tones: {tone_str} for platform: {platform}")
32
+
33
+ # Add a friendly invitation to the end of the generated post
34
  return f"""
35
  You are a skilled content writer. Based on the content below, generate a post for {platform}.
36
  The post should feel like someone sharing a personal experience or story related to the topic.
 
40
  {text}
41
  ---
42
 
43
+ And by the way, if you're curious about my fermentation experiments or want to join in on the fun, feel free to check out my blog! I've got all sorts of tips, tricks, and beginner-friendly recipes to help you start your own fermentation journey. {blog_link} – hope to see you there!
 
 
 
44
  """
45
  except Exception as e:
46
  print(f"[build_prompt] Error: {e}")
 
49
  # ---------------------------
50
  # Function to call Gemini
51
  # ---------------------------
52
+ def generate_post(api_key, text, tones, platform, blog_link):
53
  try:
54
  headers = {"Content-Type": "application/json"}
55
+ prompt = build_prompt(text, tones, platform, blog_link)
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  data = {
57
  "contents": [{"parts": [{"text": prompt}]}]
58
  }
 
73
  # ---------------------------
74
  # Gradio UI
75
  # ---------------------------
76
+ def app_interface(html_file, tones, platform_choice, api_key, blog_link):
77
  try:
78
  if not html_file:
79
  return "Please upload an HTML blog post."
 
92
  return "Couldn't extract content from file. Ensure it's a valid HTML blog post."
93
 
94
  print("[app_interface] Generating post...")
95
+ output = generate_post(api_key, main_text, tones, platform_choice, blog_link)
96
  return output
97
  except Exception as e:
98
  print(f"[app_interface] Error: {e}")
 
109
  tone_checkboxes = gr.CheckboxGroup(label="Choose Tones", choices=TONE_OPTIONS, value=["Friendly", "Humanized"])
110
  platform_radio = gr.Radio(label="Target Platform", choices=["Reddit", "Quora"], value="Reddit")
111
  api_key_input = gr.Textbox(label="Enter Your Gemini API Key", type="password")
112
+ blog_link_input = gr.Textbox(label="Enter Your Blog Link", placeholder="Insert your blog link here...")
113
  generate_button = gr.Button("Generate Post")
114
  output_box = gr.Textbox(label="Generated Post", lines=20, interactive=True)
115
 
116
+ generate_button.click(app_interface, inputs=[html_input, tone_checkboxes, platform_radio, api_key_input, blog_link_input], outputs=output_box)
117
 
118
  # ---------------------------
119
  # Launch App