Building the project requires two components: The translation service and the livecaptions project.
In one folder:
python3 -m venv .source bin/activatepip install fastapi uvicorn transformers accelerate sentencepiece
Create the file translate_service.py:
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
from fastapi.middleware.cors import CORSMiddleware # Wichtig: Importiere die Middleware
# Modell laden (einmalig beim Start)
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
# API definieren
app = FastAPI(title="English → German Translation API")
origins = [
"http://localhost", # Wenn du es über http://localhost im Browser öffnest
"http://127.0.0.1", # Manchmal auch als 127.0.0.1 geöffnet
"http://localhost:3000", # Beispiel für eine React/Vue/etc. App
"*", # Der Wildcard-Ursprung (für die Konsole)
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Liste der erlaubten Ursprünge (oder ["*"] für alle)
allow_credentials=True, # Cookies oder Authentifizierungs-Header zulassen (falls nötig)
allow_methods=["*"], # Erlaubt alle HTTP-Methoden (POST, GET, OPTIONS etc.)
allow_headers=["*"], # Erlaubt alle Header, inklusive Content-Type
)
class TranslationRequest(BaseModel):
text: str
@app.post("/translate")
def translate(req: TranslationRequest):
"""Übersetzt einen englischen Satz ins Deutsche."""
result = translator(req.text)
translated_text = result[0]['translation_text']
return {"translated_text": translated_text}
Start the service: uvicorn translate_service:app --host 0.0.0.0 --port 8000
Build my fork of the LiveCaptions project (https://github.com/DaGeRe/LiveCaptions) as described in the README.
The commands are:
wget https://github.com/microsoft/onnxruntime/releases/download/v1.14.1/onnxruntime-linux-x64-1.14.1.tgz
tar -xvf onnxruntime-linux-x64-1.14.1.tgz
# Please export the path as $ONNX_ROOT
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ONNX_ROOT/lib
git clone git@github.com:DaGeRe/LiveCaptions.git
cd LiveCaptions
git submodule update --init --recursive
sudo dnf install libcurl-devel libadwaita-devel meson ninja