PraneshJs commited on
Commit
b66f579
·
verified ·
1 Parent(s): 98d82d2

fixed tuple issue

Browse files
Files changed (1) hide show
  1. app.py +6 -7
app.py CHANGED
@@ -54,14 +54,14 @@ def process_pdf(pdf_file):
54
  db = FAISS.from_texts(chunks, embeddings)
55
  db.save_local(session_path)
56
 
57
- chat_history = [{"role": "system", "content": "Paper uploaded and processed. You can now ask questions."}]
58
  return f"Paper uploaded successfully. Session ID: {session_id}", session_id, chat_history
59
 
60
  # === QUERY FUNCTION ===
61
  def query_paper(session_id, user_message, chat_history):
62
  if not session_id or not os.path.exists(os.path.join(STORAGE_DIR, session_id)):
63
  chat_history = chat_history or []
64
- chat_history.append({"role": "system", "content": "Session expired or not found. Upload the paper again."})
65
  return chat_history, ""
66
 
67
  if not user_message.strip():
@@ -72,7 +72,7 @@ def query_paper(session_id, user_message, chat_history):
72
  db = FAISS.load_local(session_path, embeddings, allow_dangerous_deserialization=True)
73
  retriever = db.as_retriever(search_kwargs={"k": 3})
74
 
75
- # Use invoke() method instead of deprecated get_relevant_documents
76
  docs = retriever.invoke(user_message)
77
  context = "\n\n".join([d.page_content for d in docs])
78
 
@@ -114,10 +114,9 @@ def query_paper(session_id, user_message, chat_history):
114
  except Exception as e:
115
  answer = f"Error: {str(e)}"
116
 
117
- # Update chat history (Gradio expects list of dicts with role + content)
118
  chat_history = chat_history or []
119
- chat_history.append({"role": "user", "content": user_message})
120
- chat_history.append({"role": "assistant", "content": answer})
121
 
122
  return chat_history, ""
123
 
@@ -187,7 +186,7 @@ with gr.Blocks() as demo:
187
  outputs=[chatbot, state_chat]
188
  )
189
 
190
- # Update chatbot display when chat history changes
191
  state_chat.change(
192
  lambda chat: chat,
193
  inputs=[state_chat],
 
54
  db = FAISS.from_texts(chunks, embeddings)
55
  db.save_local(session_path)
56
 
57
+ chat_history = [("System", "Paper uploaded and processed. You can now ask questions.")]
58
  return f"Paper uploaded successfully. Session ID: {session_id}", session_id, chat_history
59
 
60
  # === QUERY FUNCTION ===
61
  def query_paper(session_id, user_message, chat_history):
62
  if not session_id or not os.path.exists(os.path.join(STORAGE_DIR, session_id)):
63
  chat_history = chat_history or []
64
+ chat_history.append(("System", "Session expired or not found. Upload the paper again."))
65
  return chat_history, ""
66
 
67
  if not user_message.strip():
 
72
  db = FAISS.load_local(session_path, embeddings, allow_dangerous_deserialization=True)
73
  retriever = db.as_retriever(search_kwargs={"k": 3})
74
 
75
+ # Use invoke() instead of deprecated method
76
  docs = retriever.invoke(user_message)
77
  context = "\n\n".join([d.page_content for d in docs])
78
 
 
114
  except Exception as e:
115
  answer = f"Error: {str(e)}"
116
 
117
+ # Update chat history (tuple format)
118
  chat_history = chat_history or []
119
+ chat_history.append((user_message, answer))
 
120
 
121
  return chat_history, ""
122
 
 
186
  outputs=[chatbot, state_chat]
187
  )
188
 
189
+ # Sync chat state with chatbot
190
  state_chat.change(
191
  lambda chat: chat,
192
  inputs=[state_chat],