mintlee commited on
Commit
314e765
·
1 Parent(s): 58fa02f

add fix exceed quota

Browse files
Files changed (1) hide show
  1. utils/utils.py +21 -29
utils/utils.py CHANGED
@@ -7,7 +7,7 @@ import json
7
  import time
8
  from google.api_core.exceptions import ResourceExhausted
9
 
10
- genai.configure(api_key="AIzaSyBH8O5IfqYrJ5wtWnmUC21IfMjzJCrTm3I")
11
 
12
 
13
  def unzip_office_file(pptx_file: io.BytesIO):
@@ -26,57 +26,51 @@ def unzip_office_file(pptx_file: io.BytesIO):
26
 
27
 
28
  def translate_single_text(text: str, source_lang: str = 'English', target_lang: str = "Vietnamese",
29
- max_retries: int = 5, retry_delay_seconds: int = 3) -> str:
30
  if not text or not text.strip():
31
  return "" # Bỏ qua nếu chuỗi rỗng hoặc chỉ chứa khoảng trắng
32
 
33
  retries = 0
34
  while retries <= max_retries:
35
  try:
36
- model = genai.GenerativeModel('gemini-2.0-flash') # Hoặc 'gemini-1.0-pro', 'gemini-1.5-flash' tùy bạn chọn
37
 
38
- system_prompt_simple = f"""You are a translation engine.
39
- Translate the following text accurately from {source_lang} to {target_lang}.
40
- Provide *only* the translated text as a single string.
41
- Do NOT add any extra formatting, delimiters like '#', introductory phrases, or explanations."""
42
 
43
  user_prompt = f"Source language: {source_lang}. Target language: {target_lang}. Text to translate: {text}"
44
- full_prompt = system_prompt_simple.strip() + "\n\n" + user_prompt.strip()
45
 
46
  response = model.generate_content(
47
  contents=full_prompt,
48
  generation_config={
49
- 'temperature': 0.2,
50
- 'top_p': 1.0,
51
- 'top_k': 1,
52
- # 'max_output_tokens': 2048,
53
  }
54
  )
55
- # Kiểm tra xem response có text không và có ứng viên không
56
  if response.candidates and response.candidates[0].content.parts:
57
  translated_text = "".join(part.text for part in response.candidates[0].content.parts if hasattr(part, 'text')).strip()
58
  return translated_text
59
  else:
60
- print(f"Không nhận được nội dung hợp lệ từ API cho văn bản: '{text[:50]}...'")
61
- # Không thử lại với lỗi này, trả về rỗng
62
  return ""
63
 
64
-
65
- except ResourceExhausted as e: # Bắt lỗi 429 (Too Many Requests / Quota Exceeded)
66
- print(f"Lỗi quota (429) khi dịch '{text[:50]}...': {e}. Đang thử lại sau {retry_delay_seconds} giây. Lần thử {retries + 1}/{max_retries +1 }.")
67
- if retries < max_retries:
68
- time.sleep(retry_delay_seconds)
69
- retries += 1
70
- else:
71
- print(f"Đã vượt quá số lần thử lại tối đa ({max_retries + 1}) cho '{text[:50]}...'. Bỏ qua.")
72
- return "" # Trả về rỗng sau khi đã thử lại tối đa số lần
73
 
74
  except Exception as e:
75
- print(f"Lỗi không mong muốn trong quá trình dịch (translate_single_text) cho '{text[:50]}...': {e}")
76
  return ""
77
 
78
- return "" # Trường hợp không bao giờ nên xảy ra nếu logic vòng lặp đúng
79
-
80
 
81
  def preprocess_text(text_list):
82
  """
@@ -141,7 +135,6 @@ def translate_text(text_dict, source_lang='English', target_lang="Vietnamese"):
141
 
142
 
143
  system_prompt = f"""Translate the string values within the following JSON object .
144
-
145
  Follow these instructions carefully:
146
  1. Analyze the entire JSON object to understand the context.
147
  2. Translate *only* the string values.
@@ -149,7 +142,6 @@ def translate_text(text_dict, source_lang='English', target_lang="Vietnamese"):
149
  4. Do *not* translate non-string values (like hex color codes, numbers, or potentially proper nouns like 'CALISTOGA', 'DM SANS', 'Pexels', 'Pixabay' unless they have a common translation). Use your best judgment for proper nouns.
150
  5. Preserve the original JSON structure perfectly.
151
  6. Your output *must* be only the translated JSON object, without any introductory text, explanations, or markdown formatting like ```json ... ```.
152
-
153
  """
154
  # 3. Construct User Prompt
155
  user_prompt = f"Source language: {source_lang}. Target language: {target_lang}. JSON String: {json_input_string} \n\n Translated JSON Output:"
 
7
  import time
8
  from google.api_core.exceptions import ResourceExhausted
9
 
10
+ genai.configure(api_key="AIzaSyDInJcxzqBvsh1avs4Zkxb4ZGBooNzOyEM")
11
 
12
 
13
  def unzip_office_file(pptx_file: io.BytesIO):
 
26
 
27
 
28
  def translate_single_text(text: str, source_lang: str = 'English', target_lang: str = "Vietnamese",
29
+ max_retries: int = 5, base_delay: float = 5.0) -> str:
30
  if not text or not text.strip():
31
  return "" # Bỏ qua nếu chuỗi rỗng hoặc chỉ chứa khoảng trắng
32
 
33
  retries = 0
34
  while retries <= max_retries:
35
  try:
36
+ model = genai.GenerativeModel('gemini-2.0-flash') # hoặc 'gemini-1.5-flash'
37
 
38
+ system_prompt = f"""You are a translation engine.
39
+ Translate the following text accurately from {source_lang} to {target_lang}.
40
+ Provide *only* the translated text as a single string.
41
+ Do NOT add any extra formatting, delimiters like '#', introductory phrases, or explanations."""
42
 
43
  user_prompt = f"Source language: {source_lang}. Target language: {target_lang}. Text to translate: {text}"
44
+ full_prompt = system_prompt.strip() + "\n\n" + user_prompt.strip()
45
 
46
  response = model.generate_content(
47
  contents=full_prompt,
48
  generation_config={
49
+ 'temperature': 0.2,
50
+ 'top_p': 1.0,
51
+ 'top_k': 1,
 
52
  }
53
  )
54
+
55
  if response.candidates and response.candidates[0].content.parts:
56
  translated_text = "".join(part.text for part in response.candidates[0].content.parts if hasattr(part, 'text')).strip()
57
  return translated_text
58
  else:
59
+ print(f"[!] Không nhận được nội dung hợp lệ từ API cho văn bản: '{text[:50]}...'")
 
60
  return ""
61
 
62
+ except ResourceExhausted as e:
63
+ wait_time = base_delay * (2 ** retries)
64
+ print(f"[429] Quota exceeded khi dịch '{text[:50]}...'. Thử lại sau {wait_time:.1f}s (lần {retries + 1}/{max_retries + 1}).")
65
+ time.sleep(wait_time)
66
+ retries += 1
 
 
 
 
67
 
68
  except Exception as e:
69
+ print(f"[!] Lỗi không mong muốn khi dịch '{text[:50]}...': {e}")
70
  return ""
71
 
72
+ print(f"[x] Bỏ qua sau {max_retries + 1} lần thử không thành công cho '{text[:50]}...'.")
73
+ return ""
74
 
75
  def preprocess_text(text_list):
76
  """
 
135
 
136
 
137
  system_prompt = f"""Translate the string values within the following JSON object .
 
138
  Follow these instructions carefully:
139
  1. Analyze the entire JSON object to understand the context.
140
  2. Translate *only* the string values.
 
142
  4. Do *not* translate non-string values (like hex color codes, numbers, or potentially proper nouns like 'CALISTOGA', 'DM SANS', 'Pexels', 'Pixabay' unless they have a common translation). Use your best judgment for proper nouns.
143
  5. Preserve the original JSON structure perfectly.
144
  6. Your output *must* be only the translated JSON object, without any introductory text, explanations, or markdown formatting like ```json ... ```.
 
145
  """
146
  # 3. Construct User Prompt
147
  user_prompt = f"Source language: {source_lang}. Target language: {target_lang}. JSON String: {json_input_string} \n\n Translated JSON Output:"