Socrates_docker / routes_wizard.py
alesamodio's picture
add wizard
bc4388d
# routes_wizard.py
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from translate_query_response import translate_to_english, translate_from_english
router = APIRouter(prefix="/wizard", tags=["wizard"])
# ---- Modello richiesta ----
class StringsReq(BaseModel):
ui_lang: str
class TranslateReq(BaseModel):
text: str
target_lang: str = "en"
# ---- Le stringhe base EN (uguali alle tue in index.tsx) ----
WIZARD_COPY_EN = {
"step1_q": "Which language do you prefer for our dialogue?",
"step1_ph": "e.g. English, Italiano...",
"step2_q": "By what name shall I address you in our conversations?",
"step2_ph": "Your name",
"step3_q": "In what country do you now make your home?",
"step3_ph": "Country you live in",
"step4_q": "And from which country do your roots, or your family’s roots, arise?",
"step4_ph": "Country of origin",
"step5_q": "Finally, which interests or pursuits most often occupy your thoughts and curiosity?",
"step5_ph": "Comma separated (e.g. parenting, football, philosophy)",
"next": "NEXT →",
"prev": "← PREVIOUS",
"finish": "FINISH",
"saving": "SAVING...",
}
@router.post("/strings")
async def wizard_strings(req: StringsReq):
ui_lang = (req.ui_lang or "en").lower().strip()
if ui_lang == "en":
return {"copy": WIZARD_COPY_EN}
# Traduci ogni campo dall'inglese alla lingua richiesta
out = {}
for k, v in WIZARD_COPY_EN.items():
try:
out[k] = translate_from_english(v, ui_lang)
except Exception:
out[k] = v # fallback EN se qualcosa va storto
return {"copy": out}
@router.post("/translate")
async def wizard_translate(req: TranslateReq):
text = (req.text or "").strip()
if not text:
return {"translated_text": ""}
target = (req.target_lang or "en").lower().strip()
try:
if target == "en":
return {"translated_text": translate_to_english(text)}
else:
# se ti serve anche tradurre verso altre lingue
en = translate_to_english(text)
return {"translated_text": translate_from_english(en, target)}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))