Spaces:
Sleeping
Sleeping
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
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 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|