Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -164,7 +164,7 @@ def generate_session_id():
|
|
| 164 |
def save_uploaded_file(file):
|
| 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, file.name)
|
| 168 |
|
| 169 |
with open(temp_path, "wb") as f:
|
| 170 |
f.write(file.read())
|
|
@@ -205,9 +205,9 @@ 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 = file.name
|
| 209 |
|
| 210 |
-
return [], f"Document '{file.name}' processed successfully. You can now ask questions about it."
|
| 211 |
|
| 212 |
except Exception as e:
|
| 213 |
import traceback
|
|
@@ -219,10 +219,10 @@ def answer_question(question, history):
|
|
| 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
|
|
@@ -239,12 +239,14 @@ def answer_question(question, history):
|
|
| 239 |
# Update conversation history
|
| 240 |
conversation_history[current_session_id] = updated_history
|
| 241 |
|
| 242 |
-
|
|
|
|
|
|
|
| 243 |
|
| 244 |
except Exception as e:
|
| 245 |
import traceback
|
| 246 |
traceback.print_exc()
|
| 247 |
-
return f"Error generating answer: {str(e)}"
|
| 248 |
|
| 249 |
def build_interface():
|
| 250 |
"""Build and launch the Gradio interface."""
|
|
@@ -259,7 +261,7 @@ def build_interface():
|
|
| 259 |
file_input = gr.File(
|
| 260 |
label="Upload Document",
|
| 261 |
file_types=[".pdf", ".txt", ".docx", ".pptx"],
|
| 262 |
-
type="file"
|
| 263 |
)
|
| 264 |
|
| 265 |
upload_button = gr.Button("Process Document", variant="primary")
|
|
@@ -289,8 +291,6 @@ def build_interface():
|
|
| 289 |
label="Ask a question about the document",
|
| 290 |
placeholder="What is the main topic of this document?",
|
| 291 |
lines=2,
|
| 292 |
-
max_lines=5,
|
| 293 |
-
interactive=True,
|
| 294 |
show_label=True
|
| 295 |
)
|
| 296 |
|
|
@@ -306,21 +306,13 @@ def build_interface():
|
|
| 306 |
submit_button.click(
|
| 307 |
answer_question,
|
| 308 |
inputs=[question_input, chatbot],
|
| 309 |
-
outputs=chatbot
|
| 310 |
-
).then(
|
| 311 |
-
lambda: "",
|
| 312 |
-
None,
|
| 313 |
-
question_input
|
| 314 |
)
|
| 315 |
|
| 316 |
question_input.submit(
|
| 317 |
answer_question,
|
| 318 |
inputs=[question_input, chatbot],
|
| 319 |
-
outputs=chatbot
|
| 320 |
-
).then(
|
| 321 |
-
lambda: "",
|
| 322 |
-
None,
|
| 323 |
-
question_input
|
| 324 |
)
|
| 325 |
|
| 326 |
clear_button.click(
|
|
@@ -328,16 +320,6 @@ def build_interface():
|
|
| 328 |
inputs=[],
|
| 329 |
outputs=[chatbot, upload_status]
|
| 330 |
)
|
| 331 |
-
|
| 332 |
-
# Add CSS for better styling
|
| 333 |
-
interface.load(
|
| 334 |
-
js="""
|
| 335 |
-
() => {
|
| 336 |
-
document.querySelector('body').style.backgroundColor = '#f7f7f7';
|
| 337 |
-
document.querySelector('.gradio-container').style.maxWidth = '1200px';
|
| 338 |
-
}
|
| 339 |
-
"""
|
| 340 |
-
)
|
| 341 |
|
| 342 |
return interface
|
| 343 |
|
|
@@ -345,9 +327,7 @@ def build_interface():
|
|
| 345 |
if __name__ == "__main__":
|
| 346 |
demo = build_interface()
|
| 347 |
demo.launch(
|
| 348 |
-
share=True,
|
| 349 |
server_name="0.0.0.0",
|
| 350 |
server_port=7860,
|
| 351 |
-
|
| 352 |
-
show_api=False
|
| 353 |
)
|
|
|
|
| 164 |
def save_uploaded_file(file):
|
| 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())
|
|
|
|
| 205 |
# Update global variables
|
| 206 |
current_session_id = session_id
|
| 207 |
current_document_store = vector_store
|
| 208 |
+
current_document_name = os.path.basename(file.name)
|
| 209 |
|
| 210 |
+
return [], f"Document '{os.path.basename(file.name)}' processed successfully. You can now ask questions about it."
|
| 211 |
|
| 212 |
except Exception as e:
|
| 213 |
import traceback
|
|
|
|
| 219 |
global current_session_id, current_document_store, conversation_history
|
| 220 |
|
| 221 |
if not current_document_store:
|
| 222 |
+
return "Please upload a document first.", history
|
| 223 |
|
| 224 |
if not hasattr(process_uploaded_document, "bot"):
|
| 225 |
+
return "Document AI bot not initialized. Please reload the page and try again.", history
|
| 226 |
|
| 227 |
try:
|
| 228 |
# Get current chat history
|
|
|
|
| 239 |
# Update conversation history
|
| 240 |
conversation_history[current_session_id] = updated_history
|
| 241 |
|
| 242 |
+
# Update the display history
|
| 243 |
+
history = history + [(question, answer)]
|
| 244 |
+
return "", history
|
| 245 |
|
| 246 |
except Exception as e:
|
| 247 |
import traceback
|
| 248 |
traceback.print_exc()
|
| 249 |
+
return "", history + [(question, f"Error generating answer: {str(e)}")]
|
| 250 |
|
| 251 |
def build_interface():
|
| 252 |
"""Build and launch the Gradio interface."""
|
|
|
|
| 261 |
file_input = gr.File(
|
| 262 |
label="Upload Document",
|
| 263 |
file_types=[".pdf", ".txt", ".docx", ".pptx"],
|
| 264 |
+
type="filepath" # Changed from "file" to "filepath"
|
| 265 |
)
|
| 266 |
|
| 267 |
upload_button = gr.Button("Process Document", variant="primary")
|
|
|
|
| 291 |
label="Ask a question about the document",
|
| 292 |
placeholder="What is the main topic of this document?",
|
| 293 |
lines=2,
|
|
|
|
|
|
|
| 294 |
show_label=True
|
| 295 |
)
|
| 296 |
|
|
|
|
| 306 |
submit_button.click(
|
| 307 |
answer_question,
|
| 308 |
inputs=[question_input, chatbot],
|
| 309 |
+
outputs=[question_input, chatbot]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
)
|
| 311 |
|
| 312 |
question_input.submit(
|
| 313 |
answer_question,
|
| 314 |
inputs=[question_input, chatbot],
|
| 315 |
+
outputs=[question_input, chatbot]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
)
|
| 317 |
|
| 318 |
clear_button.click(
|
|
|
|
| 320 |
inputs=[],
|
| 321 |
outputs=[chatbot, upload_status]
|
| 322 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
return interface
|
| 325 |
|
|
|
|
| 327 |
if __name__ == "__main__":
|
| 328 |
demo = build_interface()
|
| 329 |
demo.launch(
|
|
|
|
| 330 |
server_name="0.0.0.0",
|
| 331 |
server_port=7860,
|
| 332 |
+
share=False
|
|
|
|
| 333 |
)
|