Hide admin section in sidebar, optimize translation service with parallel processing, improve UX
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
"""
|
||||
Translation Service Abstraction
|
||||
Provides a unified interface for different translation providers
|
||||
Optimized for high performance with parallel processing
|
||||
"""
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional, List, Dict
|
||||
from typing import Optional, List, Dict, Tuple
|
||||
import requests
|
||||
from deep_translator import GoogleTranslator, DeeplTranslator, LibreTranslator
|
||||
from config import config
|
||||
import concurrent.futures
|
||||
import threading
|
||||
import asyncio
|
||||
from functools import lru_cache
|
||||
import time
|
||||
|
||||
|
||||
# Global thread pool for parallel translations
|
||||
_executor = concurrent.futures.ThreadPoolExecutor(max_workers=8)
|
||||
|
||||
|
||||
class TranslationProvider(ABC):
|
||||
@@ -22,6 +30,36 @@ class TranslationProvider(ABC):
|
||||
def translate_batch(self, texts: List[str], target_language: str, source_language: str = 'auto') -> List[str]:
|
||||
"""Translate multiple texts at once - default implementation"""
|
||||
return [self.translate(text, target_language, source_language) for text in texts]
|
||||
|
||||
def translate_batch_parallel(self, texts: List[str], target_language: str, source_language: str = 'auto', max_workers: int = 4) -> List[str]:
|
||||
"""Parallel batch translation using thread pool"""
|
||||
if not texts:
|
||||
return []
|
||||
|
||||
results = [''] * len(texts)
|
||||
non_empty = [(i, t) for i, t in enumerate(texts) if t and t.strip()]
|
||||
|
||||
if not non_empty:
|
||||
return [t if t else '' for t in texts]
|
||||
|
||||
def translate_one(item: Tuple[int, str]) -> Tuple[int, str]:
|
||||
idx, text = item
|
||||
try:
|
||||
return (idx, self.translate(text, target_language, source_language))
|
||||
except Exception as e:
|
||||
print(f"Translation error at index {idx}: {e}")
|
||||
return (idx, text)
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
for idx, translated in executor.map(translate_one, non_empty):
|
||||
results[idx] = translated
|
||||
|
||||
# Fill empty positions
|
||||
for i, text in enumerate(texts):
|
||||
if not text or not text.strip():
|
||||
results[i] = text if text else ''
|
||||
|
||||
return results
|
||||
|
||||
|
||||
class GoogleTranslationProvider(TranslationProvider):
|
||||
|
||||
Reference in New Issue
Block a user