Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +59 -63
src/streamlit_app.py
CHANGED
|
@@ -5,24 +5,23 @@ import io
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
# ==========================================
|
| 8 |
-
# 1.
|
| 9 |
# ==========================================
|
| 10 |
st.set_page_config(page_title="CX Architect Auto-Mapper", layout="wide", page_icon="ποΈ")
|
| 11 |
st.title("ποΈ CX Architect: Omni-Channel Generator")
|
| 12 |
st.markdown("Upload your PRD, VOC data, or an existing spreadsheet. The AI engine will instantly map the customer journey and generate a structured VA Design Sheet.")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
api_key = ""
|
| 16 |
demo_mode = False
|
| 17 |
|
| 18 |
-
# Sidebar Configuration Panel
|
| 19 |
with st.sidebar:
|
| 20 |
st.header("βοΈ Configuration")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
demo_mode = st.checkbox("π Activate Demo Simulation Mode", value=False)
|
| 24 |
|
| 25 |
-
# Render key input based on the chosen mode
|
| 26 |
if not demo_mode:
|
| 27 |
api_key = st.text_input("Enter Gemini API Key", type="password", placeholder="Paste your AIza... key here")
|
| 28 |
if api_key and api_key.startswith("AQ."):
|
|
@@ -37,10 +36,10 @@ with st.sidebar:
|
|
| 37 |
uploaded_file = st.file_uploader("Drop your Lending Document here (PDF, TXT, or CSV)", type=["pdf", "txt", "csv"])
|
| 38 |
|
| 39 |
# ==========================================
|
| 40 |
-
# 2. FAIL-SAFE
|
| 41 |
# ==========================================
|
| 42 |
def get_simulation_assets():
|
| 43 |
-
"""Returns
|
| 44 |
flow_text = (
|
| 45 |
"π MILESTONE 1: USER_AUTHENTICATION\n"
|
| 46 |
" - Happy Path: Identity verified instantly via core onboarding APIs.\n"
|
|
@@ -70,13 +69,12 @@ def get_simulation_assets():
|
|
| 70 |
return flow_text, pd.DataFrame(rows, columns=headers)
|
| 71 |
|
| 72 |
def safe_read_file(file):
|
| 73 |
-
name = file.name
|
| 74 |
try:
|
| 75 |
-
if name.endswith('.pdf'):
|
| 76 |
import PyPDF2
|
| 77 |
pdf_reader = PyPDF2.PdfReader(io.BytesIO(file.getvalue()))
|
| 78 |
return "".join([page.extract_text() or "" for page in pdf_reader.pages])
|
| 79 |
-
elif name.endswith('.csv'):
|
| 80 |
df = pd.read_csv(io.BytesIO(file.getvalue()), errors='ignore')
|
| 81 |
return df.to_string()
|
| 82 |
else:
|
|
@@ -85,80 +83,78 @@ def safe_read_file(file):
|
|
| 85 |
return f"Error: {str(e)}"
|
| 86 |
|
| 87 |
# ==========================================
|
| 88 |
-
# 3. RUNTIME CONTROLLER
|
| 89 |
# ==========================================
|
| 90 |
if st.button("π Generate Design Sheets & Flow"):
|
|
|
|
| 91 |
if not demo_mode and not api_key:
|
| 92 |
st.error("β οΈ Configuration Key Required! Please input your Gemini API Key or activate Demo Simulation Mode in the sidebar.")
|
| 93 |
-
elif not uploaded_file:
|
| 94 |
st.error("β οΈ Data Source Missing! Please upload a PDF, TXT, or CSV file to analyze.")
|
| 95 |
else:
|
| 96 |
-
with st.spinner("Processing
|
| 97 |
try:
|
| 98 |
flow_output = ""
|
| 99 |
dataframe_output = None
|
| 100 |
|
| 101 |
-
# SCENARIO A: Direct
|
| 102 |
if demo_mode:
|
| 103 |
flow_output, dataframe_output = get_simulation_assets()
|
| 104 |
|
| 105 |
-
# SCENARIO B:
|
| 106 |
-
elif uploaded_file.name.endswith('.csv'):
|
| 107 |
-
raw_df = pd.read_csv(io.BytesIO(uploaded_file.getvalue()), errors='ignore')
|
| 108 |
-
dataframe_output = raw_df
|
| 109 |
-
flow_output = f"π Native Document File Detected: '{uploaded_file.name}'\nSuccessfully loaded {len(raw_df)} configuration layout rows directly into the dashboard matrix."
|
| 110 |
-
|
| 111 |
-
# SCENARIO C: Live AI Extraction Core
|
| 112 |
else:
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
data_rows = [[cell.strip() for cell in r.split('|')] for r in table_lines[1:] if '|' in r]
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# ==========================================
|
| 154 |
-
# 4.
|
| 155 |
# ==========================================
|
| 156 |
-
st.success("π― Assets
|
| 157 |
tab1, tab2 = st.tabs(["πΊοΈ Visual Journey Path", "π VA Design Sheet (Excel)"])
|
| 158 |
|
| 159 |
with tab1:
|
| 160 |
st.subheader("Process Flow Optimization Strategy")
|
| 161 |
-
st.info("The structural user-journey steps mapped out from your document parameters:")
|
| 162 |
st.code(flow_output, language="text")
|
| 163 |
|
| 164 |
with tab2:
|
|
@@ -174,7 +170,7 @@ if st.button("π Generate Design Sheets & Flow"):
|
|
| 174 |
mime='text/csv',
|
| 175 |
)
|
| 176 |
else:
|
| 177 |
-
st.warning("Data rows could not be arranged into a standard matrix
|
| 178 |
|
| 179 |
except Exception as runtime_err:
|
| 180 |
st.error(f"Execution Interrupted: {str(runtime_err)}")
|
|
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
# ==========================================
|
| 8 |
+
# 1. UI CONFIGURATION & INTERFACE SETUP
|
| 9 |
# ==========================================
|
| 10 |
st.set_page_config(page_title="CX Architect Auto-Mapper", layout="wide", page_icon="ποΈ")
|
| 11 |
st.title("ποΈ CX Architect: Omni-Channel Generator")
|
| 12 |
st.markdown("Upload your PRD, VOC data, or an existing spreadsheet. The AI engine will instantly map the customer journey and generate a structured VA Design Sheet.")
|
| 13 |
|
| 14 |
+
# Global state variables initialization
|
| 15 |
api_key = ""
|
| 16 |
demo_mode = False
|
| 17 |
|
| 18 |
+
# Sidebar Configuration Panel
|
| 19 |
with st.sidebar:
|
| 20 |
st.header("βοΈ Configuration")
|
| 21 |
|
| 22 |
+
# 100% stable checkbox for system compatibility
|
| 23 |
demo_mode = st.checkbox("π Activate Demo Simulation Mode", value=False)
|
| 24 |
|
|
|
|
| 25 |
if not demo_mode:
|
| 26 |
api_key = st.text_input("Enter Gemini API Key", type="password", placeholder="Paste your AIza... key here")
|
| 27 |
if api_key and api_key.startswith("AQ."):
|
|
|
|
| 36 |
uploaded_file = st.file_uploader("Drop your Lending Document here (PDF, TXT, or CSV)", type=["pdf", "txt", "csv"])
|
| 37 |
|
| 38 |
# ==========================================
|
| 39 |
+
# 2. FAIL-SAFE DATA MATRICES
|
| 40 |
# ==========================================
|
| 41 |
def get_simulation_assets():
|
| 42 |
+
"""Returns layout datasets modeled directly from real lending configurations"""
|
| 43 |
flow_text = (
|
| 44 |
"π MILESTONE 1: USER_AUTHENTICATION\n"
|
| 45 |
" - Happy Path: Identity verified instantly via core onboarding APIs.\n"
|
|
|
|
| 69 |
return flow_text, pd.DataFrame(rows, columns=headers)
|
| 70 |
|
| 71 |
def safe_read_file(file):
|
|
|
|
| 72 |
try:
|
| 73 |
+
if file.name.endswith('.pdf'):
|
| 74 |
import PyPDF2
|
| 75 |
pdf_reader = PyPDF2.PdfReader(io.BytesIO(file.getvalue()))
|
| 76 |
return "".join([page.extract_text() or "" for page in pdf_reader.pages])
|
| 77 |
+
elif file.name.endswith('.csv'):
|
| 78 |
df = pd.read_csv(io.BytesIO(file.getvalue()), errors='ignore')
|
| 79 |
return df.to_string()
|
| 80 |
else:
|
|
|
|
| 83 |
return f"Error: {str(e)}"
|
| 84 |
|
| 85 |
# ==========================================
|
| 86 |
+
# 3. RUNTIME OPERATION CONTROLLER
|
| 87 |
# ==========================================
|
| 88 |
if st.button("π Generate Design Sheets & Flow"):
|
| 89 |
+
# Fail-safe condition adjustment: Bypasses file validation rules if user has Demo Mode activated
|
| 90 |
if not demo_mode and not api_key:
|
| 91 |
st.error("β οΈ Configuration Key Required! Please input your Gemini API Key or activate Demo Simulation Mode in the sidebar.")
|
| 92 |
+
elif not demo_mode and not uploaded_file:
|
| 93 |
st.error("β οΈ Data Source Missing! Please upload a PDF, TXT, or CSV file to analyze.")
|
| 94 |
else:
|
| 95 |
+
with st.spinner("Processing customer journey parameters..."):
|
| 96 |
try:
|
| 97 |
flow_output = ""
|
| 98 |
dataframe_output = None
|
| 99 |
|
| 100 |
+
# SCENARIO A: Direct Demo Mode (No File or Key requirements enforced)
|
| 101 |
if demo_mode:
|
| 102 |
flow_output, dataframe_output = get_simulation_assets()
|
| 103 |
|
| 104 |
+
# SCENARIO B: Live Data Extraction Pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
+
if uploaded_file.name.endswith('.csv'):
|
| 107 |
+
raw_df = pd.read_csv(io.BytesIO(uploaded_file.getvalue()), errors='ignore')
|
| 108 |
+
dataframe_output = raw_df
|
| 109 |
+
flow_output = f"π Loaded configuration layout rows directly from '{uploaded_file.name}'."
|
| 110 |
+
else:
|
| 111 |
+
doc_text = safe_read_file(uploaded_file)
|
| 112 |
+
genai.configure(api_key=api_key)
|
| 113 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 114 |
+
|
| 115 |
+
prompt = f"""
|
| 116 |
+
You are an elite Fintech CX Architect mapping a Virtual Assistant flow for Loan Against Mutual Funds (LAMF).
|
| 117 |
+
Output exactly two structural segments inside these XML tag elements. Do not use markdown backticks.
|
| 118 |
+
|
| 119 |
+
<FLOW>
|
| 120 |
+
List the Happy Path milestones, and then list the Unhappy Path scenarios along with recommended Bot Actions.
|
| 121 |
+
</FLOW>
|
| 122 |
+
|
| 123 |
+
<TABLE>
|
| 124 |
+
Current Milestone | Scenario | Context Message | User Selection | Short Note | Ticket Creation | Group | Issue / GTIS | TAG
|
| 125 |
+
Provide at least 4 custom rows separated by standard pipe characters (|) matching this sequence.
|
| 126 |
+
</TABLE>
|
| 127 |
+
|
| 128 |
+
Data text: {doc_text}
|
| 129 |
+
"""
|
| 130 |
+
ai_response = model.generate_content(prompt).text
|
| 131 |
+
|
| 132 |
+
flow_match = re.search(r"<FLOW>(.*?)</FLOW>", ai_response, re.DOTALL)
|
| 133 |
+
table_match = re.search(r"<TABLE>(.*?)</TABLE>", ai_response, re.DOTALL)
|
| 134 |
+
|
| 135 |
+
flow_output = flow_match.group(1).strip() if flow_match else "Journey breakdown completed."
|
| 136 |
+
raw_table_str = table_match.group(1).strip() if table_match else ""
|
|
|
|
| 137 |
|
| 138 |
+
table_lines = [l.strip() for l in raw_table_str.split('\n') if l.strip() and '---' not in l]
|
| 139 |
+
if table_lines:
|
| 140 |
+
headers = [h.strip() for h in table_lines[0].split('|')]
|
| 141 |
+
data_rows = [[cell.strip() for cell in r.split('|')] for r in table_lines[1:] if '|' in r]
|
| 142 |
+
|
| 143 |
+
clean_rows = []
|
| 144 |
+
for row in data_rows:
|
| 145 |
+
if len(row) < len(headers): row += ["NA"] * (len(headers) - len(row))
|
| 146 |
+
elif len(row) > len(headers): row = row[:len(headers)]
|
| 147 |
+
clean_rows.append(row)
|
| 148 |
+
dataframe_output = pd.DataFrame(clean_rows, columns=headers)
|
| 149 |
|
| 150 |
# ==========================================
|
| 151 |
+
# 4. OUTPUT DISPLAY LAYOUT
|
| 152 |
# ==========================================
|
| 153 |
+
st.success("π― Omni-Channel Assets Rendered Successfully!")
|
| 154 |
tab1, tab2 = st.tabs(["πΊοΈ Visual Journey Path", "π VA Design Sheet (Excel)"])
|
| 155 |
|
| 156 |
with tab1:
|
| 157 |
st.subheader("Process Flow Optimization Strategy")
|
|
|
|
| 158 |
st.code(flow_output, language="text")
|
| 159 |
|
| 160 |
with tab2:
|
|
|
|
| 170 |
mime='text/csv',
|
| 171 |
)
|
| 172 |
else:
|
| 173 |
+
st.warning("Data rows could not be arranged into a standard matrix format layout.")
|
| 174 |
|
| 175 |
except Exception as runtime_err:
|
| 176 |
st.error(f"Execution Interrupted: {str(runtime_err)}")
|