RantoG commited on
Commit
cfac577
·
verified ·
1 Parent(s): d0a2c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -152,7 +152,7 @@ def format_user_context(context_data):
152
  def home():
153
  return jsonify({
154
  "status": "online",
155
- "message": "GastroGuard AI Backend is Running (Aggressive Data Mode)!",
156
  "endpoints": ["/analyze-text", "/analyze-image", "/chat"]
157
  })
158
 
@@ -362,9 +362,6 @@ def chat():
362
  try:
363
  user_context_str = format_user_context(user_context)
364
 
365
- # --- PERBAIKAN LOGIKA CHAT ---
366
- # Prompt ini memaksa AI untuk "EXTRACT DATA" jika ada nama makanan
367
- # meskipun user menggunakan kata "mau" (intent).
368
  prompt_content = f"""
369
  ROLE: GastroGuard AI (Health & Nutrition Expert).
370
  CONTEXT: {user_context_str}
@@ -375,7 +372,6 @@ MANDATORY INSTRUCTIONS:
375
  - YOU MUST ESTIMATE THE NUTRITION FACTS in 'data_makanan'.
376
  - DO NOT leave calorie values as 0.
377
  - FILL 'nama_menu' with the specific food name.
378
- - IGNORE verbs like "want to" or "planning to". If food is named, DATA IS REQUIRED.
379
  2. If no food is mentioned (e.g., "Hello", "My stomach hurts"), keep nutrition 0.
380
  3. RETURN JSON ONLY.
381
 
@@ -384,7 +380,7 @@ OUTPUT JSON SCHEMA:
384
  "analisis_emosi": {{ "status": "Neutral", "indikator": "text" }},
385
  "chat_response": "Your friendly answer here.",
386
  "data_makanan": {{
387
- "nama_menu": "Food Name (or empty)",
388
  "estimasi_berat": "e.g. 1 serving",
389
  "nutrisi": {{
390
  "kalori": 0, "protein": 0, "karbohidrat": 0, "lemak_total": 0
@@ -398,7 +394,7 @@ OUTPUT JSON SCHEMA:
398
  contents=prompt_content,
399
  config=types.GenerateContentConfig(
400
  response_mime_type="application/json",
401
- temperature=0.3 # Temperature rendah = patuh instruksi
402
  )
403
  )
404
 
@@ -408,21 +404,27 @@ OUTPUT JSON SCHEMA:
408
  food_data = result.get("data_makanan", {})
409
  nutrisi = food_data.get("nutrisi", {})
410
 
411
- # --- PYTHON LOGIC FIX ---
412
- # Pastikan kalori dianggap angka (bukan string) untuk pengecekan
 
413
  try:
414
  cal_val = float(nutrisi.get("kalori", 0))
415
- except:
416
  cal_val = 0
417
-
418
- # Jika ada nama menu DAN kalori > 0, tempelkan teks nutrisi
419
- if food_data.get("nama_menu") and cal_val > 0:
420
- nutrition_text = f"\n\n📊 **{food_data.get('nama_menu')} Info:**\n🔥 {cal_val} kcal | 🥩 P: {nutrisi.get('protein', 0)}g | 🍞 C: {nutrisi.get('karbohidrat', 0)}g | 🥑 F: {nutrisi.get('lemak_total', 0)}g"
 
 
 
 
 
421
  final_reply += nutrition_text
422
 
423
  mapped_result = {
424
  "reply": final_reply,
425
- "food_name": food_data.get("nama_menu"),
426
  "calories": cal_val,
427
  "protein": nutrisi.get("protein", 0),
428
  "carbs": nutrisi.get("karbohidrat", 0),
 
152
  def home():
153
  return jsonify({
154
  "status": "online",
155
+ "message": "GastroGuard AI Backend is Running (Fix Chat Display)!",
156
  "endpoints": ["/analyze-text", "/analyze-image", "/chat"]
157
  })
158
 
 
362
  try:
363
  user_context_str = format_user_context(user_context)
364
 
 
 
 
365
  prompt_content = f"""
366
  ROLE: GastroGuard AI (Health & Nutrition Expert).
367
  CONTEXT: {user_context_str}
 
372
  - YOU MUST ESTIMATE THE NUTRITION FACTS in 'data_makanan'.
373
  - DO NOT leave calorie values as 0.
374
  - FILL 'nama_menu' with the specific food name.
 
375
  2. If no food is mentioned (e.g., "Hello", "My stomach hurts"), keep nutrition 0.
376
  3. RETURN JSON ONLY.
377
 
 
380
  "analisis_emosi": {{ "status": "Neutral", "indikator": "text" }},
381
  "chat_response": "Your friendly answer here.",
382
  "data_makanan": {{
383
+ "nama_menu": "Food Name",
384
  "estimasi_berat": "e.g. 1 serving",
385
  "nutrisi": {{
386
  "kalori": 0, "protein": 0, "karbohidrat": 0, "lemak_total": 0
 
394
  contents=prompt_content,
395
  config=types.GenerateContentConfig(
396
  response_mime_type="application/json",
397
+ temperature=0.3
398
  )
399
  )
400
 
 
404
  food_data = result.get("data_makanan", {})
405
  nutrisi = food_data.get("nutrisi", {})
406
 
407
+ # --- FIX LOGIKA DISPLAY (AGAR PASTI MUNCUL) ---
408
+
409
+ # Kita ambil nilai kalori, pastikan jadi angka, jika gagal set 0
410
  try:
411
  cal_val = float(nutrisi.get("kalori", 0))
412
+ except (ValueError, TypeError):
413
  cal_val = 0
414
+
415
+ # Kita ambil nama menu
416
+ menu_name = food_data.get("nama_menu")
417
+
418
+ # LOGIKA: Kalau kalori > 0, KITA PAKSA TEMPEL STRING.
419
+ # Tidak peduli 'nama_menu' kosong atau tidak (biasanya AI isi nama menu).
420
+ if cal_val > 0:
421
+ display_name = menu_name if menu_name else "Food"
422
+ nutrition_text = f"\n\n📊 **{display_name} Info:**\n🔥 {int(cal_val)} kcal | 🥩 P: {nutrisi.get('protein', 0)}g | 🍞 C: {nutrisi.get('karbohidrat', 0)}g | 🥑 F: {nutrisi.get('lemak_total', 0)}g"
423
  final_reply += nutrition_text
424
 
425
  mapped_result = {
426
  "reply": final_reply,
427
+ "food_name": menu_name,
428
  "calories": cal_val,
429
  "protein": nutrisi.get("protein", 0),
430
  "carbs": nutrisi.get("karbohidrat", 0),