Muhammad Abdur Rahman Saad
commited on
Commit
·
497072d
1
Parent(s):
eaba4ed
fix NotValidLengthException
Browse files- controllers/utils.py +23 -7
controllers/utils.py
CHANGED
@@ -153,7 +153,7 @@ def fetch_url(url):
|
|
153 |
return None
|
154 |
|
155 |
|
156 |
-
def translate(text):
|
157 |
"""
|
158 |
Translates the given text to English.
|
159 |
|
@@ -163,12 +163,28 @@ def translate(text):
|
|
163 |
Returns:
|
164 |
str: The translated text in English.
|
165 |
"""
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
|
174 |
def sentiment_computation(content):
|
|
|
153 |
return None
|
154 |
|
155 |
|
156 |
+
def translate(text, max_length=4950):
|
157 |
"""
|
158 |
Translates the given text to English.
|
159 |
|
|
|
163 |
Returns:
|
164 |
str: The translated text in English.
|
165 |
"""
|
166 |
+
if not text:
|
167 |
+
return ""
|
168 |
+
if len(text) <= max_length:
|
169 |
+
for _ in range(5):
|
170 |
+
try:
|
171 |
+
return GoogleTranslator(source='auto', target='en').translate(text) or ""
|
172 |
+
except exceptions.RequestError:
|
173 |
+
time.sleep(1)
|
174 |
+
return ""
|
175 |
+
# If text is too long, split and translate in chunks
|
176 |
+
result = []
|
177 |
+
for i in range(0, len(text), max_length):
|
178 |
+
chunk = text[i:i+max_length]
|
179 |
+
for _ in range(5):
|
180 |
+
try:
|
181 |
+
result.append(GoogleTranslator(source='auto', target='en').translate(chunk) or "")
|
182 |
+
break
|
183 |
+
except exceptions.RequestError:
|
184 |
+
time.sleep(1)
|
185 |
+
else:
|
186 |
+
result.append("") # If all retries fail, append empty string
|
187 |
+
return " ".join(result)
|
188 |
|
189 |
|
190 |
def sentiment_computation(content):
|