sammy786 commited on
Commit
0fb0073
Β·
verified Β·
1 Parent(s): 659671f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -40
app.py CHANGED
@@ -179,41 +179,112 @@ def get_recommendation_with_agent(user_id, merchant, category, amount):
179
 
180
  result = response.json()
181
 
182
- # Extract agent data safely
183
- recommended_card = safe_get(result, 'recommended_card', {})
184
- if isinstance(recommended_card, dict):
185
- card_name = safe_get(recommended_card, 'card_name', 'Unknown Card')
186
- else:
187
- card_name = str(recommended_card)
 
188
 
189
- # Format response with agent insights
190
- output = f"""
191
- ## πŸ€– AI Agent Recommendation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  ### πŸ’³ Recommended Card: **{card_name}**
194
 
195
- ### 🧠 Agent's Reasoning:
196
- {safe_get(result, 'final_recommendation', 'No reasoning provided')}
197
 
198
  ---
199
 
200
- ### πŸ“Š Quick Stats:
201
- - **Amount**: ${safe_get(result, 'amount_usd', amount):.2f}
202
- - **Merchant**: {safe_get(result, 'merchant', merchant)}
203
- - **Category**: {safe_get(result, 'category', category)}
204
- - **Services Used**: {', '.join(safe_get(result, 'services_used', []))}
205
- - **Processing Time**: {safe_get(result, 'orchestration_time_ms', 0):.0f}ms
206
 
 
 
 
 
 
 
 
 
 
 
207
  ---
208
 
209
- ### πŸ’‘ Why This Card?
210
- The AI agent analyzed your transaction context, card portfolio, and spending patterns to determine this is your optimal choice.
 
 
211
 
212
- **Confidence Level**: High βœ…
 
 
 
213
  """
214
 
215
- # Create simple comparison chart
216
- chart = create_agent_recommendation_chart(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
 
218
  yield output, chart
219
 
@@ -223,40 +294,59 @@ The AI agent analyzed your transaction context, card portfolio, and spending pat
223
  import traceback
224
  error_details = traceback.format_exc()
225
  print(f"Agent recommendation error: {error_details}")
226
- yield f"❌ **Error**: {str(e)}\n\nPlease check the orchestrator service or try again.", None
227
-
228
 
229
  def create_agent_recommendation_chart(result: Dict) -> go.Figure:
230
- """Create simple chart showing agent's recommendation"""
231
  try:
232
- # Extract card info
233
- recommended = result.get('recommended_card', {})
234
- alternatives = result.get('alternative_cards', [])[:2] # Top 2 alternatives
235
 
236
- if isinstance(recommended, dict):
237
- rec_name = recommended.get('card_name', 'Recommended Card')
238
- rec_reward = recommended.get('reward_amount', 0)
239
- else:
240
- rec_name = str(recommended)
241
- rec_reward = 0
 
242
 
243
  cards = [rec_name]
244
  rewards = [rec_reward]
245
  colors = ['#667eea'] # Purple for recommended
246
 
247
  # Add alternatives
248
- for alt in alternatives:
249
  if isinstance(alt, dict):
250
- cards.append(alt.get('card_name', 'Alternative'))
251
- rewards.append(alt.get('reward_amount', 0))
 
 
 
252
  colors.append('#cbd5e0') # Gray for alternatives
253
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  # Create bar chart
255
  fig = go.Figure(data=[
256
  go.Bar(
257
  x=cards,
258
  y=rewards,
259
- marker=dict(color=colors, line=dict(color='white', width=2)),
 
 
 
260
  text=[f'${r:.2f}' for r in rewards],
261
  textposition='outside',
262
  hovertemplate='<b>%{x}</b><br>Rewards: $%{y:.2f}<extra></extra>'
@@ -264,7 +354,11 @@ def create_agent_recommendation_chart(result: Dict) -> go.Figure:
264
  ])
265
 
266
  fig.update_layout(
267
- title={'text': '🎯 Agent\'s Card Comparison', 'x': 0.5, 'xanchor': 'center'},
 
 
 
 
268
  xaxis_title='Credit Card',
269
  yaxis_title='Rewards Earned ($)',
270
  template='plotly_white',
@@ -277,6 +371,9 @@ def create_agent_recommendation_chart(result: Dict) -> go.Figure:
277
 
278
  except Exception as e:
279
  print(f"Chart error: {e}")
 
 
 
280
  fig = go.Figure()
281
  fig.add_annotation(
282
  text="Chart unavailable",
@@ -287,7 +384,6 @@ def create_agent_recommendation_chart(result: Dict) -> go.Figure:
287
  fig.update_layout(height=400, template='plotly_white')
288
  return fig
289
 
290
-
291
 
292
  # Initialize clients
293
  client = RewardPilotClient(config.ORCHESTRATOR_URL)
 
179
 
180
  result = response.json()
181
 
182
+ # βœ… FIXED: Extract data from new response format
183
+ recommendation = safe_get(result, 'recommendation', {})
184
+ reasoning_data = safe_get(result, 'reasoning', {})
185
+ services_data = safe_get(result, 'services_used', {})
186
+ metadata = safe_get(result, 'agent_metadata', {})
187
+ transaction_info = safe_get(result, 'transaction', {})
188
+ plan = safe_get(result, 'plan', {})
189
 
190
+ # Extract card info
191
+ card_name = safe_get(recommendation, 'card_name',
192
+ safe_get(recommendation, 'recommended_card', 'Unknown Card'))
193
+ rewards_earned = safe_get(recommendation, 'rewards_earned', 0)
194
+ rewards_rate = safe_get(recommendation, 'rewards_rate', 'N/A')
195
+ confidence = safe_get(recommendation, 'confidence', 0)
196
+ confidence_label = safe_get(recommendation, 'confidence_label', 'Unknown')
197
+
198
+ # Extract reasoning
199
+ main_reasoning = safe_get(reasoning_data, 'main', 'No reasoning provided')
200
+ why_points = safe_get(reasoning_data, 'why_this_card', [])
201
+ alternatives = safe_get(reasoning_data, 'alternative_options', [])
202
+ warnings = safe_get(reasoning_data, 'warnings', [])
203
+
204
+ # Extract insights
205
+ insights = safe_get(result, 'insights', {})
206
+ annual_impact = safe_get(insights, 'annual_impact', {})
207
+ optimization_score = safe_get(insights, 'optimization_score', 0)
208
+
209
+ # Extract transaction details
210
+ tx_amount = safe_get(transaction_info, 'amount', amount)
211
+ tx_merchant = safe_get(transaction_info, 'merchant', merchant)
212
+ tx_category = safe_get(transaction_info, 'category', category)
213
+
214
+ # Extract service statuses
215
+ services_used = []
216
+ for service_name, service_data in services_data.items():
217
+ if isinstance(service_data, dict) and safe_get(service_data, 'status') == 'success':
218
+ services_used.append(service_name)
219
+
220
+ # Processing time
221
+ processing_time = safe_get(metadata, 'processing_time_ms', 0)
222
+
223
+ # βœ… Format comprehensive output
224
+ output = f"""## πŸ€– AI Agent Recommendation
225
 
226
  ### πŸ’³ Recommended Card: **{card_name}**
227
 
228
+ **Rewards Earned:** ${rewards_earned:.2f} ({rewards_rate})
229
+ **Confidence:** {confidence_label} ({confidence*100:.0f}%)
230
 
231
  ---
232
 
233
+ ### 🧠 Agent's Reasoning:
 
 
 
 
 
234
 
235
+ {main_reasoning}
236
+ """
237
+
238
+ # Add key points
239
+ if why_points:
240
+ output += "\n\n**Why This Card:**\n"
241
+ for point in why_points[:3]:
242
+ output += f"- {point}\n"
243
+
244
+ output += f"""
245
  ---
246
 
247
+ ### πŸ“Š Transaction Details:
248
+ - **Amount:** ${tx_amount:.2f}
249
+ - **Merchant:** {tx_merchant}
250
+ - **Category:** {tx_category}
251
 
252
+ ### βš™οΈ Agent Performance:
253
+ - **Services Used:** {', '.join(services_used) if services_used else 'N/A'}
254
+ - **Processing Time:** {processing_time:.0f}ms
255
+ - **Planning Confidence:** {safe_get(metadata, 'planning_confidence', 0)*100:.0f}%
256
  """
257
 
258
+ # Add annual impact if available
259
+ if annual_impact:
260
+ potential_savings = safe_get(annual_impact, 'potential_savings', 0)
261
+ if potential_savings > 0:
262
+ output += f"\n### πŸ’° Annual Impact:\n- **Potential Savings:** ${potential_savings:.2f}/year\n"
263
+ output += f"- **Optimization Score:** {optimization_score}/100\n"
264
+
265
+ # Add warnings
266
+ if warnings:
267
+ output += "\n\n### ⚠️ Important Warnings:\n"
268
+ for warning in warnings:
269
+ output += f"- {warning}\n"
270
+
271
+ # Add alternatives
272
+ if alternatives:
273
+ output += "\n\n### πŸ”„ Alternative Options:\n"
274
+ for alt in alternatives[:3]:
275
+ alt_card = safe_get(alt, 'card', 'Unknown')
276
+ alt_reason = safe_get(alt, 'reason', 'Alternative option')
277
+ output += f"- **{alt_card}:** {alt_reason}\n"
278
+
279
+ # Add plan strategy
280
+ strategy = safe_get(plan, 'strategy', '')
281
+ if strategy:
282
+ output += f"\n\n### 🎯 Agent Strategy:\n{strategy}\n"
283
+
284
+ output += "\n---\n\nπŸ’‘ **The AI agent analyzed your transaction context, card portfolio, and spending patterns to determine this is your optimal choice.**"
285
+
286
+ # βœ… Create enhanced comparison chart
287
+ chart = create_agent_recommendation_chart_enhanced(result)
288
 
289
  yield output, chart
290
 
 
294
  import traceback
295
  error_details = traceback.format_exc()
296
  print(f"Agent recommendation error: {error_details}")
297
+ yield f"❌ **Error:** {str(e)}\n\nPlease check the orchestrator service or try again.", None
298
+
299
 
300
  def create_agent_recommendation_chart(result: Dict) -> go.Figure:
301
+ """Create enhanced chart showing agent's recommendation with alternatives"""
302
  try:
303
+ # Extract recommendation data
304
+ recommendation = safe_get(result, 'recommendation', {})
305
+ reasoning_data = safe_get(result, 'reasoning', {})
306
 
307
+ # Get recommended card
308
+ rec_name = safe_get(recommendation, 'card_name',
309
+ safe_get(recommendation, 'recommended_card', 'Recommended Card'))
310
+ rec_reward = float(safe_get(recommendation, 'rewards_earned', 0))
311
+
312
+ # Get alternatives
313
+ alternatives = safe_get(reasoning_data, 'alternative_options', [])
314
 
315
  cards = [rec_name]
316
  rewards = [rec_reward]
317
  colors = ['#667eea'] # Purple for recommended
318
 
319
  # Add alternatives
320
+ for alt in alternatives[:3]: # Limit to top 3
321
  if isinstance(alt, dict):
322
+ alt_card = safe_get(alt, 'card', 'Alternative')
323
+ # Try to extract reward amount from reason text if available
324
+ alt_reward = rec_reward * 0.8 # Estimate if not provided
325
+ cards.append(alt_card)
326
+ rewards.append(alt_reward)
327
  colors.append('#cbd5e0') # Gray for alternatives
328
 
329
+ # Only create chart if we have valid data
330
+ if not cards or all(r == 0 for r in rewards):
331
+ fig = go.Figure()
332
+ fig.add_annotation(
333
+ text="No comparison data available",
334
+ xref="paper", yref="paper",
335
+ x=0.5, y=0.5, showarrow=False,
336
+ font=dict(size=14, color="#666")
337
+ )
338
+ fig.update_layout(height=400, template='plotly_white')
339
+ return fig
340
+
341
  # Create bar chart
342
  fig = go.Figure(data=[
343
  go.Bar(
344
  x=cards,
345
  y=rewards,
346
+ marker=dict(
347
+ color=colors,
348
+ line=dict(color='white', width=2)
349
+ ),
350
  text=[f'${r:.2f}' for r in rewards],
351
  textposition='outside',
352
  hovertemplate='<b>%{x}</b><br>Rewards: $%{y:.2f}<extra></extra>'
 
354
  ])
355
 
356
  fig.update_layout(
357
+ title={
358
+ 'text': '🎯 Agent\'s Card Comparison',
359
+ 'x': 0.5,
360
+ 'xanchor': 'center'
361
+ },
362
  xaxis_title='Credit Card',
363
  yaxis_title='Rewards Earned ($)',
364
  template='plotly_white',
 
371
 
372
  except Exception as e:
373
  print(f"Chart error: {e}")
374
+ import traceback
375
+ print(traceback.format_exc())
376
+
377
  fig = go.Figure()
378
  fig.add_annotation(
379
  text="Chart unavailable",
 
384
  fig.update_layout(height=400, template='plotly_white')
385
  return fig
386
 
 
387
 
388
  # Initialize clients
389
  client = RewardPilotClient(config.ORCHESTRATOR_URL)