LogicGoInfotechSpaces commited on
Commit
781f734
·
verified ·
1 Parent(s): cd56987

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -34
app.py CHANGED
@@ -3,11 +3,10 @@
3
  Financial Health Score Service (FastAPI)
4
 
5
  Rewrite includes:
6
- - Uses GPT-4.1 with strict JSON schema (response_format)
7
- - OpenAI JSON is guaranteed valid -> uses message.parsed
8
- - Bulletproof Mongo fetch for budgets, transactions, currencies
9
- - Fully rewritten OpenAI call (Option A)
10
- - No messy regex parsing
11
  """
12
 
13
  import json
@@ -193,36 +192,36 @@ def financial_score(payload: ScoreRequest):
193
  prompt = build_prompt(budgets, transactions)
194
 
195
  # ===========================
196
- # STRICT GPT-4.1 JSON (Option A)
197
  # ===========================
198
  try:
199
- response = openai.responses.create(
200
- model="gpt-4o-mini",
201
- temperature=0.6,
202
- response_format={
203
- "type": "json_schema",
204
- "json_schema": {
205
- "name": "financial_score",
206
- "schema": {
207
- "type": "object",
208
- "properties": {
209
- "score": {"type": "number"},
210
- "explanation": {"type": "string"}
211
- },
212
- "required": ["score", "explanation"],
213
- "additionalProperties": False
 
214
  }
215
- }
216
- },
217
- messages=[
218
- {"role": "system", "content": "You are a financial scoring engine."},
219
- {"role": "user", "content": prompt}
220
- ]
221
- )
222
  except Exception as exc:
223
  raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
224
-
225
- # `.output[0].content[0].parsed` contains guaranteed valid JSON
226
  try:
227
  parsed = response.output[0].content[0].parsed
228
  except Exception:
@@ -230,18 +229,17 @@ def financial_score(payload: ScoreRequest):
230
  status_code=502,
231
  detail="OpenAI response did not include parsed JSON (unexpected)"
232
  )
233
-
234
- # Score
235
  score_val = parsed.get("score", 0)
236
  try:
237
  score_val = int(float(score_val))
238
  except:
239
  score_val = 0
240
  score_val = max(0, min(100, score_val))
241
-
242
  return {
243
  "userId": payload.userId,
244
  "score": score_val,
245
  "explanation": parsed.get("explanation")
246
  }
247
-
 
3
  Financial Health Score Service (FastAPI)
4
 
5
  Rewrite includes:
6
+ - Uses GPT-4o-mini with strict JSON schema (response_format)
7
+ - Correct new OpenAI API (`openai.responses.create`)
8
+ - Fully valid `.output[0].content[0].parsed`
9
+ - Bulletproof Mongo fetch + normalization
 
10
  """
11
 
12
  import json
 
192
  prompt = build_prompt(budgets, transactions)
193
 
194
  # ===========================
195
+ # STRICT GPT-4o-mini JSON
196
  # ===========================
197
  try:
198
+ response = openai.responses.create(
199
+ model="gpt-4o-mini",
200
+ temperature=0.6,
201
+ response_format={
202
+ "type": "json_schema",
203
+ "json_schema": {
204
+ "name": "financial_score",
205
+ "schema": {
206
+ "type": "object",
207
+ "properties": {
208
+ "score": {"type": "number"},
209
+ "explanation": {"type": "string"}
210
+ },
211
+ "required": ["score", "explanation"],
212
+ "additionalProperties": False
213
+ }
214
  }
215
+ },
216
+ messages=[
217
+ {"role": "system", "content": "You are a financial scoring engine."},
218
+ {"role": "user", "content": prompt}
219
+ ]
220
+ )
 
221
  except Exception as exc:
222
  raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
223
+
224
+ # GUARANTEED VALID JSON
225
  try:
226
  parsed = response.output[0].content[0].parsed
227
  except Exception:
 
229
  status_code=502,
230
  detail="OpenAI response did not include parsed JSON (unexpected)"
231
  )
232
+
233
+ # Score normalization
234
  score_val = parsed.get("score", 0)
235
  try:
236
  score_val = int(float(score_val))
237
  except:
238
  score_val = 0
239
  score_val = max(0, min(100, score_val))
240
+
241
  return {
242
  "userId": payload.userId,
243
  "score": score_val,
244
  "explanation": parsed.get("explanation")
245
  }