Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,6 @@ import shutil
|
|
| 5 |
from typing import Optional, Tuple, Union
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from pathlib import Path
|
| 8 |
-
from PIL import Image
|
| 9 |
-
import io
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
|
| 13 |
# Initialize Hugging Face Inference Client with fal-ai provider
|
| 14 |
client = InferenceClient(
|
|
@@ -25,6 +21,7 @@ def cleanup_temp_files():
|
|
| 25 |
for file_path in Path(temp_dir).glob("*.mp4"):
|
| 26 |
try:
|
| 27 |
# Remove files older than 5 minutes
|
|
|
|
| 28 |
if file_path.stat().st_mtime < (time.time() - 300):
|
| 29 |
file_path.unlink(missing_ok=True)
|
| 30 |
except Exception:
|
|
@@ -90,37 +87,13 @@ def generate_video_from_image(
|
|
| 90 |
if not os.environ.get("HF_TOKEN") and not api_key:
|
| 91 |
return None, "❌ Please set HF_TOKEN environment variable."
|
| 92 |
|
| 93 |
-
# Load and resize image if needed
|
| 94 |
-
|
| 95 |
-
|
| 96 |
if isinstance(image, str):
|
| 97 |
-
|
|
|
|
| 98 |
elif isinstance(image, (bytes, bytearray)):
|
| 99 |
-
|
| 100 |
else:
|
| 101 |
return None, "❌ Invalid image input. Please upload an image."
|
| 102 |
-
|
| 103 |
-
# Resize if image is too large (max 1920x1080)
|
| 104 |
-
max_width = 1920
|
| 105 |
-
max_height = 1080
|
| 106 |
-
|
| 107 |
-
if img.width > max_width or img.height > max_height:
|
| 108 |
-
# Calculate aspect ratio preserving resize
|
| 109 |
-
ratio = min(max_width / img.width, max_height / img.height)
|
| 110 |
-
new_width = int(img.width * ratio)
|
| 111 |
-
new_height = int(img.height * ratio)
|
| 112 |
-
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 113 |
-
|
| 114 |
-
# Convert back to bytes
|
| 115 |
-
img_byte_arr = io.BytesIO()
|
| 116 |
-
# Save as JPEG for smaller file size
|
| 117 |
-
if img.mode in ('RGBA', 'LA', 'P'):
|
| 118 |
-
# Convert RGBA/LA/P to RGB
|
| 119 |
-
rgb_img = Image.new('RGB', img.size, (255, 255, 255))
|
| 120 |
-
rgb_img.paste(img, mask=img.split()[-1] if img.mode in ('RGBA', 'LA') else None)
|
| 121 |
-
img = rgb_img
|
| 122 |
-
img.save(img_byte_arr, format='JPEG', quality=95)
|
| 123 |
-
input_image = img_byte_arr.getvalue()
|
| 124 |
|
| 125 |
video_bytes = temp_client.image_to_video(
|
| 126 |
input_image,
|
|
|
|
| 5 |
from typing import Optional, Tuple, Union
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Initialize Hugging Face Inference Client with fal-ai provider
|
| 10 |
client = InferenceClient(
|
|
|
|
| 21 |
for file_path in Path(temp_dir).glob("*.mp4"):
|
| 22 |
try:
|
| 23 |
# Remove files older than 5 minutes
|
| 24 |
+
import time
|
| 25 |
if file_path.stat().st_mtime < (time.time() - 300):
|
| 26 |
file_path.unlink(missing_ok=True)
|
| 27 |
except Exception:
|
|
|
|
| 87 |
if not os.environ.get("HF_TOKEN") and not api_key:
|
| 88 |
return None, "❌ Please set HF_TOKEN environment variable."
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
if isinstance(image, str):
|
| 91 |
+
with open(image, "rb") as f:
|
| 92 |
+
input_image = f.read()
|
| 93 |
elif isinstance(image, (bytes, bytearray)):
|
| 94 |
+
input_image = image
|
| 95 |
else:
|
| 96 |
return None, "❌ Invalid image input. Please upload an image."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
video_bytes = temp_client.image_to_video(
|
| 99 |
input_image,
|