sunbv56 commited on
Commit
75d1af0
·
verified ·
1 Parent(s): 8e1fa2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
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 phản hồi không hợp lệ)"""
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
- print(f"⚠️ Phản hồi API không hợp lệ (thử {attempt}/{max_retries})")
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
- print(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,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
- print(f"⚠️ API quá tải! Chờ {retry_delay} giây trước khi thử lại...")
82
-
83
- # Đếm ngược thời gian chờ
84
  for i in range(retry_delay, 0, -1):
85
- print(f"⏳ Thử lại sau {i} giây...", end="\r")
86
  time.sleep(1)
87
 
88
- print("\n🔄 Đ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
- print(f"❌ Lỗi khi phân tích retryDelay: {parse_error}")
94
 
95
- print(f"❌ Lỗi khi gọi API Gemini: {e}")
96
  return []
97
 
98
- print("❌ Đã 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):
 
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):