Spaces:
Running
Running
Commit
·
01d0236
1
Parent(s):
7eccaaf
introduce googletrans w reqs mend qoogletrans
Browse files
app.py
CHANGED
@@ -30,7 +30,16 @@ class TranslationSystem:
|
|
30 |
"""
|
31 |
self.method = method
|
32 |
self.llm = llm
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def translate_text(self, text, src='ru', dest='en'):
|
36 |
"""
|
@@ -44,51 +53,96 @@ class TranslationSystem:
|
|
44 |
Returns:
|
45 |
str: Translated text
|
46 |
"""
|
47 |
-
if pd.isna(text) or not text.strip():
|
48 |
return text
|
49 |
|
50 |
try:
|
51 |
-
if self.method == 'googletrans':
|
52 |
return self._translate_with_googletrans(text, src, dest)
|
53 |
else:
|
54 |
return self._translate_with_llm(text, src, dest)
|
55 |
except Exception as e:
|
56 |
-
st.warning(f"Translation error: {str(e)}")
|
57 |
return text
|
58 |
|
59 |
def _translate_with_googletrans(self, text, src='ru', dest='en'):
|
60 |
"""
|
61 |
-
Translate using googletrans library.
|
62 |
"""
|
63 |
try:
|
|
|
|
|
|
|
|
|
|
|
64 |
# Add delay to avoid rate limits
|
65 |
time.sleep(0.5)
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
except Exception as e:
|
|
|
|
|
|
|
|
|
69 |
raise Exception(f"Googletrans error: {str(e)}")
|
70 |
|
71 |
def _translate_with_llm(self, text, src='ru', dest='en'):
|
72 |
"""
|
73 |
-
Translate using LangChain LLM.
|
74 |
"""
|
75 |
if not self.llm:
|
76 |
raise Exception("LLM not initialized for translation")
|
77 |
|
78 |
-
messages = [
|
79 |
-
{"role": "system", "content": "You are a translator. Translate the given Russian text to English accurately and concisely."},
|
80 |
-
{"role": "user", "content": f"Translate this Russian text to English: {text}"}
|
81 |
-
]
|
82 |
-
|
83 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
response = self.llm.invoke(messages)
|
85 |
|
|
|
86 |
if hasattr(response, 'content'):
|
87 |
-
|
88 |
elif isinstance(response, str):
|
89 |
-
|
90 |
else:
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
except Exception as e:
|
93 |
raise Exception(f"LLM translation error: {str(e)}")
|
94 |
|
@@ -564,7 +618,7 @@ def create_output_file(df, uploaded_file, llm):
|
|
564 |
|
565 |
def main():
|
566 |
with st.sidebar:
|
567 |
-
st.title("::: AI-анализ мониторинга новостей (v.3.
|
568 |
st.subheader("по материалам СКАН-ИНТЕРФАКС ")
|
569 |
|
570 |
model_choice = st.radio(
|
|
|
30 |
"""
|
31 |
self.method = method
|
32 |
self.llm = llm
|
33 |
+
if method == 'googletrans':
|
34 |
+
try:
|
35 |
+
self.google_translator = GoogleTranslator()
|
36 |
+
# Test the translator with a simple string
|
37 |
+
self.google_translator.translate('test', src='en', dest='ru')
|
38 |
+
except Exception as e:
|
39 |
+
st.warning(f"Error initializing Google Translator: {str(e)}. Falling back to LLM translation.")
|
40 |
+
self.method = 'llm'
|
41 |
+
else:
|
42 |
+
self.google_translator = None
|
43 |
|
44 |
def translate_text(self, text, src='ru', dest='en'):
|
45 |
"""
|
|
|
53 |
Returns:
|
54 |
str: Translated text
|
55 |
"""
|
56 |
+
if pd.isna(text) or not isinstance(text, str) or not text.strip():
|
57 |
return text
|
58 |
|
59 |
try:
|
60 |
+
if self.method == 'googletrans' and self.google_translator:
|
61 |
return self._translate_with_googletrans(text, src, dest)
|
62 |
else:
|
63 |
return self._translate_with_llm(text, src, dest)
|
64 |
except Exception as e:
|
65 |
+
st.warning(f"Translation error: {str(e)}. Returning original text.")
|
66 |
return text
|
67 |
|
68 |
def _translate_with_googletrans(self, text, src='ru', dest='en'):
|
69 |
"""
|
70 |
+
Translate using googletrans library with improved error handling.
|
71 |
"""
|
72 |
try:
|
73 |
+
# Clean and validate input text
|
74 |
+
text = text.strip()
|
75 |
+
if not text:
|
76 |
+
return text
|
77 |
+
|
78 |
# Add delay to avoid rate limits
|
79 |
time.sleep(0.5)
|
80 |
+
|
81 |
+
# Attempt translation with retry logic
|
82 |
+
max_retries = 3
|
83 |
+
for attempt in range(max_retries):
|
84 |
+
try:
|
85 |
+
result = self.google_translator.translate(text, src=src, dest=dest)
|
86 |
+
if result and result.text:
|
87 |
+
return result.text
|
88 |
+
raise Exception("Empty translation result")
|
89 |
+
except Exception as e:
|
90 |
+
if attempt == max_retries - 1:
|
91 |
+
raise
|
92 |
+
time.sleep(1) # Wait before retry
|
93 |
+
|
94 |
+
raise Exception("All translation attempts failed")
|
95 |
+
|
96 |
except Exception as e:
|
97 |
+
# If googletrans fails, fall back to LLM translation
|
98 |
+
if self.llm:
|
99 |
+
st.warning(f"Googletrans error: {str(e)}. Falling back to LLM translation.")
|
100 |
+
return self._translate_with_llm(text, src, dest)
|
101 |
raise Exception(f"Googletrans error: {str(e)}")
|
102 |
|
103 |
def _translate_with_llm(self, text, src='ru', dest='en'):
|
104 |
"""
|
105 |
+
Translate using LangChain LLM with improved error handling.
|
106 |
"""
|
107 |
if not self.llm:
|
108 |
raise Exception("LLM not initialized for translation")
|
109 |
|
|
|
|
|
|
|
|
|
|
|
110 |
try:
|
111 |
+
# Clean input text
|
112 |
+
text = text.strip()
|
113 |
+
if not text:
|
114 |
+
return text
|
115 |
+
|
116 |
+
# Prepare system message based on language direction
|
117 |
+
if src == 'ru' and dest == 'en':
|
118 |
+
system_msg = "You are a translator. Translate the given Russian text to English accurately and concisely."
|
119 |
+
user_msg = f"Translate this Russian text to English: {text}"
|
120 |
+
elif src == 'en' and dest == 'ru':
|
121 |
+
system_msg = "You are a translator. Translate the given English text to Russian accurately and concisely."
|
122 |
+
user_msg = f"Translate this English text to Russian: {text}"
|
123 |
+
else:
|
124 |
+
raise Exception(f"Unsupported language pair: {src} to {dest}")
|
125 |
+
|
126 |
+
messages = [
|
127 |
+
{"role": "system", "content": system_msg},
|
128 |
+
{"role": "user", "content": user_msg}
|
129 |
+
]
|
130 |
+
|
131 |
response = self.llm.invoke(messages)
|
132 |
|
133 |
+
# Handle different response types
|
134 |
if hasattr(response, 'content'):
|
135 |
+
translation = response.content.strip()
|
136 |
elif isinstance(response, str):
|
137 |
+
translation = response.strip()
|
138 |
else:
|
139 |
+
translation = str(response).strip()
|
140 |
+
|
141 |
+
if not translation:
|
142 |
+
raise Exception("Empty translation result")
|
143 |
+
|
144 |
+
return translation
|
145 |
+
|
146 |
except Exception as e:
|
147 |
raise Exception(f"LLM translation error: {str(e)}")
|
148 |
|
|
|
618 |
|
619 |
def main():
|
620 |
with st.sidebar:
|
621 |
+
st.title("::: AI-анализ мониторинга новостей (v.3.33 ):::")
|
622 |
st.subheader("по материалам СКАН-ИНТЕРФАКС ")
|
623 |
|
624 |
model_choice = st.radio(
|