Add LibreTranslate module, add separate envs for it in Makefile
This commit is contained in:
55
main.py
55
main.py
@@ -26,6 +26,7 @@ logger = logging.getLogger("pythagoras")
|
||||
app = FastAPI(title="Pythagoras", description="A proxy service handling HTTP and WebSocket connections")
|
||||
app.state.auto_polling = False
|
||||
app.state.polling_rate = 5
|
||||
app.state.enable_libretranslate = True
|
||||
|
||||
# Define the media directory
|
||||
MEDIA_DIR = Path("./media")
|
||||
@@ -115,6 +116,11 @@ async def control_endpoint(request: Request):
|
||||
app.state.auto_polling = new_state
|
||||
logger.info(f"Polling command issued, changing auto-polling to {new_state}")
|
||||
|
||||
elif data['command'] == "setlibretranslate" and 'state' in data:
|
||||
new_state = data['state']
|
||||
app.state.auto_polling = new_state
|
||||
logger.info(f"LibreTranslate command issued, changing state to {new_state}")
|
||||
|
||||
elif data['command'] == "autopollingrate" and 'rate' in data:
|
||||
new_rate = data['rate']
|
||||
app.state.polling_rate = new_rate
|
||||
@@ -184,12 +190,16 @@ async def get_media(file_path: str):
|
||||
async def process_subtitles(request: Request, sub_type: str):
|
||||
try:
|
||||
text_content = await request.body()
|
||||
subtitle_text = text_content.decode("utf-8")
|
||||
logger.info(f"Received subtitle text: {subtitle_text}, request type: {sub_type}")
|
||||
en_subtitle_text = text_content.decode("utf-8")
|
||||
logger.info(f"Received subtitle text: {en_subtitle_text}, request type: {sub_type}")
|
||||
|
||||
if manager.active_connections:
|
||||
await manager.broadcast_binary({"type": f"subtitle_{sub_type}", "text": subtitle_text})
|
||||
|
||||
await manager.broadcast_binary({"type": f"subtitle_en_{sub_type}", "text": en_subtitle_text})
|
||||
|
||||
if app.state.enable_libretranslate and sub_type == "submit_sentence":
|
||||
cs_subtitle_text = await translate_to_cs_libre(en_subtitle_text)
|
||||
await manager.broadcast_binary({"type": f"subtitle_cs_{sub_type}", "text": cs_subtitle_text})
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "success", "message": "Subtitle text received"}
|
||||
@@ -201,6 +211,43 @@ async def process_subtitles(request: Request, sub_type: str):
|
||||
content={"status": "error", "message": f"Failed to process request."}
|
||||
)
|
||||
|
||||
async def translate_to_cs_libre(text: str):
|
||||
"""
|
||||
Translates the provided text from English to Czech using LibreTranslate.
|
||||
"""
|
||||
if not text:
|
||||
return text
|
||||
|
||||
try:
|
||||
url = "http://localhost:5000/translate"
|
||||
|
||||
payload = {
|
||||
"q": text,
|
||||
"source": "en",
|
||||
"target": "cs",
|
||||
"format": "text"
|
||||
}
|
||||
|
||||
timeout = httpx.Timeout(10.0)
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
response = await client.post(url, json=payload)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
translated_text = result.get("translatedText", text)
|
||||
logger.info(f"Successfully translated text to Czech")
|
||||
return translated_text
|
||||
else:
|
||||
logger.error(f"Translation API error: {response.status_code}, {response.text}")
|
||||
return text
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Translation error: {str(e)}")
|
||||
return text
|
||||
|
||||
|
||||
|
||||
async def fetch_selected_message():
|
||||
"""
|
||||
Fetches a selected message from the specified endpoint.
|
||||
|
Reference in New Issue
Block a user