Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +18 -15
src/streamlit_app.py
CHANGED
|
@@ -23,15 +23,15 @@ def process_loan_data(df: pd.DataFrame):
|
|
| 23 |
return ecl_df.reset_index()
|
| 24 |
|
| 25 |
def get_gemini_decision(segment, pd_val, lgd_val, ead_val, ecl_val):
|
| 26 |
-
"""Gemini-backed risk decision
|
| 27 |
-
model = genai.GenerativeModel("gemini-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
Segment: {segment}
|
| 36 |
PD: {pd_val:.3f}
|
| 37 |
LGD: {lgd_val:.3f}
|
|
@@ -40,26 +40,29 @@ ECL: {ecl_val:,.0f}
|
|
| 40 |
|
| 41 |
Rules:
|
| 42 |
- PD > 0.25 ⇒ increase_interest
|
| 43 |
-
- PD
|
| 44 |
- PD < 0.15 ⇒ maintain
|
|
|
|
| 45 |
Respond with one JSON object only.
|
| 46 |
"""
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
resp = model.generate_content(
|
| 50 |
-
[{"role": "system", "parts": [system_prompt]},
|
| 51 |
-
{"role": "user", "parts": [user_prompt]}],
|
| 52 |
-
generation_config={"temperature": 0.1}
|
| 53 |
-
)
|
| 54 |
text = resp.text.strip()
|
|
|
|
|
|
|
| 55 |
text = re.sub(r"^```json", "", text)
|
| 56 |
text = re.sub(r"^```", "", text)
|
| 57 |
text = re.sub(r"```$", "", text)
|
|
|
|
|
|
|
| 58 |
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 59 |
if match:
|
| 60 |
text = match.group(0)
|
|
|
|
| 61 |
data = json.loads(text)
|
| 62 |
return data
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
st.warning(f"⚠️ Gemini output parse failed: {e}")
|
| 65 |
st.text_area("Raw Gemini output", value=resp.text if 'resp' in locals() else "No response", height=150)
|
|
|
|
| 23 |
return ecl_df.reset_index()
|
| 24 |
|
| 25 |
def get_gemini_decision(segment, pd_val, lgd_val, ead_val, ecl_val):
|
| 26 |
+
"""Gemini-backed risk decision. Single call, valid for all SDK versions."""
|
| 27 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 28 |
+
|
| 29 |
+
prompt = f"""
|
| 30 |
+
You are a financial risk advisor.
|
| 31 |
+
Analyze the following loan segment and return ONLY valid JSON.
|
| 32 |
+
Schema:
|
| 33 |
+
{{"action":"increase_interest"|"reduce_disbursement"|"maintain","rationale":"string","confidence":float}}
|
| 34 |
+
|
| 35 |
Segment: {segment}
|
| 36 |
PD: {pd_val:.3f}
|
| 37 |
LGD: {lgd_val:.3f}
|
|
|
|
| 40 |
|
| 41 |
Rules:
|
| 42 |
- PD > 0.25 ⇒ increase_interest
|
| 43 |
+
- 0.20 ≤ PD ≤ 0.25 ⇒ reduce_disbursement
|
| 44 |
- PD < 0.15 ⇒ maintain
|
| 45 |
+
|
| 46 |
Respond with one JSON object only.
|
| 47 |
"""
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
resp = model.generate_content(prompt, generation_config={"temperature": 0.1})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
text = resp.text.strip()
|
| 52 |
+
|
| 53 |
+
# clean markdown fences if any
|
| 54 |
text = re.sub(r"^```json", "", text)
|
| 55 |
text = re.sub(r"^```", "", text)
|
| 56 |
text = re.sub(r"```$", "", text)
|
| 57 |
+
|
| 58 |
+
# extract first {...} JSON block
|
| 59 |
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 60 |
if match:
|
| 61 |
text = match.group(0)
|
| 62 |
+
|
| 63 |
data = json.loads(text)
|
| 64 |
return data
|
| 65 |
+
|
| 66 |
except Exception as e:
|
| 67 |
st.warning(f"⚠️ Gemini output parse failed: {e}")
|
| 68 |
st.text_area("Raw Gemini output", value=resp.text if 'resp' in locals() else "No response", height=150)
|