Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -26,20 +26,33 @@ image_extensions = Image.registered_extensions()
|
|
26 |
video_extensions = ("avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg", "wav", "gif", "webm", "m4v", "3gp")
|
27 |
|
28 |
|
29 |
-
def identify_and_save_blob(
|
30 |
"""Identifies if the blob is an image or video and saves it accordingly."""
|
31 |
try:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
|
45 |
@spaces.GPU
|
|
|
26 |
video_extensions = ("avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg", "wav", "gif", "webm", "m4v", "3gp")
|
27 |
|
28 |
|
29 |
+
def identify_and_save_blob(blob_path):
|
30 |
"""Identifies if the blob is an image or video and saves it accordingly."""
|
31 |
try:
|
32 |
+
with open(blob_path, 'rb') as file:
|
33 |
+
blob_content = file.read()
|
34 |
+
|
35 |
+
# Try to identify if it's an image
|
36 |
+
try:
|
37 |
+
Image.open(io.BytesIO(blob_content)).verify() # Check if it's a valid image
|
38 |
+
extension = ".png" # Default to PNG for saving
|
39 |
+
media_type = "image"
|
40 |
+
except (IOError, SyntaxError):
|
41 |
+
# If it's not a valid image, assume it's a video
|
42 |
+
extension = ".mp4" # Default to MP4 for saving
|
43 |
+
media_type = "video"
|
44 |
+
|
45 |
+
# Create a unique filename
|
46 |
+
filename = f"temp_{uuid.uuid4()}_media{extension}"
|
47 |
+
with open(filename, "wb") as f:
|
48 |
+
f.write(blob_content)
|
49 |
+
|
50 |
+
return filename, media_type
|
51 |
+
|
52 |
+
except FileNotFoundError:
|
53 |
+
raise ValueError(f"The file {blob_path} was not found.")
|
54 |
+
except Exception as e:
|
55 |
+
raise ValueError(f"An error occurred while processing the file: {e}")
|
56 |
|
57 |
|
58 |
@spaces.GPU
|