Spaces:
Runtime error
Runtime error
| # 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 = [] | |