pentarosarium commited on
Commit
a79e62c
·
1 Parent(s): 5563747

fix gradio file reading

Browse files
Files changed (1) hide show
  1. app.py +27 -2
app.py CHANGED
@@ -823,10 +823,32 @@ def process_and_download(file_input, control=None):
823
  gr.Warning("Пожалуйста, загрузите файл")
824
  return pd.DataFrame(), None, None, None, "Ожидание файла...", ""
825
 
 
 
 
 
 
826
  # Convert file input to bytes
827
  try:
828
  if hasattr(file_input, 'name'): # Gradio 4+ NamedString object
829
- file_bytes = file_input.read() # Get bytes directly
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
830
  elif isinstance(file_input, (str, bytes)):
831
  # Either bytes or file path
832
  if isinstance(file_input, str):
@@ -836,8 +858,11 @@ def process_and_download(file_input, control=None):
836
  file_bytes = file_input
837
  else:
838
  raise ValueError(f"Unexpected file input type: {type(file_input)}")
 
 
 
839
  except Exception as e:
840
- gr.Warning(f"Ошибка чтения файла: {str(e)}")
841
  return pd.DataFrame(), None, None, None, f"Ошибка чтения файла: {str(e)}", ""
842
 
843
  if control is None:
 
823
  gr.Warning("Пожалуйста, загрузите файл")
824
  return pd.DataFrame(), None, None, None, "Ожидание файла...", ""
825
 
826
+ # Add debug logging
827
+ logger.info(f"File input type: {type(file_input)}")
828
+ if hasattr(file_input, '__dict__'):
829
+ logger.info(f"File input attributes: {dir(file_input)}")
830
+
831
  # Convert file input to bytes
832
  try:
833
  if hasattr(file_input, 'name'): # Gradio 4+ NamedString object
834
+ # Different ways to extract file content
835
+ if isinstance(file_input, str):
836
+ # If it's actually a file path
837
+ with open(file_input, 'rb') as f:
838
+ file_bytes = f.read()
839
+ elif hasattr(file_input, 'value'):
840
+ # Try accessing value attribute (common in Gradio 4+)
841
+ file_bytes = file_input.value
842
+ elif hasattr(file_input, 'content'):
843
+ # Try accessing content attribute
844
+ file_bytes = file_input.content
845
+ else:
846
+ # Last resort - convert to string and encode
847
+ file_bytes = str(file_input).encode('utf-8')
848
+ elif isinstance(file_input, dict) and 'path' in file_input:
849
+ # Gradio sometimes returns a dict with file path
850
+ with open(file_input['path'], 'rb') as f:
851
+ file_bytes = f.read()
852
  elif isinstance(file_input, (str, bytes)):
853
  # Either bytes or file path
854
  if isinstance(file_input, str):
 
858
  file_bytes = file_input
859
  else:
860
  raise ValueError(f"Unexpected file input type: {type(file_input)}")
861
+
862
+ logger.info(f"Successfully processed file, size: {len(file_bytes) if file_bytes else 0} bytes")
863
+
864
  except Exception as e:
865
+ logger.error(f"File reading error: {str(e)}", exc_info=True)
866
  return pd.DataFrame(), None, None, None, f"Ошибка чтения файла: {str(e)}", ""
867
 
868
  if control is None: