Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,7 +96,7 @@ class DocumentAIBot:
|
|
| 96 |
# Create vector store
|
| 97 |
vector_store = FAISS.from_documents(chunks, self.embedding_model)
|
| 98 |
return vector_store
|
| 99 |
-
|
| 100 |
def setup_retrieval_chain(self, vector_store):
|
| 101 |
"""Set up the retrieval chain with the vector store."""
|
| 102 |
retriever = vector_store.as_retriever(
|
|
@@ -161,35 +161,19 @@ def generate_session_id():
|
|
| 161 |
import uuid
|
| 162 |
return str(uuid.uuid4())
|
| 163 |
|
| 164 |
-
def
|
| 165 |
-
"""Save uploaded file to a temporary location and return the path."""
|
| 166 |
-
temp_dir = tempfile.gettempdir()
|
| 167 |
-
temp_path = os.path.join(temp_dir, os.path.basename(file.name))
|
| 168 |
-
|
| 169 |
-
with open(temp_path, "wb") as f:
|
| 170 |
-
f.write(file.read())
|
| 171 |
-
|
| 172 |
-
return temp_path
|
| 173 |
-
|
| 174 |
-
def clear_conversation():
|
| 175 |
-
"""Clear the conversation history for the current session."""
|
| 176 |
-
global conversation_history, current_session_id
|
| 177 |
-
|
| 178 |
-
if current_session_id and current_session_id in conversation_history:
|
| 179 |
-
conversation_history[current_session_id] = []
|
| 180 |
-
|
| 181 |
-
return [], f"Conversation cleared. You can continue asking questions about '{current_document_name}'."
|
| 182 |
-
|
| 183 |
-
def process_uploaded_document(file):
|
| 184 |
"""Process an uploaded document and set up the session."""
|
| 185 |
global current_session_id, current_document_store, current_document_name, conversation_history
|
| 186 |
|
| 187 |
try:
|
| 188 |
-
if
|
| 189 |
return None, "Please upload a document first."
|
| 190 |
|
| 191 |
-
#
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
# Create document AI bot if not already created
|
| 195 |
if not hasattr(process_uploaded_document, "bot"):
|
|
@@ -205,24 +189,33 @@ def process_uploaded_document(file):
|
|
| 205 |
# Update global variables
|
| 206 |
current_session_id = session_id
|
| 207 |
current_document_store = vector_store
|
| 208 |
-
current_document_name =
|
| 209 |
|
| 210 |
-
return [], f"Document '{
|
| 211 |
|
| 212 |
except Exception as e:
|
| 213 |
import traceback
|
| 214 |
traceback.print_exc()
|
| 215 |
return None, f"Error processing document: {str(e)}"
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
def answer_question(question, history):
|
| 218 |
"""Answer a question about the current document."""
|
| 219 |
global current_session_id, current_document_store, conversation_history
|
| 220 |
|
| 221 |
if not current_document_store:
|
| 222 |
-
return "Please upload a document first."
|
| 223 |
|
| 224 |
if not hasattr(process_uploaded_document, "bot"):
|
| 225 |
-
return "Document AI bot not initialized. Please reload the page and try again."
|
| 226 |
|
| 227 |
try:
|
| 228 |
# Get current chat history
|
|
@@ -261,7 +254,7 @@ def build_interface():
|
|
| 261 |
file_input = gr.File(
|
| 262 |
label="Upload Document",
|
| 263 |
file_types=[".pdf", ".txt", ".docx", ".pptx"],
|
| 264 |
-
type="filepath" #
|
| 265 |
)
|
| 266 |
|
| 267 |
upload_button = gr.Button("Process Document", variant="primary")
|
|
|
|
| 96 |
# Create vector store
|
| 97 |
vector_store = FAISS.from_documents(chunks, self.embedding_model)
|
| 98 |
return vector_store
|
| 99 |
+
|
| 100 |
def setup_retrieval_chain(self, vector_store):
|
| 101 |
"""Set up the retrieval chain with the vector store."""
|
| 102 |
retriever = vector_store.as_retriever(
|
|
|
|
| 161 |
import uuid
|
| 162 |
return str(uuid.uuid4())
|
| 163 |
|
| 164 |
+
def process_uploaded_document(file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
"""Process an uploaded document and set up the session."""
|
| 166 |
global current_session_id, current_document_store, current_document_name, conversation_history
|
| 167 |
|
| 168 |
try:
|
| 169 |
+
if file_path is None:
|
| 170 |
return None, "Please upload a document first."
|
| 171 |
|
| 172 |
+
# In newer Gradio versions, the file input with type="filepath" returns the path directly
|
| 173 |
+
# No need to save the file as it's already saved by Gradio
|
| 174 |
+
|
| 175 |
+
# Extract filename for display
|
| 176 |
+
filename = os.path.basename(file_path)
|
| 177 |
|
| 178 |
# Create document AI bot if not already created
|
| 179 |
if not hasattr(process_uploaded_document, "bot"):
|
|
|
|
| 189 |
# Update global variables
|
| 190 |
current_session_id = session_id
|
| 191 |
current_document_store = vector_store
|
| 192 |
+
current_document_name = filename
|
| 193 |
|
| 194 |
+
return [], f"Document '{filename}' processed successfully. You can now ask questions about it."
|
| 195 |
|
| 196 |
except Exception as e:
|
| 197 |
import traceback
|
| 198 |
traceback.print_exc()
|
| 199 |
return None, f"Error processing document: {str(e)}"
|
| 200 |
|
| 201 |
+
def clear_conversation():
|
| 202 |
+
"""Clear the conversation history for the current session."""
|
| 203 |
+
global conversation_history, current_session_id
|
| 204 |
+
|
| 205 |
+
if current_session_id and current_session_id in conversation_history:
|
| 206 |
+
conversation_history[current_session_id] = []
|
| 207 |
+
|
| 208 |
+
return [], f"Conversation cleared. You can continue asking questions about '{current_document_name}'."
|
| 209 |
+
|
| 210 |
def answer_question(question, history):
|
| 211 |
"""Answer a question about the current document."""
|
| 212 |
global current_session_id, current_document_store, conversation_history
|
| 213 |
|
| 214 |
if not current_document_store:
|
| 215 |
+
return "", history + [(question, "Please upload a document first.")]
|
| 216 |
|
| 217 |
if not hasattr(process_uploaded_document, "bot"):
|
| 218 |
+
return "", history + [(question, "Document AI bot not initialized. Please reload the page and try again.")]
|
| 219 |
|
| 220 |
try:
|
| 221 |
# Get current chat history
|
|
|
|
| 254 |
file_input = gr.File(
|
| 255 |
label="Upload Document",
|
| 256 |
file_types=[".pdf", ".txt", ".docx", ".pptx"],
|
| 257 |
+
type="filepath" # This returns the file path directly
|
| 258 |
)
|
| 259 |
|
| 260 |
upload_button = gr.Button("Process Document", variant="primary")
|