diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..b6a5f1f --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,33 @@ +{ + "name": "Python 3", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye", + "customizations": { + "codespaces": { + "openFiles": [ + "README.md", + "TalkHeal.py" + ] + }, + "vscode": { + "settings": {}, + "extensions": [ + "ms-python.python", + "ms-python.vscode-pylance" + ] + } + }, + "updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y 30 else user_input active_convo["title"] = title with st.spinner("TalkHeal is thinking..."): try: - ai_response = get_ai_response(user_input.strip(), model) + def format_memory_for_prompt(convo_history, max_turns=10): + context = "" + for msg in convo_history[-max_turns*2:]: # user+bot per turn + sender = "User" if msg["sender"] == "user" else "Bot" + context += f"{sender}: {msg['message']}\n" + return context + + context = format_memory_for_prompt(active_convo["messages"]) + full_prompt = context + f"User: {user_input.strip()}\nBot:" + + ai_response = get_ai_response(full_prompt, model) active_convo["messages"].append({ "sender": "bot", @@ -123,4 +134,5 @@ def handle_chat_input(model): "message": "I apologise, but I'm having trouble responding right now. Please try again in a moment.", "time": get_current_time() }) + save_conversations(st.session_state.conversations) st.rerun() \ No newline at end of file diff --git a/components/sidebar.py b/components/sidebar.py index 7765a57..9123bed 100644 --- a/components/sidebar.py +++ b/components/sidebar.py @@ -128,6 +128,10 @@ def render_sidebar(): if col_confirm.button("Yes, delete", key="confirm_delete"): del st.session_state.conversations[st.session_state.delete_candidate] + + from core.utils import save_conversations + save_conversations(st.session_state.conversations) + del st.session_state.delete_candidate st.session_state.active_conversation = -1 st.rerun() diff --git a/core/__pycache__/__init__.cpython-312.pyc b/core/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..1c821dc Binary files /dev/null and b/core/__pycache__/__init__.cpython-312.pyc differ diff --git a/core/__pycache__/config.cpython-312.pyc b/core/__pycache__/config.cpython-312.pyc new file mode 100644 index 0000000..cd84c93 Binary files /dev/null and b/core/__pycache__/config.cpython-312.pyc differ diff --git a/core/__pycache__/utils.cpython-312.pyc b/core/__pycache__/utils.cpython-312.pyc new file mode 100644 index 0000000..fe8fb8e Binary files /dev/null and b/core/__pycache__/utils.cpython-312.pyc differ diff --git a/core/utils.py b/core/utils.py index b442f81..367fd76 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,6 +1,9 @@ from datetime import datetime, timedelta, timezone import streamlit as st import re +import json +import os +import requests def get_current_time(): """Returns the user's local time formatted as HH:MM AM/PM.""" @@ -80,4 +83,29 @@ def get_ai_response(user_message, model): cleaned_response = clean_ai_response(response.text) return cleaned_response except Exception as e: - return "I'm here to listen and support you. Sometimes I have trouble connecting, but I want you to know that your feelings are valid and you're not alone. Would you like to share more about what you're experiencing?" \ No newline at end of file + return "I'm here to listen and support you. Sometimes I have trouble connecting, but I want you to know that your feelings are valid and you're not alone. Would you like to share more about what you're experiencing?" + +#Implementing IP Based Isolation +def get_user_ip(): + try: + return requests.get("https://api.ipify.org").text + except: + return "unknown_ip" + +#Saving and loading to/from JSON File +def get_memory_file(): + ip = get_user_ip() + os.makedirs("data", exist_ok=True) + return f"data/conversations_{ip}.json" + +def save_conversations(conversations): + memory_file = get_memory_file() + with open(memory_file, 'w', encoding="utf-8") as f: + json.dump(conversations, f, indent=4) + +def load_conversations(): + memory_file = get_memory_file() + if not os.path.exists(memory_file): + return [] + with open(memory_file, 'r', encoding="utf-8") as f: + return json.load(f) \ No newline at end of file diff --git a/css/__pycache__/styles.cpython-312.pyc b/css/__pycache__/styles.cpython-312.pyc new file mode 100644 index 0000000..854eb24 Binary files /dev/null and b/css/__pycache__/styles.cpython-312.pyc differ