Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
import os, io, re, sys, subprocess, hashlib, pathlib, time
|
| 3 |
from typing import Optional
|
| 4 |
import requests
|
| 5 |
-
from PIL import Image, ImageSequence
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
MODEL_DIR = pathlib.Path("model")
|
|
@@ -52,11 +52,24 @@ def mp4_to_gif(mp4_bytes: bytes) -> bytes:
|
|
| 52 |
r2 = requests.get(gif_url, timeout=60); r2.raise_for_status(); return r2.content
|
| 53 |
|
| 54 |
def load_first_frame(raw: bytes):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
img =
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def rebuild_llama_cpp():
|
| 62 |
env = os.environ.copy()
|
|
@@ -120,19 +133,28 @@ def generate_caption_from_url(url: str, prompt: str="Describe the image."):
|
|
| 120 |
raw = mp4_to_gif(raw)
|
| 121 |
except Exception as e:
|
| 122 |
return "MP4→GIF conversion failed: " + str(e)
|
| 123 |
-
img = load_first_frame(raw)
|
| 124 |
-
except
|
| 125 |
return "Image processing error: " + str(e)
|
|
|
|
|
|
|
| 126 |
|
|
|
|
| 127 |
try:
|
| 128 |
img = img.resize((512,512), resample=Image.BICUBIC)
|
| 129 |
-
except Exception:
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# create a tiny base64 tag to signal image presence (model must understand this format)
|
| 133 |
import base64
|
| 134 |
buf = io.BytesIO()
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
b64 = base64.b64encode(buf.getvalue()).decode()
|
| 137 |
img_tag = b64 # minimal
|
| 138 |
|
|
@@ -155,4 +177,4 @@ iface = gr.Interface(
|
|
| 155 |
)
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 2 |
import os, io, re, sys, subprocess, hashlib, pathlib, time
|
| 3 |
from typing import Optional
|
| 4 |
import requests
|
| 5 |
+
from PIL import Image, ImageSequence, UnidentifiedImageError # Import UnidentifiedImageError
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
MODEL_DIR = pathlib.Path("model")
|
|
|
|
| 52 |
r2 = requests.get(gif_url, timeout=60); r2.raise_for_status(); return r2.content
|
| 53 |
|
| 54 |
def load_first_frame(raw: bytes):
|
| 55 |
+
# Added specific handling for PIL errors
|
| 56 |
+
try:
|
| 57 |
+
img = Image.open(io.BytesIO(raw))
|
| 58 |
+
if getattr(img, "is_animated", False):
|
| 59 |
+
# Also wrap ImageSequence.Iterator in case of corrupted animated images
|
| 60 |
+
try:
|
| 61 |
+
img = next(ImageSequence.Iterator(img))
|
| 62 |
+
except Exception as e:
|
| 63 |
+
raise ValueError(f"Could not extract first frame from animated image: {e}")
|
| 64 |
+
if img.mode != "RGB":
|
| 65 |
+
img = img.convert("RGB")
|
| 66 |
+
return img
|
| 67 |
+
except UnidentifiedImageError:
|
| 68 |
+
raise ValueError("Could not identify image format or image is corrupted.")
|
| 69 |
+
except Exception as e:
|
| 70 |
+
# Catch other PIL errors (e.g., IOErrors during parsing)
|
| 71 |
+
raise ValueError(f"Failed to load or process image with PIL: {e}")
|
| 72 |
+
|
| 73 |
|
| 74 |
def rebuild_llama_cpp():
|
| 75 |
env = os.environ.copy()
|
|
|
|
| 133 |
raw = mp4_to_gif(raw)
|
| 134 |
except Exception as e:
|
| 135 |
return "MP4→GIF conversion failed: " + str(e)
|
| 136 |
+
img = load_first_frame(raw) # This function now has specific error handling
|
| 137 |
+
except ValueError as e: # Catch the specific ValueError raised by load_first_frame
|
| 138 |
return "Image processing error: " + str(e)
|
| 139 |
+
except Exception as e: # General fallback for other unexpected image issues
|
| 140 |
+
return "An unexpected image processing error occurred: " + str(e)
|
| 141 |
|
| 142 |
+
# Added try-except for image resizing as well
|
| 143 |
try:
|
| 144 |
img = img.resize((512,512), resample=Image.BICUBIC)
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print(f"Warning: Image resizing failed: {e}. Attempting to proceed without resizing.")
|
| 147 |
+
# Optionally, you might want to return an error here if resizing is critical.
|
| 148 |
+
# For captioning, not resizing might just lead to a slightly different result.
|
| 149 |
|
| 150 |
# create a tiny base64 tag to signal image presence (model must understand this format)
|
| 151 |
import base64
|
| 152 |
buf = io.BytesIO()
|
| 153 |
+
try:
|
| 154 |
+
img.save(buf, format="PNG") # Wrap image save in try-except
|
| 155 |
+
except Exception as e:
|
| 156 |
+
return "Failed to encode image to base64: " + str(e)
|
| 157 |
+
|
| 158 |
b64 = base64.b64encode(buf.getvalue()).decode()
|
| 159 |
img_tag = b64 # minimal
|
| 160 |
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|