KingNish commited on
Commit
e539fb0
·
verified ·
1 Parent(s): 393a49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
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(blob):
30
  """Identifies if the blob is an image or video and saves it accordingly."""
31
  try:
32
- Image.open(io.BytesIO(blob)).verify() # Check if it's a valid image
33
- extension = ".png"
34
- media_type = "image"
35
- except:
36
- extension = ".mp4"
37
- media_type = "video"
38
-
39
- filename = f"temp_{uuid.uuid4()}_media{extension}"
40
- with open(filename, "wb") as f:
41
- f.write(blob)
42
- return filename, media_type
 
 
 
 
 
 
 
 
 
 
 
 
 
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