Spaces:
Running
Running
Commit ·
7aad985
1
Parent(s): a0ac94b
debug news fetch
Browse files
app.py
CHANGED
|
@@ -78,6 +78,14 @@ async def chat_send(req: Request):
|
|
| 78 |
user_msg=message,
|
| 79 |
)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
return {"reply": reply, "user_id": user_id}
|
| 82 |
|
| 83 |
@APP.post("/chat/history")
|
|
@@ -108,78 +116,3 @@ async def chat_history(req: Request):
|
|
| 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}
|
|
|
|
| 78 |
user_msg=message,
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Strip non-JSON stuff safely (without breaking internal use)
|
| 82 |
+
reply = run_chat_app(...)
|
| 83 |
+
|
| 84 |
+
if isinstance(reply, dict):
|
| 85 |
+
news = reply.get("news_fetch")
|
| 86 |
+
if isinstance(news, dict):
|
| 87 |
+
news.pop("faiss_object", None)
|
| 88 |
+
|
| 89 |
return {"reply": reply, "user_id": user_id}
|
| 90 |
|
| 91 |
@APP.post("/chat/history")
|
|
|
|
| 116 |
|
| 117 |
return {"messages": safe_messages}
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|