Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import io
|
|
|
|
|
|
|
4 |
import shutil
|
5 |
import threading
|
6 |
from datetime import datetime
|
@@ -146,15 +149,36 @@ with gr.Blocks() as demo:
|
|
146 |
new_id = session_manager.create_session()
|
147 |
return new_id, []
|
148 |
|
149 |
-
def user(message,
|
150 |
-
if
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
if save_history:
|
160 |
session = session_manager.load_session(session_id)
|
@@ -176,7 +200,7 @@ with gr.Blocks() as demo:
|
|
176 |
|
177 |
with gr.Row():
|
178 |
msg = gr.Textbox(label="Message")
|
179 |
-
file_upload = gr.
|
180 |
|
181 |
msg.submit(user, [msg, file_upload, chatbot, session_id, save_history], [msg, chatbot]).then(
|
182 |
bot, [chatbot, session_id], [chatbot]
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import io
|
4 |
+
import pdfplumber
|
5 |
+
import docx
|
6 |
+
from openpyxl import load_workbook
|
7 |
import shutil
|
8 |
import threading
|
9 |
from datetime import datetime
|
|
|
149 |
new_id = session_manager.create_session()
|
150 |
return new_id, []
|
151 |
|
152 |
+
def user(message, files, history, session_id, save_history):
|
153 |
+
if files:
|
154 |
+
for file_path in files:
|
155 |
+
try:
|
156 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
157 |
+
file_content = ""
|
158 |
+
|
159 |
+
if file_extension == ".pdf":
|
160 |
+
with pdfplumber.open(file_path) as pdf:
|
161 |
+
for page in pdf.pages:
|
162 |
+
file_content += page.extract_text()
|
163 |
+
elif file_extension == ".docx":
|
164 |
+
doc = docx.Document(file_path)
|
165 |
+
for paragraph in doc.paragraphs:
|
166 |
+
file_content += paragraph.text + "\n"
|
167 |
+
elif file_extension == ".xlsx":
|
168 |
+
workbook = load_workbook(file_path)
|
169 |
+
for sheet in workbook.sheetnames:
|
170 |
+
worksheet = workbook[sheet]
|
171 |
+
for row in worksheet.iter_rows():
|
172 |
+
row_values = [str(cell.value) for cell in row]
|
173 |
+
file_content += ", ".join(row_values) + "\n"
|
174 |
+
else:
|
175 |
+
message += f"\nUnsupported file type: {file_extension}"
|
176 |
+
continue
|
177 |
+
|
178 |
+
message += f"\nFile content from {file_path}:\n{file_content}"
|
179 |
+
|
180 |
+
except Exception as e:
|
181 |
+
message += f"\nError processing {file_path}: {str(e)}"
|
182 |
|
183 |
if save_history:
|
184 |
session = session_manager.load_session(session_id)
|
|
|
200 |
|
201 |
with gr.Row():
|
202 |
msg = gr.Textbox(label="Message")
|
203 |
+
file_upload = gr.File(file_types=[".pdf", ".docx", ".xlsx"], file_count="multiple")
|
204 |
|
205 |
msg.submit(user, [msg, file_upload, chatbot, session_id, save_history], [msg, chatbot]).then(
|
206 |
bot, [chatbot, session_id], [chatbot]
|