Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from diffusers import AnimateDiffPipeline, EulerDiscreteScheduler
|
|
10 |
from diffusers.utils import export_to_video
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
from safetensors.torch import load_file
|
|
|
13 |
|
14 |
# Thiết lập logging
|
15 |
logging.basicConfig(level=logging.INFO)
|
@@ -51,6 +52,18 @@ pipe.scheduler = EulerDiscreteScheduler.from_config(
|
|
51 |
)
|
52 |
pipe.safety_checker = None
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# Hàm tạo video
|
55 |
def generate_image(prompt, base="Realistic", motion="", step=1, progress=gr.Progress()):
|
56 |
global step_loaded, base_loaded, motion_loaded
|
@@ -121,16 +134,13 @@ def generate_image(prompt, base="Realistic", motion="", step=1, progress=gr.Prog
|
|
121 |
|
122 |
logger.info(f"✅ Video sẵn sàng tại {video_path}")
|
123 |
|
124 |
-
# Tải video lên
|
125 |
try:
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
public_url = response.text.strip()
|
130 |
-
logger.info(f"✅ Public URL: {public_url}")
|
131 |
-
return public_url # Trả về URL công khai
|
132 |
except Exception as e:
|
133 |
-
logger.error(f"❌ Không thể tải lên
|
134 |
# Fallback: Trả về đường dẫn tệp để Gradio hiển thị
|
135 |
return video_path
|
136 |
|
|
|
10 |
from diffusers.utils import export_to_video
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
from safetensors.torch import load_file
|
13 |
+
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
|
14 |
|
15 |
# Thiết lập logging
|
16 |
logging.basicConfig(level=logging.INFO)
|
|
|
52 |
)
|
53 |
pipe.safety_checker = None
|
54 |
|
55 |
+
# Hàm tải lên Filebin với retry
|
56 |
+
@retry(
|
57 |
+
stop=stop_after_attempt(3), # Thử lại 3 lần
|
58 |
+
wait=wait_fixed(2), # Chờ 2 giây giữa các lần thử
|
59 |
+
retry=retry_if_exception_type(requests.exceptions.RequestException) # Thử lại nếu gặp lỗi mạng
|
60 |
+
)
|
61 |
+
def upload_to_filebin(file_path, file_name):
|
62 |
+
with open(file_path, "rb") as f:
|
63 |
+
response = requests.post(f"https://filebin.net/{uuid.uuid4()}/{file_name}", files={"file": (file_name, f)})
|
64 |
+
response.raise_for_status()
|
65 |
+
return response.json().get("url") # Lấy URL công khai từ phản hồi JSON
|
66 |
+
|
67 |
# Hàm tạo video
|
68 |
def generate_image(prompt, base="Realistic", motion="", step=1, progress=gr.Progress()):
|
69 |
global step_loaded, base_loaded, motion_loaded
|
|
|
134 |
|
135 |
logger.info(f"✅ Video sẵn sàng tại {video_path}")
|
136 |
|
137 |
+
# Tải video lên Filebin để lấy URL công khai
|
138 |
try:
|
139 |
+
public_url = upload_to_filebin(video_path, f"{name}.mp4")
|
140 |
+
logger.info(f"✅ Public URL: {public_url}")
|
141 |
+
return public_url # Trả về URL công khai
|
|
|
|
|
|
|
142 |
except Exception as e:
|
143 |
+
logger.error(f"❌ Không thể tải lên Filebin: {e}")
|
144 |
# Fallback: Trả về đường dẫn tệp để Gradio hiển thị
|
145 |
return video_path
|
146 |
|