Spaces:
Running
Running
Commit ·
a0ac94b
1
Parent(s): e6902cd
push language update in wizard
Browse files
app.py
CHANGED
|
@@ -7,6 +7,11 @@ from jose import jwt, JWTError
|
|
| 7 |
from supabase_ie import load_history_for_display
|
| 8 |
from datetime import datetime
|
| 9 |
from routes_utils import router as utils_router
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from app_nn import run_chat_app # your Socrates logic
|
| 12 |
|
|
@@ -103,3 +108,78 @@ async def chat_history(req: Request):
|
|
| 103 |
|
| 104 |
return {"messages": safe_messages}
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from supabase_ie import load_history_for_display
|
| 8 |
from datetime import datetime
|
| 9 |
from routes_utils import router as utils_router
|
| 10 |
+
from translate_query_response import (
|
| 11 |
+
detect_language_code,
|
| 12 |
+
translate_to_english,
|
| 13 |
+
translate_from_english,
|
| 14 |
+
)
|
| 15 |
|
| 16 |
from app_nn import run_chat_app # your Socrates logic
|
| 17 |
|
|
|
|
| 108 |
|
| 109 |
return {"messages": safe_messages}
|
| 110 |
|
| 111 |
+
@APP.post("/wizard/detect_language")
|
| 112 |
+
async def wizard_detect_language(req: Request):
|
| 113 |
+
# No JWT required (optional). If you want it protected, reuse verify_jwt.
|
| 114 |
+
data = await req.json()
|
| 115 |
+
text = (data.get("text") or "").strip()
|
| 116 |
+
|
| 117 |
+
if not text:
|
| 118 |
+
return {"ui_lang_code": "en", "language_en": "English"}
|
| 119 |
+
|
| 120 |
+
code = (detect_language_code(text) or "en").lower()
|
| 121 |
+
|
| 122 |
+
# optional: friendly label in English (handy for logs/UI)
|
| 123 |
+
language_en = {
|
| 124 |
+
"en": "English",
|
| 125 |
+
"it": "Italian",
|
| 126 |
+
"es": "Spanish",
|
| 127 |
+
"fr": "French",
|
| 128 |
+
"de": "German",
|
| 129 |
+
}.get(code, "English")
|
| 130 |
+
|
| 131 |
+
return {"ui_lang_code": code, "language_en": language_en}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
@APP.post("/wizard/translate")
|
| 135 |
+
async def wizard_translate(req: Request):
|
| 136 |
+
data = await req.json()
|
| 137 |
+
text = (data.get("text") or "").strip()
|
| 138 |
+
target_lang = (data.get("target_lang") or "en").lower()
|
| 139 |
+
|
| 140 |
+
if not text:
|
| 141 |
+
return {"translated_text": ""}
|
| 142 |
+
|
| 143 |
+
# Your mobile app currently uses this for translating to English before saving.
|
| 144 |
+
# If target_lang is not "en", translate_from_english expects EN input,
|
| 145 |
+
# so for now we mainly support target_lang="en" here.
|
| 146 |
+
if target_lang == "en":
|
| 147 |
+
out = translate_to_english(text)
|
| 148 |
+
return {"translated_text": out}
|
| 149 |
+
|
| 150 |
+
# If you want general translation, you can add a translate_any() function later.
|
| 151 |
+
raise HTTPException(status_code=400, detail="Only target_lang='en' supported")
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
@APP.post("/wizard/strings")
|
| 155 |
+
async def wizard_strings(req: Request):
|
| 156 |
+
data = await req.json()
|
| 157 |
+
ui_lang = (data.get("ui_lang") or "en").lower()
|
| 158 |
+
|
| 159 |
+
# Source-of-truth wizard text in English
|
| 160 |
+
copy_en = {
|
| 161 |
+
"step1_q": "Which language do you prefer for our dialogue?",
|
| 162 |
+
"step1_ph": "e.g. English, Italiano...",
|
| 163 |
+
"step2_q": "By what name shall I address you in our conversations?",
|
| 164 |
+
"step2_ph": "Your name",
|
| 165 |
+
"step3_q": "In what country do you now make your home?",
|
| 166 |
+
"step3_ph": "Country you live in",
|
| 167 |
+
"step4_q": "And from which country do your roots, or your family’s roots, arise?",
|
| 168 |
+
"step4_ph": "Country of origin",
|
| 169 |
+
"step5_q": "Finally, which interests or pursuits most often occupy your thoughts and curiosity?",
|
| 170 |
+
"step5_ph": "Comma separated (e.g. parenting, football, philosophy)",
|
| 171 |
+
"next": "NEXT →",
|
| 172 |
+
"prev": "← PREVIOUS",
|
| 173 |
+
"finish": "FINISH",
|
| 174 |
+
"saving": "SAVING...",
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
if ui_lang == "en":
|
| 178 |
+
return {"copy": copy_en}
|
| 179 |
+
|
| 180 |
+
# translate each value from English into ui_lang
|
| 181 |
+
copy_out = {}
|
| 182 |
+
for k, v in copy_en.items():
|
| 183 |
+
copy_out[k] = translate_from_english(v, ui_lang)
|
| 184 |
+
|
| 185 |
+
return {"copy": copy_out}
|