Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,40 +26,34 @@ import os
|
|
26 |
import io
|
27 |
|
28 |
def save_uploaded_file(file):
|
29 |
-
"""
|
30 |
-
Handles only file uploads, not URLs."""
|
31 |
|
32 |
if file is None:
|
33 |
return None
|
34 |
|
35 |
try:
|
36 |
temp_dir = tempfile.gettempdir()
|
37 |
-
temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file.
|
38 |
-
|
39 |
-
#
|
40 |
-
if hasattr(file, '
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
return f"Error during file write: {str(e)}"
|
57 |
-
|
58 |
-
else:
|
59 |
-
return "Error: Unsupported file type."
|
60 |
|
61 |
return temp_filename
|
62 |
-
|
63 |
except Exception as e:
|
64 |
return f"Error saving file: {str(e)}"
|
65 |
|
|
|
26 |
import io
|
27 |
|
28 |
def save_uploaded_file(file):
|
29 |
+
"""Saves a file uploaded with gardio to a temporary location."""
|
|
|
30 |
|
31 |
if file is None:
|
32 |
return None
|
33 |
|
34 |
try:
|
35 |
temp_dir = tempfile.gettempdir()
|
36 |
+
temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file.filename)[1])[1]
|
37 |
+
|
38 |
+
# Check if the file object is valid before attempting to read it
|
39 |
+
if not hasattr(file, 'filename') or not file.filename:
|
40 |
+
return "Error: Invalid file object. Missing filename."
|
41 |
+
|
42 |
+
|
43 |
+
with open(temp_filename, 'wb') as f:
|
44 |
+
try:
|
45 |
+
# Crucial: Check for a 'read' attribute to handle various file-like objects
|
46 |
+
if hasattr(file, 'read'):
|
47 |
+
for chunk in file.chunks():
|
48 |
+
f.write(chunk)
|
49 |
+
elif hasattr(file, 'getvalue'):
|
50 |
+
f.write(file.getvalue())
|
51 |
+
else:
|
52 |
+
return "Error: Unsupported file-like object. Missing 'read' or 'getvalue'."
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error writing to file: {str(e)}"
|
|
|
|
|
|
|
|
|
55 |
|
56 |
return temp_filename
|
|
|
57 |
except Exception as e:
|
58 |
return f"Error saving file: {str(e)}"
|
59 |
|