Infinity-1995 commited on
Commit
ae2cee3
·
verified ·
1 Parent(s): 6a74830

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -100
app.py CHANGED
@@ -1,137 +1,78 @@
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
- from huggingface_hub import InferenceClient
4
- import os, math
5
- from collections import defaultdict
6
 
7
  app = Flask(__name__)
8
  CORS(app)
9
 
10
- client = InferenceClient(
11
- model="meta-llama/Meta-Llama-3.3-70B-Versatile",
12
- token=os.getenv("HF_TOKEN")
13
- )
14
-
15
  NGOS = [
16
- {"name":"Dubai Food Bank","type":"Food Bank","lat":25.20,"lon":55.27,"capacity":500},
17
- {"name":"Sharjah Elderly Care Home","type":"Elderly Home","lat":25.34,"lon":55.42,"capacity":120},
18
- {"name":"UAE Refugee Support Center","type":"Refugee Camp","lat":25.27,"lon":55.29,"capacity":800},
19
- {"name":"Abu Dhabi Community Kitchen","type":"Community Kitchen","lat":24.45,"lon":54.37,"capacity":300},
20
- {"name":"Ajman Senior Support Home","type":"Elderly Home","lat":25.41,"lon":55.51,"capacity":140}
21
  ]
22
 
23
- ngo_learning = defaultdict(float)
24
-
25
- def haversine(lat1, lon1, lat2, lon2):
26
- R = 6371
27
- dlat = math.radians(lat2-lat1)
28
- dlon = math.radians(lon2-lon1)
29
-
30
- a = math.sin(dlat/2)**2 + math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.sin(dlon/2)**2
31
- return R * (2 * math.atan2(math.sqrt(a), math.sqrt(1-a)))
32
-
33
- def score(meals, ngo, foodType, expiry, lat, lon):
34
-
35
- dist = haversine(lat, lon, ngo["lat"], ngo["lon"])
36
- dist_score = max(0, 100 - dist * 6)
37
-
38
- cap_score = min(100, ngo["capacity"] / max(meals,1) * 50)
39
-
40
- urgency = 20
41
- ft = foodType.lower()
42
- nt = ngo["type"].lower()
43
-
44
- if "cooked" in ft:
45
- urgency += 35 if "elderly" in nt or "refugee" in nt else 15
46
- elif "baked" in ft:
47
- urgency += 30 if "elderly" in nt else 10
48
- elif "raw" in ft:
49
- urgency += 30 if "kitchen" in nt else 12
50
- elif "packaged" in ft:
51
- urgency += 25 if "food bank" in nt else 15
52
-
53
- meals = int(meals)
54
- if meals <= 40:
55
- urgency += 40
56
- elif meals <= 100:
57
- urgency += 25
58
- else:
59
- urgency += 15
60
-
61
- expiry = int(expiry)
62
- if expiry <= 4:
63
- urgency += 60
64
- elif expiry <= 8:
65
- urgency += 35
66
- elif expiry <= 24:
67
- urgency += 15
68
-
69
- return round(dist_score*0.2 + cap_score*0.2 + urgency*0.55 + ngo_learning[ngo["name"]], 2)
70
-
71
- def explain(ngo, meals, foodType, score, rank):
72
-
73
- prompt = f"""
74
- Rank {rank}
75
- NGO: {ngo['name']} ({ngo['type']})
76
- Food: {foodType}
77
- Meals: {meals}
78
- Score: {score}
79
-
80
- Give 1–2 line unique explanation. No repetition.
81
- """
82
-
83
- try:
84
- return client.text_generation(
85
- prompt,
86
- max_new_tokens=60,
87
- temperature=0.8
88
- ).strip()
89
- except:
90
- return "Matched based on urgency and suitability."
91
 
92
  @app.route("/analyze", methods=["POST"])
93
  def analyze():
94
 
95
  data = request.json
96
-
97
  meals = int(data["meals"])
98
  foodType = data["foodType"]
99
- expiry = data["expiry"]
100
 
101
- lat, lon = 25.2048, 55.2708
102
 
103
  results = []
104
 
105
  for ngo in NGOS:
 
 
 
 
106
 
107
- s = score(meals, ngo, foodType, expiry, lat, lon)
108
 
109
  results.append({
110
  "recipient": ngo["name"],
111
- "type": ngo["type"],
112
- "score": s,
113
- "km": round(haversine(lat, lon, ngo["lat"], ngo["lon"]), 1)
114
  })
115
 
116
  results.sort(key=lambda x: x["score"], reverse=True)
117
 
118
- for i, r in enumerate(results):
 
 
119
 
120
- ngo_obj = next(n for n in NGOS if n["name"] == r["recipient"])
121
 
122
- r["priority"] = (
123
- "High" if r["score"] >= 75 else
124
- "Medium" if r["score"] >= 55 else
125
- "Low"
126
- )
127
 
128
- r["reason"] = explain(ngo_obj, meals, foodType, r["score"], i+1)
 
 
129
 
130
- return jsonify(results)
 
 
131
 
132
- @app.route("/")
133
- def home():
134
- return "NourishNet AI Running 🚀"
135
 
136
  if __name__ == "__main__":
137
  app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
+ import random, math
 
 
4
 
5
  app = Flask(__name__)
6
  CORS(app)
7
 
 
 
 
 
 
8
  NGOS = [
9
+ {"name":"Dubai Food Bank","type":"Food Bank","lat":25.20,"lon":55.27},
10
+ {"name":"Sharjah Elderly Care","type":"Elderly Home","lat":25.34,"lon":55.42},
11
+ {"name":"UAE Relief Center","type":"Shelter","lat":25.27,"lon":55.29},
12
+ {"name":"Abu Dhabi Kitchen","type":"Kitchen","lat":24.45,"lon":54.37},
13
+ {"name":"Ajman Support Home","type":"Elderly Home","lat":25.41,"lon":55.51}
14
  ]
15
 
16
+ def dist():
17
+ return random.uniform(2,10)
18
+
19
+ def reason(food, ngo):
20
+ reasons = [
21
+ f"{ngo} has urgent demand for {food}",
22
+ f"{food} matches dietary needs of {ngo}",
23
+ f"High priority distribution zone for {ngo}",
24
+ f"Efficient delivery route for {ngo}"
25
+ ]
26
+ return random.choice(reasons)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  @app.route("/analyze", methods=["POST"])
29
  def analyze():
30
 
31
  data = request.json
 
32
  meals = int(data["meals"])
33
  foodType = data["foodType"]
34
+ seed = data.get("seed", 1)
35
 
36
+ random.seed(seed)
37
 
38
  results = []
39
 
40
  for ngo in NGOS:
41
+ score = random.randint(50,95)
42
+
43
+ if meals > 120:
44
+ score += 5
45
 
46
+ km = round(dist(),1)
47
 
48
  results.append({
49
  "recipient": ngo["name"],
50
+ "km": km,
51
+ "score": score,
52
+ "reason": reason(foodType, ngo["name"]),
53
  })
54
 
55
  results.sort(key=lambda x: x["score"], reverse=True)
56
 
57
+ highs = results[:2]
58
+ meds = results[2:4]
59
+ lows = results[4:6]
60
 
61
+ final = []
62
 
63
+ for r in highs:
64
+ r["priority"] = "High"
65
+ final.append(r)
 
 
66
 
67
+ for r in meds:
68
+ r["priority"] = "Medium"
69
+ final.append(r)
70
 
71
+ for r in lows:
72
+ r["priority"] = "Low"
73
+ final.append(r)
74
 
75
+ return jsonify(final)
 
 
76
 
77
  if __name__ == "__main__":
78
  app.run(host="0.0.0.0", port=7860)