Spaces:
Runtime error
Runtime error
File size: 1,802 Bytes
1a5ca7d 899ef09 1a5ca7d 899ef09 1a5ca7d 899ef09 1a5ca7d 899ef09 1a5ca7d 899ef09 1a5ca7d 899ef09 1a5ca7d 899ef09 1a5ca7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # utils/session_manager.py
import os
import json
from datetime import datetime
class SessionManager:
def __init__(self, base_path="/content/drive/MyDrive/MedMentor_Final_Year_Project/sessions"):
"""
Manages per-patient chat sessions.
Stores doctor-patient conversation history.
"""
self.base_path = base_path
os.makedirs(self.base_path, exist_ok=True)
self.current_patient = None
self.history = []
def start_session(self, patient_id):
"""Initialize new chat session for a patient."""
self.current_patient = patient_id
self.history = []
print(f"πΉ Started session for patient {patient_id}")
def add_message(self, role, message):
"""Add chat message with timestamp."""
if not self.current_patient:
raise ValueError("β οΈ No active patient session found!")
self.history.append({
"role": role,
"message": message.strip(),
"timestamp": datetime.now().isoformat()
})
def get_history(self):
"""Return full conversation for the active patient."""
return self.history
def end_session(self):
"""Save chat history to Drive/local path."""
if not self.current_patient:
print("β οΈ No active session to save.")
return
file_name = f"{self.current_patient}_session_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
file_path = os.path.join(self.base_path, file_name)
with open(file_path, "w", encoding="utf-8") as f:
json.dump(self.history, f, indent=2, ensure_ascii=False)
print(f"πΎ Session saved for {self.current_patient} β {file_path}")
self.current_patient = None
self.history = []
|