Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import logging
|
3 |
from typing import List, Dict
|
4 |
import torch
|
@@ -27,17 +28,19 @@ class DocumentManager:
|
|
27 |
|
28 |
def __init__(self):
|
29 |
self.upload_folder = UPLOAD_FOLDER
|
|
|
|
|
30 |
os.makedirs(self.upload_folder, exist_ok=True)
|
31 |
self.max_files = 5
|
32 |
self.max_file_size = 10 * 1024 * 1024 # 10 MB
|
33 |
self.supported_formats = ['.pdf', '.txt', '.docx']
|
34 |
self.documents = []
|
35 |
|
36 |
-
def validate_file(self,
|
37 |
-
if
|
38 |
raise ValueError(f"File size exceeds {self.max_file_size // 1024 // 1024}MB limit")
|
39 |
|
40 |
-
ext = os.path.splitext(
|
41 |
if ext not in self.supported_formats:
|
42 |
raise ValueError(f"Unsupported file format. Supported formats: {', '.join(self.supported_formats)}")
|
43 |
|
@@ -65,22 +68,34 @@ class DocumentManager:
|
|
65 |
logger.error(f"Error loading {file_path}: {str(e)}")
|
66 |
raise
|
67 |
|
68 |
-
def process_upload(self, files: List) -> str:
|
69 |
-
if
|
70 |
-
|
|
|
|
|
|
|
|
|
71 |
|
72 |
processed_files = []
|
73 |
for file in files:
|
74 |
try:
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
docs = self.load_document(save_path)
|
79 |
self.documents.extend(docs)
|
80 |
-
processed_files.append(
|
|
|
81 |
except Exception as e:
|
82 |
-
logger.error(f"Error processing {
|
83 |
-
return f"Error processing {
|
84 |
|
85 |
return f"Successfully processed files: {', '.join(processed_files)}"
|
86 |
|
@@ -203,7 +218,7 @@ def process_file_upload(files):
|
|
203 |
"""Handle file uploads and system initialization."""
|
204 |
try:
|
205 |
upload_result = rag_system.document_manager.process_upload(files)
|
206 |
-
if "Error" in upload_result:
|
207 |
return upload_result
|
208 |
|
209 |
init_result = rag_system.initialize_system(rag_system.document_manager.documents)
|
@@ -231,7 +246,16 @@ def process_query(message, history):
|
|
231 |
return history + [(message, f"Error: {str(e)}")]
|
232 |
|
233 |
# Create Gradio interface
|
234 |
-
demo = gr.Blocks(css="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
with demo:
|
237 |
gr.HTML("""
|
@@ -244,29 +268,42 @@ with demo:
|
|
244 |
""")
|
245 |
|
246 |
with gr.Row():
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
gr.HTML("""
|
272 |
<div style="text-align: center; max-width: 800px; margin: 20px auto; padding: 20px;
|
|
|
1 |
import os
|
2 |
+
import shutil
|
3 |
import logging
|
4 |
from typing import List, Dict
|
5 |
import torch
|
|
|
28 |
|
29 |
def __init__(self):
|
30 |
self.upload_folder = UPLOAD_FOLDER
|
31 |
+
if os.path.exists(self.upload_folder):
|
32 |
+
shutil.rmtree(self.upload_folder)
|
33 |
os.makedirs(self.upload_folder, exist_ok=True)
|
34 |
self.max_files = 5
|
35 |
self.max_file_size = 10 * 1024 * 1024 # 10 MB
|
36 |
self.supported_formats = ['.pdf', '.txt', '.docx']
|
37 |
self.documents = []
|
38 |
|
39 |
+
def validate_file(self, file_path, file_size):
|
40 |
+
if file_size > self.max_file_size:
|
41 |
raise ValueError(f"File size exceeds {self.max_file_size // 1024 // 1024}MB limit")
|
42 |
|
43 |
+
ext = os.path.splitext(file_path)[1].lower()
|
44 |
if ext not in self.supported_formats:
|
45 |
raise ValueError(f"Unsupported file format. Supported formats: {', '.join(self.supported_formats)}")
|
46 |
|
|
|
68 |
logger.error(f"Error loading {file_path}: {str(e)}")
|
69 |
raise
|
70 |
|
71 |
+
def process_upload(self, files: List[gr.File]) -> str:
|
72 |
+
if not files:
|
73 |
+
return "No files uploaded"
|
74 |
+
|
75 |
+
current_files = len(os.listdir(self.upload_folder))
|
76 |
+
if current_files + len(files) > self.max_files:
|
77 |
+
return f"Maximum number of documents ({self.max_files}) exceeded"
|
78 |
|
79 |
processed_files = []
|
80 |
for file in files:
|
81 |
try:
|
82 |
+
file_path = file.name
|
83 |
+
file_size = os.path.getsize(file_path)
|
84 |
+
|
85 |
+
self.validate_file(file_path, file_size)
|
86 |
+
|
87 |
+
# Copy file to upload folder
|
88 |
+
filename = os.path.basename(file_path)
|
89 |
+
save_path = os.path.join(self.upload_folder, filename)
|
90 |
+
shutil.copy2(file_path, save_path)
|
91 |
+
|
92 |
docs = self.load_document(save_path)
|
93 |
self.documents.extend(docs)
|
94 |
+
processed_files.append(filename)
|
95 |
+
|
96 |
except Exception as e:
|
97 |
+
logger.error(f"Error processing {file_path}: {str(e)}")
|
98 |
+
return f"Error processing {os.path.basename(file_path)}: {str(e)}"
|
99 |
|
100 |
return f"Successfully processed files: {', '.join(processed_files)}"
|
101 |
|
|
|
218 |
"""Handle file uploads and system initialization."""
|
219 |
try:
|
220 |
upload_result = rag_system.document_manager.process_upload(files)
|
221 |
+
if "Error" in upload_result or "Maximum" in upload_result:
|
222 |
return upload_result
|
223 |
|
224 |
init_result = rag_system.initialize_system(rag_system.document_manager.documents)
|
|
|
246 |
return history + [(message, f"Error: {str(e)}")]
|
247 |
|
248 |
# Create Gradio interface
|
249 |
+
demo = gr.Blocks(css="""
|
250 |
+
div.gradio-container {background-color: #f0f2f6}
|
251 |
+
div.upload-box {
|
252 |
+
border: 2px dashed #ccc;
|
253 |
+
padding: 20px;
|
254 |
+
border-radius: 10px;
|
255 |
+
background-color: #ffffff;
|
256 |
+
margin-bottom: 15px;
|
257 |
+
}
|
258 |
+
""")
|
259 |
|
260 |
with demo:
|
261 |
gr.HTML("""
|
|
|
268 |
""")
|
269 |
|
270 |
with gr.Row():
|
271 |
+
# Sidebar for document upload
|
272 |
+
with gr.Column(scale=1):
|
273 |
+
with gr.Box(elem_classes="upload-box"):
|
274 |
+
gr.HTML("<h3>📁 Document Upload</h3>")
|
275 |
+
file_output = gr.File(
|
276 |
+
file_count="multiple",
|
277 |
+
label="Upload Documents (PDF, TXT, DOCX)",
|
278 |
+
elem_classes="file-upload"
|
279 |
+
)
|
280 |
+
gr.HTML("""
|
281 |
+
<div style="font-size: 0.8em; color: #666;">
|
282 |
+
<p>• Maximum 5 files</p>
|
283 |
+
<p>• 10MB per file</p>
|
284 |
+
<p>• Supported formats: PDF, TXT, DOCX</p>
|
285 |
+
</div>
|
286 |
+
""")
|
287 |
+
upload_button = gr.Button("📤 Upload and Initialize", variant="primary")
|
288 |
+
system_output = gr.Textbox(label="System Status", interactive=False)
|
289 |
+
|
290 |
+
# Main chat area
|
291 |
+
with gr.Column(scale=3):
|
292 |
+
chatbot = gr.Chatbot(
|
293 |
+
show_label=False,
|
294 |
+
container=True,
|
295 |
+
height=600,
|
296 |
+
show_copy_button=True
|
297 |
+
)
|
298 |
+
|
299 |
+
with gr.Row():
|
300 |
+
message = gr.Textbox(
|
301 |
+
placeholder="Ask a question about your documents...",
|
302 |
+
show_label=False,
|
303 |
+
container=False,
|
304 |
+
scale=8
|
305 |
+
)
|
306 |
+
clear = gr.Button("🗑️ Clear", size="sm", scale=1)
|
307 |
|
308 |
gr.HTML("""
|
309 |
<div style="text-align: center; max-width: 800px; margin: 20px auto; padding: 20px;
|