Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# ================================
|
| 2 |
# πͺ MoodMirror+ β Text Emotion β’ Advice-only + brief intros & reasons
|
| 3 |
# - Tabs: Advice β’ Emergency numbers β’ Breathing β’ Journal (with export & delete)
|
|
|
|
| 4 |
# ================================
|
| 5 |
import os
|
| 6 |
import re
|
|
@@ -204,7 +205,6 @@ def journal_export_txt(entry_id: int):
|
|
| 204 |
return None, "Entry not found."
|
| 205 |
fname = f"journal_{entry_id}_{data['ts'].replace(':','-')}.txt"
|
| 206 |
fpath = os.path.join(DATA_DIR, fname)
|
| 207 |
-
# Build plain text
|
| 208 |
lines = []
|
| 209 |
title = data["title"] or "(Untitled)"
|
| 210 |
lines.append(f"Title: {title}")
|
|
@@ -232,7 +232,17 @@ def train_or_load_model():
|
|
| 232 |
Y_train = mlb.fit_transform(y_train)
|
| 233 |
clf = Pipeline([
|
| 234 |
("tfidf", TfidfVectorizer(lowercase=True, ngram_range=(1,2), min_df=2, max_df=0.9, strip_accents="unicode")),
|
| 235 |
-
("ovr", OneVsRestClassifier(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
])
|
| 237 |
clf.fit(X_train, Y_train)
|
| 238 |
joblib.dump({"version": MODEL_VERSION, "pipeline": clf, "mlb": mlb, "label_names": names}, MODEL_PATH)
|
|
@@ -313,7 +323,7 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 313 |
with gr.Row():
|
| 314 |
country = gr.Dropdown(list(CRISIS_NUMBERS_EN.keys()), value="United States", label="Country")
|
| 315 |
save_ok = gr.Checkbox(False, label="Save anonymized session")
|
| 316 |
-
chat = gr.Chatbot(height=380)
|
| 317 |
msg = gr.Textbox(label="Your message", placeholder="Share how you feel...")
|
| 318 |
with gr.Row():
|
| 319 |
send = gr.Button("Send", variant="primary")
|
|
@@ -401,7 +411,7 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 401 |
j_search = gr.Textbox(label="Search (title, text or emotion)", placeholder="e.g., anxiety, work, joy")
|
| 402 |
j_refresh = gr.Button("Refresh list")
|
| 403 |
j_entries = gr.Dropdown(label="Entries (newest first)", choices=[], value=None, interactive=True)
|
| 404 |
-
j_table = gr.Dataframe(headers=["UTC time","Emotion","Title","Preview"], value=[], interactive=False
|
| 405 |
j_view = gr.Markdown()
|
| 406 |
|
| 407 |
# Export + Delete UI
|
|
@@ -458,6 +468,5 @@ with gr.Blocks(title="πͺ MoodMirror+ β Text Emotion β’ Advice-only") as dem
|
|
| 458 |
outputs=[j_status, j_entries, j_table, j_view])
|
| 459 |
|
| 460 |
if __name__ == "__main__":
|
| 461 |
-
gr.set_theme("default")
|
| 462 |
demo.queue()
|
| 463 |
demo.launch()
|
|
|
|
| 1 |
# ================================
|
| 2 |
# πͺ MoodMirror+ β Text Emotion β’ Advice-only + brief intros & reasons
|
| 3 |
# - Tabs: Advice β’ Emergency numbers β’ Breathing β’ Journal (with export & delete)
|
| 4 |
+
# - Gradio + sklearn compatibility fixes (no Dataframe height/wrap, Chatbot type, LR hyperparams)
|
| 5 |
# ================================
|
| 6 |
import os
|
| 7 |
import re
|
|
|
|
| 205 |
return None, "Entry not found."
|
| 206 |
fname = f"journal_{entry_id}_{data['ts'].replace(':','-')}.txt"
|
| 207 |
fpath = os.path.join(DATA_DIR, fname)
|
|
|
|
| 208 |
lines = []
|
| 209 |
title = data["title"] or "(Untitled)"
|
| 210 |
lines.append(f"Title: {title}")
|
|
|
|
| 232 |
Y_train = mlb.fit_transform(y_train)
|
| 233 |
clf = Pipeline([
|
| 234 |
("tfidf", TfidfVectorizer(lowercase=True, ngram_range=(1,2), min_df=2, max_df=0.9, strip_accents="unicode")),
|
| 235 |
+
("ovr", OneVsRestClassifier(
|
| 236 |
+
LogisticRegression(
|
| 237 |
+
solver="saga",
|
| 238 |
+
penalty="l2",
|
| 239 |
+
C=0.5, # stronger regularization to help convergence
|
| 240 |
+
tol=1e-3, # slightly looser tolerance
|
| 241 |
+
max_iter=5000, # more iterations
|
| 242 |
+
class_weight="balanced"
|
| 243 |
+
),
|
| 244 |
+
n_jobs=-1
|
| 245 |
+
))
|
| 246 |
])
|
| 247 |
clf.fit(X_train, Y_train)
|
| 248 |
joblib.dump({"version": MODEL_VERSION, "pipeline": clf, "mlb": mlb, "label_names": names}, MODEL_PATH)
|
|
|
|
| 323 |
with gr.Row():
|
| 324 |
country = gr.Dropdown(list(CRISIS_NUMBERS_EN.keys()), value="United States", label="Country")
|
| 325 |
save_ok = gr.Checkbox(False, label="Save anonymized session")
|
| 326 |
+
chat = gr.Chatbot(type="tuples", height=380)
|
| 327 |
msg = gr.Textbox(label="Your message", placeholder="Share how you feel...")
|
| 328 |
with gr.Row():
|
| 329 |
send = gr.Button("Send", variant="primary")
|
|
|
|
| 411 |
j_search = gr.Textbox(label="Search (title, text or emotion)", placeholder="e.g., anxiety, work, joy")
|
| 412 |
j_refresh = gr.Button("Refresh list")
|
| 413 |
j_entries = gr.Dropdown(label="Entries (newest first)", choices=[], value=None, interactive=True)
|
| 414 |
+
j_table = gr.Dataframe(headers=["UTC time","Emotion","Title","Preview"], value=[], interactive=False)
|
| 415 |
j_view = gr.Markdown()
|
| 416 |
|
| 417 |
# Export + Delete UI
|
|
|
|
| 468 |
outputs=[j_status, j_entries, j_table, j_view])
|
| 469 |
|
| 470 |
if __name__ == "__main__":
|
|
|
|
| 471 |
demo.queue()
|
| 472 |
demo.launch()
|