-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
40 lines (32 loc) · 1.17 KB
/
translator.py
File metadata and controls
40 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from openai import OpenAI
from .config import get_api_key
# Inicializa o cliente
client = OpenAI(api_key=get_api_key())
def traduzir_texto(texto: str, idioma_origem: str = "inglês",
idioma_destino: str = "português", tom: str = "técnico") -> str:
"""
Traduz um texto técnico usando a API da OpenAI
"""
if not texto or len(texto.strip()) == 0:
return "Erro: Texto vazio para tradução."
prompt = f"""
Traduza o seguinte texto técnico de {idioma_origem} para o {idioma_destino}.
Mantenha o tom {tom} e a terminologia precisa da área de tecnologia.
Preserve a formatação original.
Texto:
{texto}
Tradução:
"""
try:
resposta = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Você é um tradutor técnico especializado."},
{"role": "user", "content": prompt}
],
temperature=0.3
)
traducao = resposta.choices[0].message.content
return traducao
except Exception as e:
return f"Erro na tradução: {str(e)}"