Srinivasulu kethanaboina commited on
Commit
23b9040
1 Parent(s): 2f95558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -34,6 +34,8 @@ os.makedirs(PERSIST_DIR, exist_ok=True)
34
 
35
  # Function to save chat history to cookies
36
  def save_chat_history_to_cookies(chat_id, query, response, cookies):
 
 
37
  history = cookies.get('chat_history', '[]')
38
  history_list = eval(history)
39
  history_list.append({
@@ -44,7 +46,7 @@ def save_chat_history_to_cookies(chat_id, query, response, cookies):
44
  })
45
  cookies['chat_history'] = str(history_list)
46
 
47
- def handle_query(query, cookies):
48
  chat_text_qa_msgs = [
49
  (
50
  "user",
@@ -64,9 +66,12 @@ def handle_query(query, cookies):
64
 
65
  # Use chat history to enhance response
66
  context_str = ""
67
- for past_query, response in reversed(cookies.get('chat_history', [])):
68
- if past_query.strip():
69
- context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
 
 
 
70
 
71
  query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
72
  answer = query_engine.query(query)
@@ -85,7 +90,8 @@ def handle_query(query, cookies):
85
  return response
86
 
87
  # Define your Gradio chat interface function
88
- def chat_interface(message, history, cookies):
 
89
  try:
90
  # Process the user message and generate a response
91
  response = handle_query(message, cookies)
 
34
 
35
  # Function to save chat history to cookies
36
  def save_chat_history_to_cookies(chat_id, query, response, cookies):
37
+ if cookies is None:
38
+ cookies = {}
39
  history = cookies.get('chat_history', '[]')
40
  history_list = eval(history)
41
  history_list.append({
 
46
  })
47
  cookies['chat_history'] = str(history_list)
48
 
49
+ def handle_query(query, cookies=None):
50
  chat_text_qa_msgs = [
51
  (
52
  "user",
 
66
 
67
  # Use chat history to enhance response
68
  context_str = ""
69
+ if cookies:
70
+ history = cookies.get('chat_history', '[]')
71
+ history_list = eval(history)
72
+ for entry in reversed(history_list):
73
+ if entry["query"].strip():
74
+ context_str += f"User asked: '{entry['query']}'\nBot answered: '{entry['response']}'\n"
75
 
76
  query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
77
  answer = query_engine.query(query)
 
90
  return response
91
 
92
  # Define your Gradio chat interface function
93
+ def chat_interface(message, history):
94
+ cookies = {} # You might need to get cookies from the request in a real implementation
95
  try:
96
  # Process the user message and generate a response
97
  response = handle_query(message, cookies)