33import re
44
55def 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
7259def 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