hf-actions commited on
Commit
204674e
·
1 Parent(s): b42d9d1

feat: add SHOW_POST_LOG view to display log.txt in Gradio with Refresh/Clear

Browse files
Files changed (1) hide show
  1. app.py +72 -39
app.py CHANGED
@@ -44,45 +44,78 @@ def generate_and_optionally_post(topic: str, do_post: bool):
44
 
45
 
46
  with gr.Blocks() as demo:
47
- gr.Markdown("# Buddhism Wisdom Generator & Facebook Poster")
48
- with gr.Row():
49
- topic = gr.Textbox(label="Topic (optional)", placeholder="e.g. mindfulness, compassion")
50
- do_post = gr.Checkbox(label="Post to Facebook", value=False)
51
- with gr.Row():
52
- image_prompt = gr.Textbox(label="Image prompt (optional)", placeholder="Prompt for image generation")
53
- image_post = gr.Checkbox(label="Post image to Facebook", value=False)
54
- force_replicate = gr.Checkbox(label="Force Replicate", value=False)
55
- use_wisdom_for_image = gr.Checkbox(label="Use generated wisdom as image prompt", value=True)
56
- generate_btn = gr.Button("Generate & Post")
57
- output_text = gr.Textbox(label="Generated Wisdom", lines=3)
58
- status = gr.Textbox(label="Status", lines=2)
59
-
60
- img_btn = gr.Button("Generate Image & Post")
61
- img_output = gr.Image(label="Generated Image")
62
- img_status = gr.Textbox(label="Image Status", lines=2)
63
-
64
- generate_btn.click(fn=generate_and_optionally_post, inputs=[topic, do_post], outputs=[output_text, status])
65
- def _gen_img(prompt, do_post, use_wisdom, force_replicate):
66
- if not prompt and not use_wisdom:
67
- return None, "No image prompt provided and not using wisdom"
68
- try:
69
- provider_order = None
70
- if force_replicate:
71
- provider_order = "replicate,openai"
72
- res = generate_and_post(prompt, caption=None, post=do_post, use_wisdom_as_prompt=use_wisdom, provider_order=provider_order)
73
- img_path = res.get("image_path")
74
- wisdom = res.get("wisdom")
75
- fb = res.get("facebook")
76
- status_text = "Saved: " + img_path
77
- if wisdom:
78
- status_text += " | Wisdom used: " + (wisdom if len(wisdom) < 120 else wisdom[:117] + "...")
79
- if fb:
80
- status_text += " | Posted: " + str(fb)
81
- return img_path, status_text
82
- except Exception as e:
83
- return None, "Error: " + str(e)
84
-
85
- img_btn.click(fn=_gen_img, inputs=[image_prompt, image_post, use_wisdom_for_image, force_replicate], outputs=[img_output, img_status])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  if __name__ == "__main__":
88
  # Before launching, optionally start the daily Replicate job if enabled
 
44
 
45
 
46
  with gr.Blocks() as demo:
47
+ # If requested via env, show a simple log-only UI that displays `log.txt` entries
48
+ SHOW_LOG = os.getenv("SHOW_POST_LOG", "false").lower() in ("1", "true", "yes")
49
+ if SHOW_LOG:
50
+ gr.Markdown("# Posted Log")
51
+ log_box = gr.Textbox(label="Post Log (most recent first)", lines=20)
52
+ with gr.Row():
53
+ refresh_btn = gr.Button("Refresh")
54
+ clear_btn = gr.Button("Clear Log")
55
+
56
+ def read_log():
57
+ try:
58
+ if not os.path.exists("log.txt"):
59
+ return "(no log.txt present)"
60
+ with open("log.txt", "r", encoding="utf-8") as lf:
61
+ data = lf.read().strip()
62
+ # show most recent entries first for convenience
63
+ lines = data.splitlines()
64
+ return "\n".join(reversed(lines)) if lines else "(log empty)"
65
+ except Exception as e:
66
+ return f"Error reading log.txt: {e}"
67
+
68
+ def clear_log():
69
+ try:
70
+ open("log.txt", "w", encoding="utf-8").close()
71
+ return "(log cleared)"
72
+ except Exception as e:
73
+ return f"Error clearing log.txt: {e}"
74
+
75
+ refresh_btn.click(fn=read_log, inputs=[], outputs=[log_box])
76
+ clear_btn.click(fn=clear_log, inputs=[], outputs=[log_box])
77
+ # Populate initially
78
+ log_box.value = read_log()
79
+ else:
80
+ gr.Markdown("# Buddhism Wisdom Generator & Facebook Poster")
81
+ with gr.Row():
82
+ topic = gr.Textbox(label="Topic (optional)", placeholder="e.g. mindfulness, compassion")
83
+ do_post = gr.Checkbox(label="Post to Facebook", value=False)
84
+ with gr.Row():
85
+ image_prompt = gr.Textbox(label="Image prompt (optional)", placeholder="Prompt for image generation")
86
+ image_post = gr.Checkbox(label="Post image to Facebook", value=False)
87
+ force_replicate = gr.Checkbox(label="Force Replicate", value=False)
88
+ use_wisdom_for_image = gr.Checkbox(label="Use generated wisdom as image prompt", value=True)
89
+ generate_btn = gr.Button("Generate & Post")
90
+ output_text = gr.Textbox(label="Generated Wisdom", lines=3)
91
+ status = gr.Textbox(label="Status", lines=2)
92
+
93
+ img_btn = gr.Button("Generate Image & Post")
94
+ img_output = gr.Image(label="Generated Image")
95
+ img_status = gr.Textbox(label="Image Status", lines=2)
96
+
97
+ generate_btn.click(fn=generate_and_optionally_post, inputs=[topic, do_post], outputs=[output_text, status])
98
+ def _gen_img(prompt, do_post, use_wisdom, force_replicate):
99
+ if not prompt and not use_wisdom:
100
+ return None, "No image prompt provided and not using wisdom"
101
+ try:
102
+ provider_order = None
103
+ if force_replicate:
104
+ provider_order = "replicate,openai"
105
+ res = generate_and_post(prompt, caption=None, post=do_post, use_wisdom_as_prompt=use_wisdom, provider_order=provider_order)
106
+ img_path = res.get("image_path")
107
+ wisdom = res.get("wisdom")
108
+ fb = res.get("facebook")
109
+ status_text = "Saved: " + img_path
110
+ if wisdom:
111
+ status_text += " | Wisdom used: " + (wisdom if len(wisdom) < 120 else wisdom[:117] + "...")
112
+ if fb:
113
+ status_text += " | Posted: " + str(fb)
114
+ return img_path, status_text
115
+ except Exception as e:
116
+ return None, "Error: " + str(e)
117
+
118
+ img_btn.click(fn=_gen_img, inputs=[image_prompt, image_post, use_wisdom_for_image, force_replicate], outputs=[img_output, img_status])
119
 
120
  if __name__ == "__main__":
121
  # Before launching, optionally start the daily Replicate job if enabled