starkbyte45896 commited on
Commit
bea8169
·
verified ·
1 Parent(s): d85def4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -28
app.py CHANGED
@@ -1,43 +1,114 @@
1
- # app.py
2
  import firebase_admin
3
  from firebase_admin import credentials, firestore
4
- from flask import Flask, render_template, redirect, url_for
 
5
 
6
  app = Flask(__name__)
7
 
8
- # Initialize Firebase Admin with service account key file
9
- cred = credentials.Certificate("serviceAccountKey.json") # NOT google-services.json
 
 
10
  firebase_admin.initialize_app(cred)
11
  db = firestore.client()
12
 
13
- @app.route("/")
14
- def index():
15
- return redirect(url_for("list_all_schemes"))
16
 
17
- @app.route("/schemes")
18
- def list_all_schemes():
19
- # path: data/<docId>/Schemes
20
- parent_doc_id = "qo3JakPyOUWzdEOFYWbv" # put your real document id here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  schemes_ref = db.collection("data").document(parent_doc_id).collection("Schemes")
22
  docs = schemes_ref.stream()
23
 
24
- schemes_list = []
 
25
  for doc in docs:
26
- schemes_list.append({"id": doc.id, "data": doc.to_dict()})
27
- return render_template("all_schemes.html", schemes=schemes_list)
28
-
29
- @app.route("/scheme/<scheme_id>")
30
- def show_scheme(scheme_id):
31
- # path: data/<docId>/Schemes/<scheme_id>
32
- parent_doc_id = "qo3JakPyOUWzdEOFYWbv" # put your real document id here
33
- scheme_ref = db.collection("data").document(parent_doc_id).collection("Schemes").document(scheme_id)
34
- scheme = scheme_ref.get()
35
-
36
- if scheme.exists:
37
- scheme_data = scheme.to_dict()
38
- return render_template("schemes.html", scheme=scheme_data)
39
- else:
40
- return "Scheme not found", 404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == "__main__":
43
- app.run(debug=True)
 
 
1
  import firebase_admin
2
  from firebase_admin import credentials, firestore
3
+ from flask import Flask, request, jsonify, render_template, redirect, url_for
4
+ import datetime
5
 
6
  app = Flask(__name__)
7
 
8
+ # -------------------------------------------------------
9
+ # Firebase Initialization
10
+ # -------------------------------------------------------
11
+ cred = credentials.Certificate("serviceAccountKey.json")
12
  firebase_admin.initialize_app(cred)
13
  db = firestore.client()
14
 
 
 
 
15
 
16
+ # -------------------------------------------------------
17
+ # Utility: Scheme Scoring Function
18
+ # -------------------------------------------------------
19
+ def compute_scheme_score(scheme, user):
20
+ score = 0
21
+
22
+ # AGE
23
+ try:
24
+ if user["age"] >= scheme.get("eligibility", {}).get("age_min", 0):
25
+ score += 25
26
+ except:
27
+ pass
28
+
29
+ # CATEGORY (SC required)
30
+ target_group = scheme.get("target_group", "")
31
+ if target_group == "SCFAMILIES" and user["category"] == "SC":
32
+ score += 40
33
+
34
+ # LOCATION MATCH
35
+ loc = scheme.get("locations", {})
36
+ if isinstance(loc, dict):
37
+ if user["district"].lower() == loc.get("district", "").lower():
38
+ score += 15
39
+ if user["state"].lower() == loc.get("state", "").lower():
40
+ score += 10
41
+
42
+ # REQUIRED DOCUMENTS
43
+ required_docs = scheme.get("eligibility", {}).get("required_documents", [])
44
+ user_docs = user.get("documents", [])
45
+
46
+ doc_matches = len(set(required_docs).intersection(set(user_docs)))
47
+ score += doc_matches * 5
48
+
49
+ # LIVESTOCK INTEREST MATCH
50
+ livestock = scheme.get("livestock_types", [])
51
+ if user.get("interest") in livestock:
52
+ score += 20
53
+
54
+ # SC SUBSIDY %
55
+ subsidy = scheme.get("subsidy_percent", 0)
56
+ if subsidy >= 50:
57
+ score += 10
58
+
59
+ # TRAINING?
60
+ if scheme.get("training_days", 0) == 0:
61
+ score += 5 # Easier process → better recommendation
62
+
63
+ return score
64
+
65
+
66
+ # -------------------------------------------------------
67
+ # ROUTE: Recommendation Endpoint
68
+ # -------------------------------------------------------
69
+ @app.route("/recommend", methods=["POST"])
70
+ def recommend_schemes():
71
+ user = request.json
72
+
73
+ parent_doc_id = "qo3JakPyOUWzdEOFYWbv"
74
  schemes_ref = db.collection("data").document(parent_doc_id).collection("Schemes")
75
  docs = schemes_ref.stream()
76
 
77
+ output = []
78
+
79
  for doc in docs:
80
+ scheme = doc.to_dict()
81
+ score = compute_scheme_score(scheme, user)
82
+
83
+ output.append({
84
+ "scheme_id": scheme.get("scheme_id"),
85
+ "title": scheme.get("title"),
86
+ "short_description": scheme.get("short_description"),
87
+ "full_description": scheme.get("full_description"),
88
+ "score": score,
89
+ "livestock_types": scheme.get("livestock_types"),
90
+ "subsidy_percent": scheme.get("subsidy_percent"),
91
+ "locations": scheme.get("locations"),
92
+ "eligibility": scheme.get("eligibility"),
93
+ "application_process": scheme.get("application_process"),
94
+ "contact": scheme.get("contact")
95
+ })
96
+
97
+ output = sorted(output, key=lambda x: x["score"], reverse=True)
98
+
99
+ return jsonify({
100
+ "timestamp": datetime.datetime.utcnow().isoformat(),
101
+ "recommendations": output
102
+ })
103
+
104
+
105
+ # -------------------------------------------------------
106
+ # Default Redirect
107
+ # -------------------------------------------------------
108
+ @app.route("/")
109
+ def index():
110
+ return jsonify({"message": "Scheme Recommendation API Active"})
111
+
112
 
113
  if __name__ == "__main__":
114
+ app.run(debug=True)