Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# ================================
|
| 2 |
# πͺ MoodMirror+ β Text Emotion β’ Advice-only + brief intros & reasons
|
| 3 |
-
# - Tabs: Advice β’ Emergency numbers β’ Breathing β’ Journal (
|
| 4 |
-
# - Gradio + sklearn compatibility fixes
|
| 5 |
# ================================
|
| 6 |
import os
|
| 7 |
import re
|
|
@@ -10,6 +10,7 @@ import sqlite3
|
|
| 10 |
import joblib
|
| 11 |
import numpy as np
|
| 12 |
import time
|
|
|
|
| 13 |
from datetime import datetime
|
| 14 |
|
| 15 |
import gradio as gr
|
|
@@ -216,6 +217,27 @@ def journal_export_txt(entry_id: int):
|
|
| 216 |
f.write("\n".join(lines))
|
| 217 |
return fpath, f"Ready: {fname}"
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
# ---------------- Model ----------------
|
| 220 |
def load_goemotions_dataset():
|
| 221 |
ds = load_dataset("google-research-datasets/go_emotions", "simplified")
|
|
@@ -390,9 +412,9 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 390 |
yield "β
Done. Notice how your body feels."
|
| 391 |
start_btn.click(run_breathing, inputs=[pattern, cycles], outputs=[breathe_out])
|
| 392 |
|
| 393 |
-
# ---- Tab 4: Journal (
|
| 394 |
with gr.Tab("Journal"):
|
| 395 |
-
gr.Markdown("#### π Personal journal\nWrite freely
|
| 396 |
|
| 397 |
with gr.Row():
|
| 398 |
j_title = gr.Textbox(label="Title (optional)", placeholder="e.g., A tough day at work")
|
|
@@ -414,13 +436,16 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 414 |
j_table = gr.Dataframe(headers=["UTC time","Emotion","Title","Preview"], value=[], interactive=False)
|
| 415 |
j_view = gr.Markdown()
|
| 416 |
|
| 417 |
-
# Export + Delete UI
|
| 418 |
with gr.Row():
|
| 419 |
-
j_export_btn = gr.Button("Export .txt")
|
| 420 |
j_export_file = gr.File(label="Download exported entry", visible=True)
|
| 421 |
j_delete_btn = gr.Button("Delete entry", variant="stop")
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
-
# Backend
|
| 424 |
def _refresh_entries(search):
|
| 425 |
options, table = journal_list(search or "", 50)
|
| 426 |
return gr.Dropdown(choices=options, value=None), table
|
|
@@ -456,6 +481,10 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 456 |
drop, table = _refresh_entries(search)
|
| 457 |
return status, drop, table, ""
|
| 458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
# Wire actions
|
| 460 |
j_save.click(_save_entry, inputs=[j_title, j_text, j_emotion, j_search],
|
| 461 |
outputs=[j_status, j_entries, j_table, j_text, j_title])
|
|
@@ -466,6 +495,7 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 466 |
j_export_btn.click(_export_entry, inputs=[j_entries], outputs=[j_export_file, j_status])
|
| 467 |
j_delete_btn.click(_delete_entry, inputs=[j_entries, j_search],
|
| 468 |
outputs=[j_status, j_entries, j_table, j_view])
|
|
|
|
| 469 |
|
| 470 |
if __name__ == "__main__":
|
| 471 |
demo.queue()
|
|
|
|
| 1 |
# ================================
|
| 2 |
# πͺ MoodMirror+ β Text Emotion β’ Advice-only + brief intros & reasons
|
| 3 |
+
# - Tabs: Advice β’ Emergency numbers β’ Breathing β’ Journal (export selected/delete/export all)
|
| 4 |
+
# - Gradio + sklearn compatibility fixes
|
| 5 |
# ================================
|
| 6 |
import os
|
| 7 |
import re
|
|
|
|
| 10 |
import joblib
|
| 11 |
import numpy as np
|
| 12 |
import time
|
| 13 |
+
import zipfile
|
| 14 |
from datetime import datetime
|
| 15 |
|
| 16 |
import gradio as gr
|
|
|
|
| 217 |
f.write("\n".join(lines))
|
| 218 |
return fpath, f"Ready: {fname}"
|
| 219 |
|
| 220 |
+
def journal_export_all_zip():
|
| 221 |
+
conn = get_conn()
|
| 222 |
+
rows = list(conn.execute("SELECT id, ts, emotion, title, content FROM journal ORDER BY ts"))
|
| 223 |
+
conn.close()
|
| 224 |
+
if not rows:
|
| 225 |
+
return None, "No entries to export."
|
| 226 |
+
zip_name = os.path.join(DATA_DIR, "journal_all.zip")
|
| 227 |
+
with zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 228 |
+
for (id_, ts, emo, title, content) in rows:
|
| 229 |
+
safe_title = re.sub(r"[^a-zA-Z0-9_\- ]", "_", title or "untitled")
|
| 230 |
+
fname = f"{ts[:19].replace(':','-')} - {safe_title}.txt"
|
| 231 |
+
text = (
|
| 232 |
+
f"Title: {title or '(Untitled)'}\n"
|
| 233 |
+
f"Emotion: {emo or '-'}\n"
|
| 234 |
+
f"Saved (UTC): {ts}\n"
|
| 235 |
+
f"{'-'*40}\n"
|
| 236 |
+
f"{content or ''}"
|
| 237 |
+
)
|
| 238 |
+
zf.writestr(fname, text)
|
| 239 |
+
return zip_name, f"Exported {len(rows)} entries."
|
| 240 |
+
|
| 241 |
# ---------------- Model ----------------
|
| 242 |
def load_goemotions_dataset():
|
| 243 |
ds = load_dataset("google-research-datasets/go_emotions", "simplified")
|
|
|
|
| 412 |
yield "β
Done. Notice how your body feels."
|
| 413 |
start_btn.click(run_breathing, inputs=[pattern, cycles], outputs=[breathe_out])
|
| 414 |
|
| 415 |
+
# ---- Tab 4: Journal (export selected/delete/export all) ----
|
| 416 |
with gr.Tab("Journal"):
|
| 417 |
+
gr.Markdown("#### π Personal journal\nWrite freely, then save. Entries are stored locally (SQLite).")
|
| 418 |
|
| 419 |
with gr.Row():
|
| 420 |
j_title = gr.Textbox(label="Title (optional)", placeholder="e.g., A tough day at work")
|
|
|
|
| 436 |
j_table = gr.Dataframe(headers=["UTC time","Emotion","Title","Preview"], value=[], interactive=False)
|
| 437 |
j_view = gr.Markdown()
|
| 438 |
|
| 439 |
+
# Export + Delete + Export all UI
|
| 440 |
with gr.Row():
|
| 441 |
+
j_export_btn = gr.Button("Export selected .txt")
|
| 442 |
j_export_file = gr.File(label="Download exported entry", visible=True)
|
| 443 |
j_delete_btn = gr.Button("Delete entry", variant="stop")
|
| 444 |
+
with gr.Row():
|
| 445 |
+
j_export_all_btn = gr.Button("β¬οΈ Export all entries (.zip)")
|
| 446 |
+
j_export_all_file = gr.File(label="Download all entries", visible=True)
|
| 447 |
|
| 448 |
+
# --------------- Backend ---------------
|
| 449 |
def _refresh_entries(search):
|
| 450 |
options, table = journal_list(search or "", 50)
|
| 451 |
return gr.Dropdown(choices=options, value=None), table
|
|
|
|
| 481 |
drop, table = _refresh_entries(search)
|
| 482 |
return status, drop, table, ""
|
| 483 |
|
| 484 |
+
def _export_all():
|
| 485 |
+
path, msg = journal_export_all_zip()
|
| 486 |
+
return path, msg
|
| 487 |
+
|
| 488 |
# Wire actions
|
| 489 |
j_save.click(_save_entry, inputs=[j_title, j_text, j_emotion, j_search],
|
| 490 |
outputs=[j_status, j_entries, j_table, j_text, j_title])
|
|
|
|
| 495 |
j_export_btn.click(_export_entry, inputs=[j_entries], outputs=[j_export_file, j_status])
|
| 496 |
j_delete_btn.click(_delete_entry, inputs=[j_entries, j_search],
|
| 497 |
outputs=[j_status, j_entries, j_table, j_view])
|
| 498 |
+
j_export_all_btn.click(_export_all, outputs=[j_export_all_file, j_status])
|
| 499 |
|
| 500 |
if __name__ == "__main__":
|
| 501 |
demo.queue()
|