Files

44 lines
1.2 KiB
Python
Raw Permalink Normal View History

2026-05-31 18:45:38 +08:00
import sys
import subprocess
import re
import io
# Fix Windows console encoding
if sys.platform == 'win32':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
def translate(text):
"""Translate text to Russian using ollama translator model"""
try:
# Run ollama command
result = subprocess.run(
['ollama', 'run', 'translator', text],
capture_output=True,
text=True,
encoding='utf-8'
)
# Get output
output = result.stdout
# Remove <think>...</think> tags and their content
output = re.sub(r'<think>.*?</think>', '', output, flags=re.DOTALL)
# Remove empty lines and strip whitespace
output = '\n'.join(line.strip() for line in output.split('\n') if line.strip())
return output
except Exception as e:
return f"Error: {e}"
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python translate.py \"text to translate\"")
sys.exit(1)
text = ' '.join(sys.argv[1:])
result = translate(text)
print(result)