Spaces:
Sleeping
Sleeping
Rename app.py to appFINAL.py
Browse files- app.py โ appFINAL.py +127 -11
app.py โ appFINAL.py
RENAMED
|
@@ -3,6 +3,27 @@ import tempfile
|
|
| 3 |
import json
|
| 4 |
import datetime
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# User data
|
| 8 |
user_info = {"name": "", "age": "", "gender": "", "language": "english" , "Guardian_info": ""}
|
|
@@ -340,7 +361,42 @@ emotions = {
|
|
| 340 |
"mental struggle" : "Write a small gratitude note.",
|
| 341 |
"mind fatigue" : "Do light stretches or walk for 5 minutes."
|
| 342 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
# Functions
|
| 345 |
def set_personal_info(name, age, gender, language, Guardian_info):
|
| 346 |
user_info.update({"name": name, "age": age, "gender": gender, "language": language, "Guardian_info": Guardian_info,})
|
|
@@ -350,33 +406,80 @@ def show_personal_data():
|
|
| 350 |
today = datetime.date.today().strftime("%Y-%m-%d (%A)")
|
| 351 |
return f"๐
{today}\n๐ค Name: {user_info['name']}\n๐ Age: {user_info['age']}\nโ Gender: {user_info['gender']}\n๐ Language: {user_info['language']}\n Guardian_info: {user_info['Guardian_info']}"
|
| 352 |
|
| 353 |
-
def
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
def chat_function(audio_input, text_input):
|
| 364 |
user_text = text_input.strip()
|
|
|
|
| 365 |
if not user_text:
|
| 366 |
return "Please type something.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
reply = generate_reply(user_text)
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
lang_code = lang_codes.get(user_info["language"], "en")
|
| 370 |
tts = gTTS(reply, lang=lang_code)
|
|
|
|
| 371 |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 372 |
tts.save(audio_file.name)
|
|
|
|
| 373 |
return reply, audio_file.name
|
| 374 |
|
| 375 |
def get_chat_history():
|
| 376 |
if not chat_history:
|
| 377 |
return "No conversations yet."
|
| 378 |
return "\n\n".join([f"You: {c['user']}\nBot: {c['bot']}" for c in chat_history])
|
| 379 |
-
|
| 380 |
def save_journal(entry):
|
| 381 |
journal_entries.append(entry)
|
| 382 |
with open("journal.json", "w") as f:
|
|
@@ -494,7 +597,20 @@ with gr.Blocks() as app:
|
|
| 494 |
|
| 495 |
with gr.Tab("9๏ธโฃ Pomodoro"):
|
| 496 |
gr.Markdown("โฑ๏ธ Use 25 min study + 5 min break cycles.\n(For real timer, use front-end JavaScript or Android timers)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
|
|
|
|
| 498 |
with gr.Tab("๐ Games"):
|
| 499 |
gr.Markdown("๐ฎ Play from the game portal below:")
|
| 500 |
gr.HTML(
|
|
|
|
| 3 |
import json
|
| 4 |
import datetime
|
| 5 |
import gradio as gr
|
| 6 |
+
# ---------- MEMORY FILES ----------
|
| 7 |
+
CHAT_FILE = "chat_history.json"
|
| 8 |
+
EVENT_FILE = "event_memory.json"
|
| 9 |
+
REPORT_FILE = "reports.json"
|
| 10 |
+
|
| 11 |
+
# ---------- LOAD & SAVE ----------
|
| 12 |
+
def load_json(file):
|
| 13 |
+
try:
|
| 14 |
+
with open(file, "r") as f:
|
| 15 |
+
return json.load(f)
|
| 16 |
+
except:
|
| 17 |
+
return []
|
| 18 |
+
|
| 19 |
+
def save_json(file, data):
|
| 20 |
+
with open(file, "w") as f:
|
| 21 |
+
json.dump(data, f, indent=4)
|
| 22 |
+
|
| 23 |
+
# Load memory
|
| 24 |
+
chat_history = load_json(CHAT_FILE)
|
| 25 |
+
event_memory = load_json(EVENT_FILE)
|
| 26 |
+
reports = load_json(REPORT_FILE)
|
| 27 |
|
| 28 |
# User data
|
| 29 |
user_info = {"name": "", "age": "", "gender": "", "language": "english" , "Guardian_info": ""}
|
|
|
|
| 361 |
"mental struggle" : "Write a small gratitude note.",
|
| 362 |
"mind fatigue" : "Do light stretches or walk for 5 minutes."
|
| 363 |
}
|
| 364 |
+
import datetime
|
| 365 |
+
|
| 366 |
+
def extract_event(user_text):
|
| 367 |
+
import random
|
| 368 |
+
|
| 369 |
+
def get_emotional_response(emotion):
|
| 370 |
+
responses = {
|
| 371 |
+
"sad": ["I'm really sorry you're feeling this way ๐", "That sounds heavyโฆ I'm here for you"],
|
| 372 |
+
"anxiety": ["That sounds stressful ๐ฟ", "Letโs slow things down together"],
|
| 373 |
+
"angry": ["I can sense your frustration", "Itโs okay to feel angry"],
|
| 374 |
+
"lonely": ["Youโre not alone ๐ค", "Iโm here with you"]
|
| 375 |
+
}
|
| 376 |
+
return random.choice(responses.get(emotion, ["I understand."]))
|
| 377 |
+
|
| 378 |
|
| 379 |
+
def humanize_reply(base_reply):
|
| 380 |
+
starters = ["I hear you.", "Iโm really glad you shared that.", "Iโm here with you."]
|
| 381 |
+
empathy = ["That must feel tough.", "I understand how that can feel."]
|
| 382 |
+
follow = ["Do you want to tell me more?", "Iโm listening ๐"]
|
| 383 |
+
|
| 384 |
+
return f"{random.choice(starters)}\n{random.choice(empathy)}\n\n{base_reply}\n\n{random.choice(follow)}"
|
| 385 |
+
text = user_text.lower()
|
| 386 |
+
keywords = ["exam", "test", "fight", "friend", "family", "school", "stress"]
|
| 387 |
+
|
| 388 |
+
for word in keywords:
|
| 389 |
+
if word in text:
|
| 390 |
+
event = {
|
| 391 |
+
"event": user_text,
|
| 392 |
+
"keyword": word,
|
| 393 |
+
"time": str(datetime.datetime.now())
|
| 394 |
+
}
|
| 395 |
+
event_memory.append(event)
|
| 396 |
+
save_json(EVENT_FILE, event_memory)
|
| 397 |
+
return event
|
| 398 |
+
return None
|
| 399 |
+
|
| 400 |
# Functions
|
| 401 |
def set_personal_info(name, age, gender, language, Guardian_info):
|
| 402 |
user_info.update({"name": name, "age": age, "gender": gender, "language": language, "Guardian_info": Guardian_info,})
|
|
|
|
| 406 |
today = datetime.date.today().strftime("%Y-%m-%d (%A)")
|
| 407 |
return f"๐
{today}\n๐ค Name: {user_info['name']}\n๐ Age: {user_info['age']}\nโ Gender: {user_info['gender']}\n๐ Language: {user_info['language']}\n Guardian_info: {user_info['Guardian_info']}"
|
| 408 |
|
| 409 |
+
def generate_report():
|
| 410 |
+
today = str(datetime.date.today())
|
| 411 |
+
|
| 412 |
+
emotions_seen = []
|
| 413 |
+
topics = []
|
| 414 |
+
|
| 415 |
+
for chat in chat_history[-20:]:
|
| 416 |
+
text = chat["user"].lower()
|
| 417 |
+
|
| 418 |
+
for word in emotions:
|
| 419 |
+
if word in text:
|
| 420 |
+
emotions_seen.append(word)
|
| 421 |
+
|
| 422 |
+
if "exam" in text:
|
| 423 |
+
topics.append("studies")
|
| 424 |
+
if "friend" in text:
|
| 425 |
+
topics.append("friends")
|
| 426 |
+
if "family" in text:
|
| 427 |
+
topics.append("family")
|
| 428 |
+
|
| 429 |
+
summary = f"๐ง Report - {today}\n\n"
|
| 430 |
+
|
| 431 |
+
if emotions_seen:
|
| 432 |
+
summary += f"๐ญ Emotions: {', '.join(set(emotions_seen))}\n"
|
| 433 |
+
else:
|
| 434 |
+
summary += "๐ญ Emotions: Stable\n"
|
| 435 |
+
|
| 436 |
+
if topics:
|
| 437 |
+
summary += f"๐ Focus: {topics[-1]}\n"
|
| 438 |
+
|
| 439 |
+
summary += "\n๐ก Suggestion: Take care of your mental health ๐"
|
| 440 |
+
|
| 441 |
+
reports.append({"date": today, "report": summary})
|
| 442 |
+
save_json(REPORT_FILE, reports)
|
| 443 |
+
|
| 444 |
+
return summary
|
| 445 |
|
| 446 |
def chat_function(audio_input, text_input):
|
| 447 |
user_text = text_input.strip()
|
| 448 |
+
|
| 449 |
if not user_text:
|
| 450 |
return "Please type something.", None
|
| 451 |
+
|
| 452 |
+
# ๐ง Extract event
|
| 453 |
+
extract_event(user_text)
|
| 454 |
+
|
| 455 |
+
# ๐ง Generate reply
|
| 456 |
reply = generate_reply(user_text)
|
| 457 |
+
|
| 458 |
+
# ๐พ Save chat
|
| 459 |
+
chat_history.append({
|
| 460 |
+
"user": user_text,
|
| 461 |
+
"bot": reply,
|
| 462 |
+
"time": str(datetime.datetime.now())
|
| 463 |
+
})
|
| 464 |
+
save_json(CHAT_FILE, chat_history)
|
| 465 |
+
|
| 466 |
+
# ๐ Update report
|
| 467 |
+
generate_report()
|
| 468 |
+
|
| 469 |
+
# ๐ Voice
|
| 470 |
lang_code = lang_codes.get(user_info["language"], "en")
|
| 471 |
tts = gTTS(reply, lang=lang_code)
|
| 472 |
+
|
| 473 |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 474 |
tts.save(audio_file.name)
|
| 475 |
+
|
| 476 |
return reply, audio_file.name
|
| 477 |
|
| 478 |
def get_chat_history():
|
| 479 |
if not chat_history:
|
| 480 |
return "No conversations yet."
|
| 481 |
return "\n\n".join([f"You: {c['user']}\nBot: {c['bot']}" for c in chat_history])
|
| 482 |
+
|
| 483 |
def save_journal(entry):
|
| 484 |
journal_entries.append(entry)
|
| 485 |
with open("journal.json", "w") as f:
|
|
|
|
| 597 |
|
| 598 |
with gr.Tab("9๏ธโฃ Pomodoro"):
|
| 599 |
gr.Markdown("โฑ๏ธ Use 25 min study + 5 min break cycles.\n(For real timer, use front-end JavaScript or Android timers)")
|
| 600 |
+
|
| 601 |
+
with gr.Tab("๐ Daily Report"):
|
| 602 |
+
report_btn = gr.Button("Generate Report")
|
| 603 |
+
report_box = gr.Textbox(lines=10)
|
| 604 |
+
|
| 605 |
+
history_btn = gr.Button("View Past Reports")
|
| 606 |
+
history_box = gr.Textbox(lines=15)
|
| 607 |
+
|
| 608 |
+
report_btn.click(generate_report, outputs=report_box)
|
| 609 |
+
|
| 610 |
+
def show_reports():
|
| 611 |
+
return "\n\n---\n\n".join([r["report"] for r in reports])
|
| 612 |
|
| 613 |
+
history_btn.click(show_reports, outputs=history_box)
|
| 614 |
with gr.Tab("๐ Games"):
|
| 615 |
gr.Markdown("๐ฎ Play from the game portal below:")
|
| 616 |
gr.HTML(
|