iamstrong commited on
Commit
1ffa361
ยท
verified ยท
1 Parent(s): a96ecda

Rename app.py to appFINAL.py

Browse files
Files changed (1) hide show
  1. 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 generate_reply(input_text):
354
- text = input_text.lower()
355
- for word in harmful_keywords:
356
- if word in text:
357
- return harmful_response
358
- for word, reply in emotions.items():
359
- if word in text:
360
- return reply
361
- return "Tell me more about your day."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- chat_history.append({"user": user_text, "bot": reply})
 
 
 
 
 
 
 
 
 
 
 
 
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(