Update app.py
Browse files
app.py
CHANGED
|
@@ -37,7 +37,7 @@ def load_image_as_bytes(image_path):
|
|
| 37 |
return None
|
| 38 |
|
| 39 |
async def generate_image(image_bytes_list, text_input, max_retries=5):
|
| 40 |
-
"""Gửi request và nhận kết quả từ Gemini API (Xử lý lỗi 429
|
| 41 |
attempt = 0 # Đếm số lần thử lại
|
| 42 |
while attempt < max_retries:
|
| 43 |
try:
|
|
@@ -57,7 +57,7 @@ async def generate_image(image_bytes_list, text_input, max_retries=5):
|
|
| 57 |
|
| 58 |
if not response or not response.candidates or not response.candidates[0].content:
|
| 59 |
attempt += 1
|
| 60 |
-
|
| 61 |
continue # Thử lại ngay lập tức
|
| 62 |
|
| 63 |
images = []
|
|
@@ -67,7 +67,7 @@ async def generate_image(image_bytes_list, text_input, max_retries=5):
|
|
| 67 |
img = Image.open(BytesIO(part.inline_data.data))
|
| 68 |
images.append(img)
|
| 69 |
except Exception as e:
|
| 70 |
-
|
| 71 |
return images # Thành công, trả về ảnh ngay lập tức
|
| 72 |
|
| 73 |
except Exception as e:
|
|
@@ -78,24 +78,24 @@ async def generate_image(image_bytes_list, text_input, max_retries=5):
|
|
| 78 |
error_json = json.loads(error_message.split("RESOURCE_EXHAUSTED. ")[1])
|
| 79 |
retry_delay = int(error_json["error"]["details"][-1]["retryDelay"][:-1]) # Lấy số giây từ '2s'
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Đếm ngược thời gian chờ
|
| 84 |
for i in range(retry_delay, 0, -1):
|
| 85 |
-
|
| 86 |
time.sleep(1)
|
| 87 |
|
| 88 |
-
|
| 89 |
attempt += 1
|
| 90 |
continue # Thử lại sau thời gian chờ
|
| 91 |
|
| 92 |
except Exception as parse_error:
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
return []
|
| 97 |
|
| 98 |
-
|
| 99 |
return []
|
| 100 |
|
| 101 |
async def process_request(images, text, num_requests):
|
|
|
|
| 37 |
return None
|
| 38 |
|
| 39 |
async def generate_image(image_bytes_list, text_input, max_retries=5):
|
| 40 |
+
"""Gửi request và nhận kết quả từ Gemini API (Xử lý lỗi 429 với popup thông báo)"""
|
| 41 |
attempt = 0 # Đếm số lần thử lại
|
| 42 |
while attempt < max_retries:
|
| 43 |
try:
|
|
|
|
| 57 |
|
| 58 |
if not response or not response.candidates or not response.candidates[0].content:
|
| 59 |
attempt += 1
|
| 60 |
+
gr.Warning(f"⚠️ API trả về dữ liệu không hợp lệ (thử {attempt}/{max_retries}). Đang thử lại...")
|
| 61 |
continue # Thử lại ngay lập tức
|
| 62 |
|
| 63 |
images = []
|
|
|
|
| 67 |
img = Image.open(BytesIO(part.inline_data.data))
|
| 68 |
images.append(img)
|
| 69 |
except Exception as e:
|
| 70 |
+
gr.Error(f"❌ Lỗi khi hiển thị ảnh: {e}")
|
| 71 |
return images # Thành công, trả về ảnh ngay lập tức
|
| 72 |
|
| 73 |
except Exception as e:
|
|
|
|
| 78 |
error_json = json.loads(error_message.split("RESOURCE_EXHAUSTED. ")[1])
|
| 79 |
retry_delay = int(error_json["error"]["details"][-1]["retryDelay"][:-1]) # Lấy số giây từ '2s'
|
| 80 |
|
| 81 |
+
gr.Warning(f"⚠️ API quá tải! Sẽ thử lại sau {retry_delay} giây...")
|
| 82 |
+
|
| 83 |
+
# Đếm ngược thời gian chờ (có popup)
|
| 84 |
for i in range(retry_delay, 0, -1):
|
| 85 |
+
gr.Info(f"⏳ Thử lại sau {i} giây...")
|
| 86 |
time.sleep(1)
|
| 87 |
|
| 88 |
+
gr.Info("🔄 Đang thử lại...")
|
| 89 |
attempt += 1
|
| 90 |
continue # Thử lại sau thời gian chờ
|
| 91 |
|
| 92 |
except Exception as parse_error:
|
| 93 |
+
gr.Error(f"❌ Lỗi khi phân tích retryDelay: {parse_error}")
|
| 94 |
|
| 95 |
+
gr.Error(f"❌ Lỗi khi gọi API Gemini: {e}")
|
| 96 |
return []
|
| 97 |
|
| 98 |
+
gr.Error("❌ Đã thử lại nhiều lần nhưng vẫn thất bại!")
|
| 99 |
return []
|
| 100 |
|
| 101 |
async def process_request(images, text, num_requests):
|