Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,18 +102,60 @@ def convert_file(file):
|
|
| 102 |
return "No file uploaded. Please select a file first.", None, None
|
| 103 |
|
| 104 |
try:
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
converter = MultiConverter()
|
| 107 |
-
filename = file.name
|
| 108 |
_, file_ext = os.path.splitext(filename)
|
| 109 |
file_ext = file_ext.lower()
|
| 110 |
-
|
| 111 |
logging.info(f"File extension: {file_ext}")
|
|
|
|
| 112 |
|
| 113 |
# Create a temporary file
|
| 114 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 115 |
-
file_content = file.read()
|
| 116 |
-
logging.info(f"Read {len(file_content)} bytes from input file")
|
| 117 |
temp_file.write(file_content)
|
| 118 |
temp_file_path = temp_file.name
|
| 119 |
logging.info(f"Created temporary file at: {temp_file_path}")
|
|
|
|
| 102 |
return "No file uploaded. Please select a file first.", None, None
|
| 103 |
|
| 104 |
try:
|
| 105 |
+
# Log file info
|
| 106 |
+
logging.info(f"File type: {type(file)}")
|
| 107 |
+
logging.info(f"File attributes: {dir(file) if hasattr(file, '__dir__') else 'No dir method'}")
|
| 108 |
+
|
| 109 |
+
# Wczesna obsługa obiektu NamedString
|
| 110 |
+
if str(type(file)).find('NamedString') > -1:
|
| 111 |
+
logging.info("Detected NamedString object")
|
| 112 |
+
# Traktuj plik jako ścieżkę, ale zachowaj informację o nazwie
|
| 113 |
+
filename = file.name if hasattr(file, 'name') else "unknown.file"
|
| 114 |
+
try:
|
| 115 |
+
with open(str(file), 'rb') as f:
|
| 116 |
+
file_content = f.read()
|
| 117 |
+
logging.info(f"Successfully read file content from NamedString as path")
|
| 118 |
+
except Exception as e:
|
| 119 |
+
error_msg = f"Could not read NamedString as path: {str(e)}"
|
| 120 |
+
logging.error(error_msg)
|
| 121 |
+
return error_msg, None, error_msg
|
| 122 |
+
else:
|
| 123 |
+
# Handle different types of file objects
|
| 124 |
+
if hasattr(file, 'name'):
|
| 125 |
+
filename = file.name
|
| 126 |
+
elif isinstance(file, tuple) and len(file) > 1:
|
| 127 |
+
filename = file[0] # In some versions, Gradio returns (filename, temppath)
|
| 128 |
+
else:
|
| 129 |
+
filename = "unknown_file"
|
| 130 |
+
logging.warning(f"Unknown file object type: {type(file)}")
|
| 131 |
+
|
| 132 |
+
logging.info(f"Converting file: {filename}")
|
| 133 |
+
|
| 134 |
+
# Handle different file object types
|
| 135 |
+
if hasattr(file, 'read'):
|
| 136 |
+
# File-like object with read method
|
| 137 |
+
file_content = file.read()
|
| 138 |
+
elif isinstance(file, tuple) and len(file) > 1 and os.path.exists(file[1]):
|
| 139 |
+
# Tuple with (name, path)
|
| 140 |
+
with open(file[1], 'rb') as f:
|
| 141 |
+
file_content = f.read()
|
| 142 |
+
elif isinstance(file, str) and os.path.exists(file):
|
| 143 |
+
# Direct path to file
|
| 144 |
+
with open(file, 'rb') as f:
|
| 145 |
+
file_content = f.read()
|
| 146 |
+
else:
|
| 147 |
+
error_msg = f"Could not read file content from object type: {type(file)}"
|
| 148 |
+
logging.error(error_msg)
|
| 149 |
+
return error_msg, None, error_msg
|
| 150 |
+
|
| 151 |
converter = MultiConverter()
|
|
|
|
| 152 |
_, file_ext = os.path.splitext(filename)
|
| 153 |
file_ext = file_ext.lower()
|
|
|
|
| 154 |
logging.info(f"File extension: {file_ext}")
|
| 155 |
+
logging.info(f"Read {len(file_content)} bytes from input file")
|
| 156 |
|
| 157 |
# Create a temporary file
|
| 158 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
|
|
|
|
|
|
| 159 |
temp_file.write(file_content)
|
| 160 |
temp_file_path = temp_file.name
|
| 161 |
logging.info(f"Created temporary file at: {temp_file_path}")
|