Skip to content

Commit eee0e03

Browse files
committed
removed the apimux and invoking directly the goog and msft contextual translators.
1 parent ca15708 commit eee0e03

File tree

3 files changed

+81
-40
lines changed

3 files changed

+81
-40
lines changed

zeeguu/api/endpoints/translation.py

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from zeeguu.api.utils.translator import (
1313
get_next_results,
1414
contribute_trans,
15+
google_contextual_translate,
16+
microsoft_contextual_translate,
1517
)
1618
from zeeguu.core.crowd_translations import (
1719
get_own_past_translation,
@@ -71,38 +73,37 @@ def get_one_translation(from_lang_code, to_lang_code):
7173
user, word_str, from_lang_code, to_lang_code, context
7274
)
7375
if bookmark:
74-
best_guess = bookmark.meaning.translation.content
76+
translation = bookmark.meaning.translation.content
7577
likelihood = 1
7678
source = "Own past translation"
7779
print(f"about to return {bookmark}")
7880
else:
7981
# TODO: must remove theurl, and title - they are not used in the calling method.
8082
if IS_DEV_SKIP_TRANSLATION:
8183
print("Dev Skipping Translation")
82-
best_guess = f"T-({to_lang_code})-'{word_str}'"
84+
translation = f"T-({to_lang_code})-'{word_str}'"
8385
likelihood = None
8486
source = "DEV_SKIP"
8587
else:
86-
translations = get_next_results(
87-
{
88-
"from_lang_code": from_lang_code,
89-
"to_lang_code": to_lang_code,
90-
"word": word_str,
91-
"query": query,
92-
"context": context,
93-
},
94-
number_of_results=3,
95-
).translations
96-
best_guess = translations[0]["translation"]
97-
likelihood = translations[0].pop("quality")
98-
source = translations[0].pop("service_name")
88+
# The API Mux is misbehaving and will only serve the non-contextual translators after a while
89+
# For now hardcoding google on the first place
90+
91+
data = {
92+
"source_language": from_lang_code,
93+
"target_language": to_lang_code,
94+
"word": word_str,
95+
"query": query,
96+
"context": context,
97+
}
98+
t1 = google_contextual_translate(data)
99+
99100
user = User.find_by_id(flask.g.user_id)
100101
bookmark = Bookmark.find_or_create(
101102
db_session,
102103
user,
103104
word_str,
104105
from_lang_code,
105-
best_guess,
106+
t1["translation"],
106107
to_lang_code,
107108
context,
108109
article_id,
@@ -120,10 +121,10 @@ def get_one_translation(from_lang_code, to_lang_code):
120121

121122
return json_result(
122123
{
123-
"translation": best_guess,
124+
"translation": t1["translation"],
124125
"bookmark_id": bookmark.id,
125-
"source": source,
126-
"likelihood": likelihood,
126+
"source": t1["source"],
127+
"likelihood": t1["likelihood"],
127128
}
128129
)
129130

@@ -161,32 +162,16 @@ def get_multiple_translations(from_lang_code, to_lang_code):
161162
query = TranslationQuery.for_word_occurrence(word_str, context, 1, 7)
162163

163164
data = {
164-
"from_lang_code": from_lang_code,
165-
"to_lang_code": to_lang_code,
165+
"source_language": from_lang_code,
166+
"target_language": to_lang_code,
166167
"word": word_str,
167168
"query": query,
168169
"context": context,
169170
}
171+
t1 = google_contextual_translate(data)
172+
t2 = microsoft_contextual_translate(data)
170173

171-
translations = get_next_results(
172-
data,
173-
exclude_services=exclude_services,
174-
exclude_results=exclude_results,
175-
number_of_results=number_of_results,
176-
).translations
177-
178-
# translators talk about quality, but our users expect likelihood.
179-
# rename the key in the dictionary
180-
for t in translations:
181-
t["likelihood"] = t.pop("quality")
182-
t["source"] = t["service_name"]
183-
184-
# ML: Note: We used to save the first bookmark here;
185-
# but that does not make sense; this is used to show all
186-
# alternatives; why save the first to the DB?
187-
# But leaving this note here just in case...
188-
189-
return json_result(dict(translations=translations))
174+
return json_result(dict(translations=[t1, t2]))
190175

191176

192177
@api.route("/update_bookmark/<bookmark_id>", methods=["POST"])

zeeguu/api/utils/caching_decorator.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Claude's Idea
2+
from functools import wraps
3+
4+
5+
def cache_on_data_keys(*cache_keys):
6+
"""Decorator that caches functions taking 'data' as first parameter"""
7+
8+
def decorator(func):
9+
cache = {}
10+
11+
@wraps(func)
12+
def wrapper(data, *args, **kwargs):
13+
# Create cache key from specified dictionary keys
14+
cache_key = tuple(data.get(key) for key in cache_keys)
15+
16+
if cache_key not in cache:
17+
cache[cache_key] = func(data, *args, **kwargs)
18+
19+
return cache[cache_key]
20+
21+
# Add cache management methods
22+
wrapper.cache_clear = lambda: cache.clear()
23+
wrapper.cache_info = (
24+
lambda: f"Cache size: {len(cache)}, Keys: {list(cache.keys())}"
25+
)
26+
27+
return wrapper
28+
29+
return decorator

zeeguu/api/utils/translator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33

4+
from zeeguu.api.utils.caching_decorator import cache_on_data_keys
45
from zeeguu.logging import log
56

67
from apimux.api_base import BaseThirdPartyAPIService
@@ -226,3 +227,29 @@ def contribute_trans(data):
226227
logger.debug(
227228
"Preferred service: %s" % json.dumps(data, ensure_ascii=False).encode("utf-8")
228229
)
230+
231+
232+
@cache_on_data_keys("source_language", "target_language", "word", "context")
233+
def google_contextual_translate(data):
234+
gtx = GoogleTranslateWithContext()
235+
236+
response = gtx.get_result(data)
237+
t = response.translations[0]
238+
239+
t["likelihood"] = t.pop("quality")
240+
t["source"] = t["service_name"]
241+
242+
return t
243+
244+
245+
@cache_on_data_keys("source_language", "target_language", "word", "context")
246+
def microsoft_contextual_translate(data):
247+
gtx = MicrosoftTranslateWithContext()
248+
249+
response = gtx.get_result(data)
250+
t = response.translations[0]
251+
252+
t["likelihood"] = t.pop("quality")
253+
t["source"] = t["service_name"]
254+
255+
return t

0 commit comments

Comments
 (0)