Spaces:
Running
Running
Update Gradio_UI.py
Browse files- Gradio_UI.py +20 -19
Gradio_UI.py
CHANGED
@@ -207,10 +207,12 @@ class GradioUI:
|
|
207 |
"application/pdf",
|
208 |
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
209 |
"text/plain",
|
|
|
|
|
210 |
],
|
211 |
):
|
212 |
"""
|
213 |
-
Handle file uploads, default allowed types are .pdf, .docx, and .txt
|
214 |
"""
|
215 |
import gradio as gr
|
216 |
|
@@ -218,31 +220,30 @@ class GradioUI:
|
|
218 |
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
219 |
|
220 |
try:
|
221 |
-
|
|
|
|
|
|
|
222 |
except Exception as e:
|
223 |
return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
|
224 |
|
225 |
if mime_type not in allowed_file_types:
|
226 |
return gr.Textbox("File type disallowed", visible=True), file_uploads_log
|
227 |
|
228 |
-
#
|
229 |
original_name = os.path.basename(file.name)
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
sanitized_name = "".join(sanitized_name)
|
243 |
-
|
244 |
-
# Save the uploaded file to the specified folder
|
245 |
-
file_path = os.path.join(self.file_upload_folder, os.path.basename(sanitized_name))
|
246 |
shutil.copy(file.name, file_path)
|
247 |
|
248 |
return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
|
|
|
207 |
"application/pdf",
|
208 |
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
209 |
"text/plain",
|
210 |
+
"text/csv",
|
211 |
+
"application/vnd.ms-excel"
|
212 |
],
|
213 |
):
|
214 |
"""
|
215 |
+
Handle file uploads, default allowed types are .pdf, .docx, .csv, .xlsx and .txt
|
216 |
"""
|
217 |
import gradio as gr
|
218 |
|
|
|
220 |
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
221 |
|
222 |
try:
|
223 |
+
if file.name.endswith(".csv"):
|
224 |
+
mime_type = "text/csv"
|
225 |
+
else:
|
226 |
+
mime_type, _ = mimetypes.guess_type(file.name)
|
227 |
except Exception as e:
|
228 |
return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
|
229 |
|
230 |
if mime_type not in allowed_file_types:
|
231 |
return gr.Textbox("File type disallowed", visible=True), file_uploads_log
|
232 |
|
233 |
+
# Extract name and extension safely
|
234 |
original_name = os.path.basename(file.name)
|
235 |
+
name, ext = os.path.splitext(original_name)
|
236 |
+
|
237 |
+
# Sanitize the filename (only alphanumeric, dash, and dot allowed)
|
238 |
+
sanitized_name = re.sub(r"[^\w\-.]", "_", name) + ext
|
239 |
+
|
240 |
+
# Ensure extension matches mime type
|
241 |
+
expected_ext = mimetypes.guess_extension(mime_type)
|
242 |
+
if expected_ext and ext.lower() != expected_ext:
|
243 |
+
sanitized_name = sanitized_name.replace(ext, expected_ext)
|
244 |
+
|
245 |
+
# Save the uploaded file
|
246 |
+
file_path = os.path.join(self.file_upload_folder, sanitized_name)
|
|
|
|
|
|
|
|
|
247 |
shutil.copy(file.name, file_path)
|
248 |
|
249 |
return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
|