Nymbo commited on
Commit
0c80777
·
verified ·
1 Parent(s): 4beb2ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
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
- os.makedirs("outputs", exist_ok=True)
972
- fname = f"outputs/video_{int(time.time())}_{random.randint(1000,9999)}{suffix}"
973
- mode = "wb"
974
- with open(fname, mode) as f:
975
- # bytes-like
976
- if isinstance(data_iter_or_bytes, (bytes, bytearray)):
977
- f.write(data_iter_or_bytes) # type: ignore[arg-type]
978
- # file-like with read()
979
- elif hasattr(data_iter_or_bytes, "read"):
980
- f.write(data_iter_or_bytes.read()) # type: ignore[call-arg]
981
- # response-like with content
982
- elif hasattr(data_iter_or_bytes, "content"):
983
- f.write(data_iter_or_bytes.content) # type: ignore[attr-defined]
984
- # iterable of chunks
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
  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