Skip to content

Commit 41cf65a

Browse files
committed
Final translator microservice - DeepSeek integration
1 parent 78c2491 commit 41cf65a

File tree

4 files changed

+54
-72
lines changed

4 files changed

+54
-72
lines changed

app.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
from flask import Flask, request, jsonify
22
import os
3-
4-
from flask import Flask
5-
from flask import request, jsonify
6-
from src.translator import translate
3+
from src.translator import translate_content # fixed import
74

85
app = Flask(__name__)
96

107
@app.route("/", methods=["GET"])
118
def translator():
12-
content = request.args.get("content", default = "", type = str)
13-
is_english, translated_content = translate(content)
9+
content = request.args.get("content", default="", type=str)
10+
11+
result = translate_content(content)
12+
1413
return jsonify({
15-
"is_english": is_english,
16-
"translated_content": translated_content,
14+
"isEnglish": result["isEnglish"],
15+
"translatedContent": result["translatedContent"]
1716
})
1817

1918
if __name__ == "__main__":
2019
port = int(os.environ.get("PORT", 8080))
2120
app.run(debug=True, host="0.0.0.0", port=port)
22-

docker-compose.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
version: '3.8'
2-
31
services:
4-
mytranslator:
5-
build: .
6-
network_mode: "host"
7-
restart: always
2+
translator:
3+
build: .
4+
restart: unless-stopped
5+
network_mode: host

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/translator.py

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,59 @@
33
import re
44

55
def translate(content: str) -> tuple[bool, str]:
6-
"""
7-
Translates content if it's not in English.
8-
Returns (is_english: bool, translated_content: str)
9-
"""
6+
prompt = f"""
7+
Analyze the following text and determine if it is in English or another language.
108
11-
prompt = f
12-
"""Analyze the following text and determine if it is in English or another language.
13-
If the text is in English, respond with exactly:
14-
{{"is_english": true, "translated_content": ""}}
9+
If the text is in English, respond with exactly:
10+
{{"is_english": true, "translated_content": ""}}
1511
16-
If the text is NOT in English, translate it to English and respond with exactly:
17-
{{"is_english": false, "translated_content": "YOUR TRANSLATION HERE"}}
12+
If the text is NOT in English, translate it to English and respond with exactly:
13+
{{"is_english": false, "translated_content": "YOUR TRANSLATION HERE"}}
1814
19-
You must respond ONLY with valid JSON in the exact format above. Do not include any other text.
15+
Respond ONLY with valid JSON.
16+
No explanations. No thinking. No markdown. No extra text.
17+
If you cannot comply, output exactly: {{"is_english": true, "translated_content": ""}}.
2018
21-
Text to analyze:
22-
{content}
23-
"""
19+
Text to analyze:
20+
{content}
21+
"""
2422

2523
try:
26-
# Call the LLM using Ollama
2724
response = ollama.chat(
28-
model='llama3.2',
29-
messages=[
30-
{
31-
'role': 'user',
32-
'content': prompt
33-
}
34-
]
25+
model="deepseek-r1:8b",
26+
messages=[{"role": "user", "content": prompt}]
3527
)
3628

37-
response_text = response['message']['content'].strip()
29+
response_text = response["message"]["content"].strip()
3830

39-
# Try to parse the JSON directly
31+
# Try normal JSON parsing
4032
try:
4133
result = json.loads(response_text)
42-
is_english = result.get("is_english", True)
43-
translated_content = result.get("translated_content", "")
44-
return (is_english, translated_content)
34+
return result.get("is_english", True), result.get("translated_content", "")
35+
except:
36+
pass
37+
38+
# Try fallback JSON extraction
39+
json_match = re.search(
40+
r'\{[\s\S]*?"is_english"[\s\S]*?"translated_content"[\s\S]*?\}',
41+
response_text
42+
)
4543

46-
except json.JSONDecodeError:
47-
# Fallback: try to extract a JSON-like substring if LLM added extra text
48-
json_match = re.search(
49-
r'\{[^}]"is_english"[^}]"translated_content"[^}]*\}',
50-
response_text,
51-
re.DOTALL
52-
)
53-
if json_match:
54-
try:
55-
result = json.loads(json_match.group(0))
56-
is_english = result.get("is_english", True)
57-
translated_content = result.get("translated_content", "")
58-
return (is_english, translated_content)
59-
except json.JSONDecodeError:
60-
pass
44+
if json_match:
45+
try:
46+
result = json.loads(json_match.group(0))
47+
return result.get("is_english", True), result.get("translated_content", "")
48+
except:
49+
pass
6150

62-
# If parsing completely fails, assume input is English
63-
print(f"Warning: Could not parse LLM response: {response_text}")
64-
return (True, "")
51+
print("Warning: Could not parse:", response_text)
52+
return True, ""
6553

6654
except Exception as e:
67-
print(f"Error calling LLM: {e}")
68-
# On error, assume English so posts don't break
69-
return (True, "")
55+
print("Error calling LLM:", e)
56+
return True, ""
7057

7158

7259
def translate_content(content: str) -> dict:
73-
"""
74-
Wrapper for Flask app to return JSON-friendly output.
75-
Returns keys in camelCase to match NodeBB expectations.
76-
"""
77-
is_english, translated_content = translate(content)
78-
return {
79-
"isEnglish": is_english,
80-
"translatedContent": translated_content
81-
}
60+
is_eng, translated = translate(content)
61+
return {"isEnglish": is_eng, "translatedContent": translated}

0 commit comments

Comments
 (0)