Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,7 @@ from ddgs import DDGS
|
|
| 26 |
from PIL import Image
|
| 27 |
from huggingface_hub import InferenceClient
|
| 28 |
import time
|
|
|
|
| 29 |
|
| 30 |
# Optional imports for Kokoro TTS (loaded lazily)
|
| 31 |
import numpy as np
|
|
@@ -967,27 +968,33 @@ image_generation_interface = gr.Interface(
|
|
| 967 |
# ==========================
|
| 968 |
|
| 969 |
def _write_video_tmp(data_iter_or_bytes: object, suffix: str = ".mp4") -> str:
|
| 970 |
-
"""Write video bytes or iterable of bytes to a temporary file and return its path.
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 991 |
return fname
|
| 992 |
|
| 993 |
|
|
|
|
| 26 |
from PIL import Image
|
| 27 |
from huggingface_hub import InferenceClient
|
| 28 |
import time
|
| 29 |
+
import tempfile
|
| 30 |
|
| 31 |
# Optional imports for Kokoro TTS (loaded lazily)
|
| 32 |
import numpy as np
|
|
|
|
| 968 |
# ==========================
|
| 969 |
|
| 970 |
def _write_video_tmp(data_iter_or_bytes: object, suffix: str = ".mp4") -> str:
|
| 971 |
+
"""Write video bytes or iterable of bytes to a system temporary file and return its path.
|
| 972 |
+
|
| 973 |
+
This avoids polluting the project directory. The file is created in the OS temp
|
| 974 |
+
location; Gradio will handle serving & offering the download button.
|
| 975 |
+
"""
|
| 976 |
+
fd, fname = tempfile.mkstemp(suffix=suffix)
|
| 977 |
+
try:
|
| 978 |
+
with os.fdopen(fd, "wb") as f:
|
| 979 |
+
if isinstance(data_iter_or_bytes, (bytes, bytearray)):
|
| 980 |
+
f.write(data_iter_or_bytes) # type: ignore[arg-type]
|
| 981 |
+
elif hasattr(data_iter_or_bytes, "read"):
|
| 982 |
+
f.write(data_iter_or_bytes.read()) # type: ignore[call-arg]
|
| 983 |
+
elif hasattr(data_iter_or_bytes, "content"):
|
| 984 |
+
f.write(data_iter_or_bytes.content) # type: ignore[attr-defined]
|
| 985 |
+
elif hasattr(data_iter_or_bytes, "__iter__") and not isinstance(data_iter_or_bytes, (str, dict)):
|
| 986 |
+
for chunk in data_iter_or_bytes: # type: ignore[assignment]
|
| 987 |
+
if chunk:
|
| 988 |
+
f.write(chunk)
|
| 989 |
+
else:
|
| 990 |
+
raise gr.Error("Unsupported video data type returned by provider.")
|
| 991 |
+
except Exception:
|
| 992 |
+
# Clean up if writing failed
|
| 993 |
+
try:
|
| 994 |
+
os.remove(fname)
|
| 995 |
+
except Exception:
|
| 996 |
+
pass
|
| 997 |
+
raise
|
| 998 |
return fname
|
| 999 |
|
| 1000 |
|