Add system prompt, glossary, presets for Ollama/WebLLM, image translation support
This commit is contained in:
@@ -9,7 +9,11 @@ from docx.table import Table, _Cell
|
||||
from docx.oxml.text.paragraph import CT_P
|
||||
from docx.oxml.table import CT_Tbl
|
||||
from docx.section import Section
|
||||
from docx.shared import Inches, Pt
|
||||
from docx.oxml.ns import qn
|
||||
from services.translation_service import translation_service
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
|
||||
class WordTranslator:
|
||||
@@ -39,11 +43,66 @@ class WordTranslator:
|
||||
for section in document.sections:
|
||||
self._translate_section(section, target_language)
|
||||
|
||||
# Translate images if enabled
|
||||
if getattr(self.translation_service, 'translate_images', False):
|
||||
self._translate_images(document, target_language, input_path)
|
||||
|
||||
# Save the translated document
|
||||
document.save(output_path)
|
||||
|
||||
return output_path
|
||||
|
||||
def _translate_images(self, document: Document, target_language: str, input_path: Path):
|
||||
"""
|
||||
Extract text from images and add translations as captions
|
||||
"""
|
||||
from services.translation_service import OllamaTranslationProvider
|
||||
|
||||
# Only works with Ollama vision
|
||||
if not isinstance(self.translation_service.provider, OllamaTranslationProvider):
|
||||
return
|
||||
|
||||
try:
|
||||
import zipfile
|
||||
import base64
|
||||
|
||||
# Extract images from docx (it's a zip file)
|
||||
with zipfile.ZipFile(input_path, 'r') as zip_ref:
|
||||
image_files = [f for f in zip_ref.namelist() if f.startswith('word/media/')]
|
||||
|
||||
for idx, image_file in enumerate(image_files):
|
||||
try:
|
||||
# Extract image
|
||||
image_data = zip_ref.read(image_file)
|
||||
|
||||
# Create temp file
|
||||
ext = os.path.splitext(image_file)[1]
|
||||
with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as tmp:
|
||||
tmp.write(image_data)
|
||||
tmp_path = tmp.name
|
||||
|
||||
# Translate image with vision
|
||||
translated_text = self.translation_service.provider.translate_image(tmp_path, target_language)
|
||||
|
||||
# Clean up temp file
|
||||
os.unlink(tmp_path)
|
||||
|
||||
if translated_text and translated_text.strip():
|
||||
# Add translated text as a new paragraph after image
|
||||
# We'll add it at the end with a note
|
||||
p = document.add_paragraph()
|
||||
p.add_run(f"[Image {idx + 1} translation: ").bold = True
|
||||
p.add_run(translated_text)
|
||||
p.add_run("]").bold = True
|
||||
|
||||
print(f"Translated image {idx + 1}: {translated_text[:50]}...")
|
||||
except Exception as e:
|
||||
print(f"Error translating image {image_file}: {e}")
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing images: {e}")
|
||||
|
||||
def _translate_document_body(self, document: Document, target_language: str):
|
||||
"""
|
||||
Translate all elements in the document body
|
||||
|
||||
Reference in New Issue
Block a user