Spaces:
Sleeping
Sleeping
rakeshmadasaniai commited on
Commit ·
9cb8e52
1
Parent(s): b12d75f
Fix Streamlit startup order for reliable page load
Browse files- 01-rag-system/app.py +0 -49
- 01-rag-system/core/product_runtime.py +36 -0
01-rag-system/app.py
CHANGED
|
@@ -1,53 +1,4 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
from core.product_runtime import run_product_runtime
|
| 4 |
|
| 5 |
-
if "risk_profile" not in st.session_state:
|
| 6 |
-
st.session_state.risk_profile = None
|
| 7 |
-
|
| 8 |
-
if "investment_goal" not in st.session_state:
|
| 9 |
-
st.session_state.investment_goal = None
|
| 10 |
-
|
| 11 |
-
if "liquidity_need" not in st.session_state:
|
| 12 |
-
st.session_state.liquidity_need = None
|
| 13 |
-
|
| 14 |
-
if "investment_horizon" not in st.session_state:
|
| 15 |
-
st.session_state.investment_horizon = None
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def update_investment_memory(user_query: str) -> None:
|
| 19 |
-
q = user_query.lower()
|
| 20 |
-
|
| 21 |
-
if "conservative" in q:
|
| 22 |
-
st.session_state.risk_profile = "conservative"
|
| 23 |
-
elif "moderate" in q:
|
| 24 |
-
st.session_state.risk_profile = "moderate"
|
| 25 |
-
elif "aggressive" in q:
|
| 26 |
-
st.session_state.risk_profile = "aggressive"
|
| 27 |
-
|
| 28 |
-
if "growth" in q:
|
| 29 |
-
st.session_state.investment_goal = "growth"
|
| 30 |
-
elif "income" in q:
|
| 31 |
-
st.session_state.investment_goal = "income"
|
| 32 |
-
elif "preservation" in q or "protect" in q:
|
| 33 |
-
st.session_state.investment_goal = "capital preservation"
|
| 34 |
-
elif "retirement" in q:
|
| 35 |
-
st.session_state.investment_goal = "retirement"
|
| 36 |
-
|
| 37 |
-
if "1 year" in q or "2 years" in q or "3 years" in q or "short term" in q:
|
| 38 |
-
st.session_state.liquidity_need = "high"
|
| 39 |
-
elif "long term" in q or "10 years" in q or "ten years" in q:
|
| 40 |
-
st.session_state.liquidity_need = "low"
|
| 41 |
-
|
| 42 |
-
if "10 years" in q or "ten years" in q:
|
| 43 |
-
st.session_state.investment_horizon = "10 years"
|
| 44 |
-
elif "5 years" in q or "five years" in q:
|
| 45 |
-
st.session_state.investment_horizon = "5 years"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
pending_query = st.session_state.get("pending_question", "")
|
| 49 |
-
if pending_query:
|
| 50 |
-
update_investment_memory(pending_query)
|
| 51 |
|
| 52 |
run_product_runtime()
|
| 53 |
-
st.stop()
|
|
|
|
|
|
|
|
|
|
| 1 |
from core.product_runtime import run_product_runtime
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
run_product_runtime()
|
|
|
01-rag-system/core/product_runtime.py
CHANGED
|
@@ -101,6 +101,10 @@ def _ensure_state() -> None:
|
|
| 101 |
"model_mode": "OpenAI",
|
| 102 |
"pending_question": "",
|
| 103 |
"last_voice_lang": "",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
}
|
| 105 |
for k, v in defaults.items():
|
| 106 |
if k not in st.session_state:
|
|
@@ -188,6 +192,36 @@ def _response_profile(question: str) -> str:
|
|
| 188 |
return "detailed"
|
| 189 |
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
def _llm_text_call(prompt: str, retrieval: dict, response_language: str, response_profile: str) -> str:
|
| 192 |
from models.openai_mode import generate_openai_response
|
| 193 |
|
|
@@ -486,6 +520,7 @@ def run_product_runtime() -> None:
|
|
| 486 |
# Process deferred generation so the user message appears in history above composer.
|
| 487 |
pending_question = st.session_state.get("pending_question")
|
| 488 |
if pending_question:
|
|
|
|
| 489 |
if base_index is None:
|
| 490 |
base_index = get_base_index()
|
| 491 |
render_assistant_thinking()
|
|
@@ -563,6 +598,7 @@ def run_product_runtime() -> None:
|
|
| 563 |
|
| 564 |
# Guarantee submit uses composer-selected mode (avoids stale sidebar/composer mismatch).
|
| 565 |
st.session_state.model_mode = st.session_state.get("composer_model_mode", st.session_state.model_mode)
|
|
|
|
| 566 |
st.session_state.messages.append({"role": "user", "content": question})
|
| 567 |
_save_active_chat()
|
| 568 |
st.session_state.pending_question = question
|
|
|
|
| 101 |
"model_mode": "OpenAI",
|
| 102 |
"pending_question": "",
|
| 103 |
"last_voice_lang": "",
|
| 104 |
+
"risk_profile": None,
|
| 105 |
+
"investment_goal": None,
|
| 106 |
+
"liquidity_need": None,
|
| 107 |
+
"investment_horizon": None,
|
| 108 |
}
|
| 109 |
for k, v in defaults.items():
|
| 110 |
if k not in st.session_state:
|
|
|
|
| 192 |
return "detailed"
|
| 193 |
|
| 194 |
|
| 195 |
+
def _update_investment_memory(user_query: str) -> None:
|
| 196 |
+
q = user_query.lower()
|
| 197 |
+
|
| 198 |
+
if "conservative" in q:
|
| 199 |
+
st.session_state.risk_profile = "conservative"
|
| 200 |
+
elif "moderate" in q:
|
| 201 |
+
st.session_state.risk_profile = "moderate"
|
| 202 |
+
elif "aggressive" in q:
|
| 203 |
+
st.session_state.risk_profile = "aggressive"
|
| 204 |
+
|
| 205 |
+
if "growth" in q:
|
| 206 |
+
st.session_state.investment_goal = "growth"
|
| 207 |
+
elif "income" in q:
|
| 208 |
+
st.session_state.investment_goal = "income"
|
| 209 |
+
elif "preservation" in q or "protect" in q:
|
| 210 |
+
st.session_state.investment_goal = "capital preservation"
|
| 211 |
+
elif "retirement" in q or "retire" in q:
|
| 212 |
+
st.session_state.investment_goal = "retirement"
|
| 213 |
+
|
| 214 |
+
if "1 year" in q or "2 years" in q or "3 years" in q or "short term" in q:
|
| 215 |
+
st.session_state.liquidity_need = "high"
|
| 216 |
+
elif "long term" in q or "10 years" in q or "ten years" in q:
|
| 217 |
+
st.session_state.liquidity_need = "low"
|
| 218 |
+
|
| 219 |
+
if "10 years" in q or "ten years" in q:
|
| 220 |
+
st.session_state.investment_horizon = "10 years"
|
| 221 |
+
elif "5 years" in q or "five years" in q:
|
| 222 |
+
st.session_state.investment_horizon = "5 years"
|
| 223 |
+
|
| 224 |
+
|
| 225 |
def _llm_text_call(prompt: str, retrieval: dict, response_language: str, response_profile: str) -> str:
|
| 226 |
from models.openai_mode import generate_openai_response
|
| 227 |
|
|
|
|
| 520 |
# Process deferred generation so the user message appears in history above composer.
|
| 521 |
pending_question = st.session_state.get("pending_question")
|
| 522 |
if pending_question:
|
| 523 |
+
_update_investment_memory(pending_question)
|
| 524 |
if base_index is None:
|
| 525 |
base_index = get_base_index()
|
| 526 |
render_assistant_thinking()
|
|
|
|
| 598 |
|
| 599 |
# Guarantee submit uses composer-selected mode (avoids stale sidebar/composer mismatch).
|
| 600 |
st.session_state.model_mode = st.session_state.get("composer_model_mode", st.session_state.model_mode)
|
| 601 |
+
_update_investment_memory(question)
|
| 602 |
st.session_state.messages.append({"role": "user", "content": question})
|
| 603 |
_save_active_chat()
|
| 604 |
st.session_state.pending_question = question
|