Spaces:
Sleeping
Sleeping
add chat history
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import chainlit as cl
|
|
| 2 |
import pandas as pd
|
| 3 |
import time
|
| 4 |
from typing import Dict, Any
|
| 5 |
-
from chainlit.session import get_session
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
from datetime import datetime
|
|
@@ -22,15 +21,15 @@ HISTORY_DIR = "chat_history"
|
|
| 22 |
os.makedirs(HISTORY_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
# Function to get or create a user ID
|
| 25 |
-
def get_user_id(
|
| 26 |
# Check if user already has an ID
|
| 27 |
-
user_id =
|
| 28 |
|
| 29 |
if not user_id:
|
| 30 |
# Generate a new user ID if none exists
|
| 31 |
# In a real app, you might use authentication or cookies
|
| 32 |
user_id = str(uuid.uuid4())
|
| 33 |
-
|
| 34 |
|
| 35 |
# Create a user directory for this user
|
| 36 |
user_dir = os.path.join(HISTORY_DIR, user_id)
|
|
@@ -98,15 +97,12 @@ def load_chat_history(user_id, conversation_id=None):
|
|
| 98 |
|
| 99 |
@cl.on_chat_start
|
| 100 |
async def start():
|
| 101 |
-
# Get the current session
|
| 102 |
-
session = get_session()
|
| 103 |
-
|
| 104 |
# Get or create user ID
|
| 105 |
-
user_id = get_user_id(
|
| 106 |
|
| 107 |
# Create a new conversation ID for this session
|
| 108 |
conversation_id = str(uuid.uuid4())
|
| 109 |
-
|
| 110 |
|
| 111 |
# Load previous conversations for this user
|
| 112 |
conversations = load_chat_history(user_id)
|
|
@@ -130,7 +126,7 @@ async def start():
|
|
| 130 |
if res and res.get("value") != "new":
|
| 131 |
# User selected a previous conversation
|
| 132 |
selected_conv_id = res.get("value")
|
| 133 |
-
|
| 134 |
|
| 135 |
# Load the selected conversation
|
| 136 |
conversation = load_chat_history(user_id, selected_conv_id)
|
|
@@ -151,10 +147,9 @@ async def start():
|
|
| 151 |
|
| 152 |
@cl.on_message
|
| 153 |
async def on_message(message: cl.Message):
|
| 154 |
-
# Get
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
conversation_id = session.user_session.get("conversation_id")
|
| 158 |
|
| 159 |
# Process the message and generate a response
|
| 160 |
# Replace this with your actual processing logic
|
|
@@ -168,9 +163,8 @@ async def on_message(message: cl.Message):
|
|
| 168 |
|
| 169 |
@cl.action_callback("clear_history")
|
| 170 |
async def clear_history_callback(action):
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
conversation_id = session.user_session.get("conversation_id")
|
| 174 |
|
| 175 |
# Clear specific conversation or all conversations
|
| 176 |
user_dir = os.path.join(HISTORY_DIR, user_id)
|
|
@@ -182,7 +176,7 @@ async def clear_history_callback(action):
|
|
| 182 |
|
| 183 |
# Create a new conversation ID
|
| 184 |
new_conversation_id = str(uuid.uuid4())
|
| 185 |
-
|
| 186 |
|
| 187 |
@cl.on_message
|
| 188 |
async def on_message(message: cl.Message):
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import time
|
| 4 |
from typing import Dict, Any
|
|
|
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
from datetime import datetime
|
|
|
|
| 21 |
os.makedirs(HISTORY_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
# Function to get or create a user ID
|
| 24 |
+
def get_user_id():
|
| 25 |
# Check if user already has an ID
|
| 26 |
+
user_id = cl.user_session.get("user_id")
|
| 27 |
|
| 28 |
if not user_id:
|
| 29 |
# Generate a new user ID if none exists
|
| 30 |
# In a real app, you might use authentication or cookies
|
| 31 |
user_id = str(uuid.uuid4())
|
| 32 |
+
cl.user_session.set("user_id", user_id)
|
| 33 |
|
| 34 |
# Create a user directory for this user
|
| 35 |
user_dir = os.path.join(HISTORY_DIR, user_id)
|
|
|
|
| 97 |
|
| 98 |
@cl.on_chat_start
|
| 99 |
async def start():
|
|
|
|
|
|
|
|
|
|
| 100 |
# Get or create user ID
|
| 101 |
+
user_id = get_user_id()
|
| 102 |
|
| 103 |
# Create a new conversation ID for this session
|
| 104 |
conversation_id = str(uuid.uuid4())
|
| 105 |
+
cl.user_session.set("conversation_id", conversation_id)
|
| 106 |
|
| 107 |
# Load previous conversations for this user
|
| 108 |
conversations = load_chat_history(user_id)
|
|
|
|
| 126 |
if res and res.get("value") != "new":
|
| 127 |
# User selected a previous conversation
|
| 128 |
selected_conv_id = res.get("value")
|
| 129 |
+
cl.user_session.set("conversation_id", selected_conv_id)
|
| 130 |
|
| 131 |
# Load the selected conversation
|
| 132 |
conversation = load_chat_history(user_id, selected_conv_id)
|
|
|
|
| 147 |
|
| 148 |
@cl.on_message
|
| 149 |
async def on_message(message: cl.Message):
|
| 150 |
+
# Get user and conversation IDs
|
| 151 |
+
user_id = cl.user_session.get("user_id")
|
| 152 |
+
conversation_id = cl.user_session.get("conversation_id")
|
|
|
|
| 153 |
|
| 154 |
# Process the message and generate a response
|
| 155 |
# Replace this with your actual processing logic
|
|
|
|
| 163 |
|
| 164 |
@cl.action_callback("clear_history")
|
| 165 |
async def clear_history_callback(action):
|
| 166 |
+
user_id = cl.user_session.get("user_id")
|
| 167 |
+
conversation_id = cl.user_session.get("conversation_id")
|
|
|
|
| 168 |
|
| 169 |
# Clear specific conversation or all conversations
|
| 170 |
user_dir = os.path.join(HISTORY_DIR, user_id)
|
|
|
|
| 176 |
|
| 177 |
# Create a new conversation ID
|
| 178 |
new_conversation_id = str(uuid.uuid4())
|
| 179 |
+
cl.user_session.set("conversation_id", new_conversation_id)
|
| 180 |
|
| 181 |
@cl.on_message
|
| 182 |
async def on_message(message: cl.Message):
|