maiurilorenzo commited on
Commit
7098220
·
verified ·
1 Parent(s): 32cb6a4

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. 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
- mime_type, _ = mimetypes.guess_type(file.name)
 
 
 
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
- # Sanitize file name
229
  original_name = os.path.basename(file.name)
230
- sanitized_name = re.sub(
231
- r"[^\w\-.]", "_", original_name
232
- ) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
233
-
234
- type_to_ext = {}
235
- for ext, t in mimetypes.types_map.items():
236
- if t not in type_to_ext:
237
- type_to_ext[t] = ext
238
-
239
- # Ensure the extension correlates to the mime type
240
- sanitized_name = sanitized_name.split(".")[:-1]
241
- sanitized_name.append("" + type_to_ext[mime_type])
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]