Spaces:
Sleeping
Sleeping
Update app.py
Browse files28 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251263 unmodified lines1301311321331341354 unmodified lines14014114214314414514614714812 unmodified lines16116216316416516616728 unmodified lines]# -----------------------------# 📊 SCORE ENGINE (BALANCED)# -----------------------------def score(meals, ngo, foodType, expiry):# distancedist = math.sqrt((ngo["lat"] - 25.20)**2 + (ngo["lon"] - 55.27)**2) * 100dist_score = max(0, 100 - dist * 10)# capacity (reduced dominance!)cap_score = min(100, (ngo["capacity"] / max(meals, 1)) * 60)urgency = 40 # basengo_type = ngo["type"].lower()ft = foodType.lower()# -------------------------# 🍱 FOOD TYPE IMPACT (IMPORTANT)# -------------------------if "cooked" in ft:if "elderly" in ngo_type:urgency += 35elif "refugee" in ngo_type:urgency += 25elif "baked" in ft:if "elderly" in ngo_type:urgency += 30elif "raw" in ft:if "kitchen" in ngo_type:urgency += 35elif "packaged" in ft:if "food bank" in ngo_type:urgency += 30# -------------------------# 🍽 PORTION IMPACT# -------------------------if meals >= 150:urgency += 25elif meals <= 60:urgency += 20# -------------------------# ⏳ EXPIRY IMPACT# -------------------------if expiry <= 6:urgency += 30elif expiry <= 24:urgency += 15# final scorereturn round(dist_score * 0.25 +cap_score * 0.20 +urgency * 0.55,2)# -----------------------------# 🤖 LLM REASONING (1–2 LINES ONLY)# -----------------------------def explain(ngo, meals, foodType, expiry):prompt = f"""You are a logistics AI.Match:- NGO: {ngo['name']}- Type: {ngo['type']}- Food: {foodType}- Meals: {meals}- Expiry: {expiry} hoursGive ONLY 1–2 short clear lines explaining WHY this is a good match.Be specific. Mention food type or urgency or capacity.Avoid generic phrases."""try:out = client.text_generation(prompt,max_new_tokens=60,temperature=1.0,top_p=0.9)return out.strip()except:return f"Best suited for {foodType} based on demand and capacity alignment."# -----------------------------# 📡 API3 unmodified linesdata = request.jsonmeals = int(data["meals"])foodType = data["foodType"]expiry = int(data["expiry"])4 unmodified linesbase = score(meals, ngo, foodType, expiry)# small randomness for realismfinal_score = round(base + random.uniform(-1.2, 1.2), 2)if final_score >= 85:priority = "High"elif final_score >= 65:12 unmodified lines"reason": explain(ngo, meals, foodType, expiry)})# sort final rankingresults.sort(key=lambda x: x["score"], reverse=True)return jsonify(results)28 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181193 unmodified lines1231241251261271281291301314 unmodified lines13613713813914014114214314414512 unmodified lines15815916016116216316428 unmodified lines]# -----------------------------# 🧠 SCORING ENGINE (FULL FIX)# -----------------------------def score(meals, ngo, foodType, expiry):# distancedist = math.sqrt((ngo["lat"] - 25.20)**2 + (ngo["lon"] - 55.27)**2) * 100dist_score = max(0, 100 - dist * 12)# capacity (reduced dominance)cap_score = min(100, (ngo["capacity"] / max(meals, 1)) * 70)urgency = 30 # base low so AI can actually influence resultngo_type = ngo["type"].lower()ft = foodType.lower()# ---------------- FOOD TYPE IMPACT ----------------if "cooked" in ft:urgency += 40 if "elderly" in ngo_type else 25elif "baked" in ft:urgency += 35 if "elderly" in ngo_type else 20elif "raw" in ft:urgency += 40 if "kitchen" in ngo_type else 25elif "packaged" in ft:urgency += 45 if "food bank" in ngo_type else 20elif "vegetarian" in ft:urgency += 30 if "refugee" in ngo_type else 15# ---------------- PORTION IMPACT ----------------if meals >= 150:urgency += 35elif meals <= 60:urgency += 20else:urgency += 10# ---------------- EXPIRY IMPACT ----------------if expiry <= 6:urgency += 45elif expiry <= 24:urgency += 25else:urgency += 10# FINAL SCOREreturn round(dist_score * 0.20 +cap_score * 0.15 +urgency * 0.65,2)# -----------------------------# 🤖 LLM REASONING (1–2 LINES)# -----------------------------def explain(ngo, meals, foodType, expiry):prompt = f"""You are NourishNet AI.NGO: {ngo['name']}Type: {ngo['type']}Food: {foodType}Meals: {meals}Expiry: {expiry}Give ONLY 1–2 short clear lines explaining why this is the best match.Be specific. Mention food type, urgency or capacity.Avoid repetition and generic phrases."""try:return client.text_generation(prompt,max_new_tokens=60,temperature=1.1,top_p=0.95).strip()except:return f"Matched due to {foodType} suitability and demand alignment."# -----------------------------# 📡 API3 unmodified linesdata = request.json# 🔥 DEBUG INPUT (IMPORTANT)print("INPUT RECEIVED:", data)meals = int(data["meals"])foodType = data["foodType"]expiry = int(data["expiry"])4 unmodified linesbase = score(meals, ngo, foodType, expiry)# 🔥 ADD VARIATION (prevents static output)final_score = round(base + random.uniform(-6, 6), 2)# priorityif final_score >= 85:priority = "High"elif final_score >= 65:12 unmodified lines"reason": explain(ngo, meals, foodType, expiry)})# sort by scoreresults.sort(key=lambda x: x["score"], reverse=True)return jsonify(results)
app.py+50-53