Spaces:
Sleeping
Sleeping
fixed tuple issue
Browse files
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 = [
|
| 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(
|
| 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()
|
| 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 (
|
| 118 |
chat_history = chat_history or []
|
| 119 |
-
chat_history.append(
|
| 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 |
-
#
|
| 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],
|