Update routers/other_route.py
Browse files- routers/other_route.py +19 -15
routers/other_route.py
CHANGED
@@ -8,17 +8,17 @@ router = APIRouter()
|
|
8 |
@router.get("/translate/")
|
9 |
def translate_text(
|
10 |
text: str = Query(..., description="Texto a ser traduzido"),
|
11 |
-
|
12 |
-
|
13 |
):
|
14 |
"""
|
15 |
Traduz o texto para o idioma alvo e retorna detalhes, incluindo a detecção de idioma e o tempo de tradução.
|
16 |
"""
|
17 |
# Verifica se o idioma de destino é válido
|
18 |
-
if
|
19 |
raise HTTPException(
|
20 |
status_code=400,
|
21 |
-
detail=f"Invalid target language: {
|
22 |
)
|
23 |
|
24 |
try:
|
@@ -27,26 +27,30 @@ def translate_text(
|
|
27 |
|
28 |
# Se o idioma de origem não for fornecido, será detectado automaticamente
|
29 |
start_time = time.time() # Início do tempo de tradução
|
30 |
-
if
|
31 |
# Traduz com idioma de origem especificado
|
32 |
-
translated = translator.translate(text, src=
|
|
|
33 |
else:
|
34 |
# Se não for fornecido, detecta automaticamente o idioma de origem
|
35 |
-
translated = translator.translate(text, dest=
|
|
|
36 |
|
37 |
end_time = time.time() # Fim do tempo de tradução
|
38 |
translation_time = round(end_time - start_time, 4) # Tempo em segundos (com 4 casas decimais)
|
39 |
|
40 |
# Retorna a resposta com dados adicionais
|
41 |
return {
|
42 |
-
"
|
43 |
-
"
|
44 |
-
"
|
45 |
-
"
|
46 |
-
"
|
47 |
-
"
|
48 |
-
"
|
49 |
-
"
|
|
|
|
|
50 |
}
|
51 |
|
52 |
except Exception as e:
|
|
|
8 |
@router.get("/translate/")
|
9 |
def translate_text(
|
10 |
text: str = Query(..., description="Texto a ser traduzido"),
|
11 |
+
target_lang: str = Query("en", description="Idioma alvo para tradução"),
|
12 |
+
source_lang: str = Query(None, description="Idioma de origem (opcional, se não fornecido será detectado automaticamente)")
|
13 |
):
|
14 |
"""
|
15 |
Traduz o texto para o idioma alvo e retorna detalhes, incluindo a detecção de idioma e o tempo de tradução.
|
16 |
"""
|
17 |
# Verifica se o idioma de destino é válido
|
18 |
+
if target_lang not in LANGUAGES:
|
19 |
raise HTTPException(
|
20 |
status_code=400,
|
21 |
+
detail=f"Invalid target language: {target_lang}. Supported languages: {', '.join(LANGUAGES.keys())}"
|
22 |
)
|
23 |
|
24 |
try:
|
|
|
27 |
|
28 |
# Se o idioma de origem não for fornecido, será detectado automaticamente
|
29 |
start_time = time.time() # Início do tempo de tradução
|
30 |
+
if source_lang:
|
31 |
# Traduz com idioma de origem especificado
|
32 |
+
translated = translator.translate(text, src=source_lang, dest=target_lang)
|
33 |
+
detected_lang = False # Indica que o idioma foi fornecido
|
34 |
else:
|
35 |
# Se não for fornecido, detecta automaticamente o idioma de origem
|
36 |
+
translated = translator.translate(text, dest=target_lang)
|
37 |
+
detected_lang = True # Indica que o idioma foi detectado
|
38 |
|
39 |
end_time = time.time() # Fim do tempo de tradução
|
40 |
translation_time = round(end_time - start_time, 4) # Tempo em segundos (com 4 casas decimais)
|
41 |
|
42 |
# Retorna a resposta com dados adicionais
|
43 |
return {
|
44 |
+
"orig_text": text,
|
45 |
+
"trans_text": translated.text,
|
46 |
+
"detected_lang": detected_lang,
|
47 |
+
"source_lang": translated.src if not source_lang else source_lang,
|
48 |
+
"target_lang": target_lang,
|
49 |
+
"trans_time_s": translation_time,
|
50 |
+
"orig_lang_name": LANGUAGES.get(translated.src, "Unknown"),
|
51 |
+
"target_lang_name": LANGUAGES.get(target_lang, "Unknown"),
|
52 |
+
"orig_word_count": len(text.split()),
|
53 |
+
"trans_word_count": len(translated.text.split())
|
54 |
}
|
55 |
|
56 |
except Exception as e:
|