Spaces:
Runtime error
Runtime error
Implement retry logic for loading model
Browse files
app.py
CHANGED
@@ -1,8 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import login
|
3 |
import os
|
|
|
4 |
|
5 |
# Lấy token từ biến môi trường
|
6 |
token = os.getenv("TOKEN")
|
|
|
|
|
|
|
|
|
|
|
7 |
login(token=token)
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import login
|
3 |
import os
|
4 |
+
import time
|
5 |
|
6 |
# Lấy token từ biến môi trường
|
7 |
token = os.getenv("TOKEN")
|
8 |
+
|
9 |
+
if token is None:
|
10 |
+
raise ValueError("TOKEN environment variable not set")
|
11 |
+
|
12 |
+
# Đăng nhập vào Hugging Face bằng token
|
13 |
login(token=token)
|
14 |
+
|
15 |
+
# Đảm bảo rằng tên mô hình là chính xác
|
16 |
+
model_name = "HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1"
|
17 |
+
|
18 |
+
# Hàm tải mô hình với xử lý lỗi
|
19 |
+
def load_model_with_retry(model_name, token, retries=3, delay=5):
|
20 |
+
for i in range(retries):
|
21 |
+
try:
|
22 |
+
demo = gr.load(f"models/{model_name}", api_key=token)
|
23 |
+
return demo
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Error loading model (attempt {i+1}/{retries}): {e}")
|
26 |
+
if i < retries - 1:
|
27 |
+
time.sleep(delay)
|
28 |
+
else:
|
29 |
+
raise
|
30 |
+
|
31 |
+
# Thử tải mô hình
|
32 |
+
try:
|
33 |
+
demo = load_model_with_retry(model_name, token)
|
34 |
+
demo.launch()
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Failed to load model after multiple attempts: {e}")
|