feat: complete translator stack
This commit is contained in:
commit
6fe7c5121b
9946 files changed
+1747495
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Helpers for ETA, config, and state tracking will live here."""
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
LOG_FILE = Path("backend.log")
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
handlers=[
|
||||
logging.StreamHandler(),
|
||||
logging.FileHandler(LOG_FILE, encoding="utf-8"),
|
||||
],
|
||||
)
|
||||
|
||||
logger = logging.getLogger("traduttore")
|
||||
@@ -0,0 +1,10 @@
|
||||
from pathlib import Path
|
||||
|
||||
OUTPUT_BASE = Path("_bmad-output").resolve()
|
||||
OUTPUT_BASE.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
def write_final_document(session_id: str, content: str) -> Path:
|
||||
target = OUTPUT_BASE / f"translated-{session_id}.txt"
|
||||
target.write_text(content, encoding="utf-8")
|
||||
return target
|
||||
@@ -0,0 +1,37 @@
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
def _build_prompt(source_lang: str, target_lang: str, text: str) -> str:
|
||||
return (
|
||||
f"Traduci dal {source_lang} al {target_lang}:\\n" f"{text}"
|
||||
)
|
||||
|
||||
|
||||
class OllamaClient:
|
||||
def __init__(self, base_url: str, model: str):
|
||||
self._base_url = base_url.rstrip("/")
|
||||
self._model = model
|
||||
self._client = httpx.AsyncClient(base_url=self._base_url, timeout=60.0)
|
||||
|
||||
async def translate(self, text: str, source_lang: str, target_lang: str) -> str:
|
||||
payload = {
|
||||
"model": self._model,
|
||||
"prompt": _build_prompt(source_lang or "auto", target_lang, text),
|
||||
"stream": False,
|
||||
}
|
||||
try:
|
||||
resp = await self._client.post("/api/chat", json=payload)
|
||||
resp.raise_for_status()
|
||||
body = resp.json()
|
||||
except httpx.HTTPError as exc:
|
||||
raise RuntimeError(f"Errore Ollama: {exc}") from exc
|
||||
|
||||
if isinstance(body, dict):
|
||||
message = body.get("response") or ""
|
||||
return message.strip()
|
||||
return ""
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._client.aclose()
|
||||
@@ -0,0 +1,23 @@
|
||||
from typing import Dict, Optional
|
||||
|
||||
from backend.app.models.session import SessionState
|
||||
|
||||
|
||||
class SessionStore:
|
||||
def __init__(self):
|
||||
self._sessions: Dict[str, SessionState] = {}
|
||||
|
||||
def create(self, state: SessionState) -> None:
|
||||
self._sessions[state.session_id] = state
|
||||
|
||||
def get(self, session_id: str) -> Optional[SessionState]:
|
||||
return self._sessions.get(session_id)
|
||||
|
||||
def update(self, state: SessionState) -> None:
|
||||
self._sessions[state.session_id] = state
|
||||
|
||||
def remove(self, session_id: str) -> None:
|
||||
self._sessions.pop(session_id, None)
|
||||
|
||||
|
||||
session_store = SessionStore()
|
||||
Reference in new issue
Block a user