-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathapp.py
More file actions
209 lines (166 loc) · 6.7 KB
/
Copy pathapp.py
File metadata and controls
209 lines (166 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from flask import Flask, request, jsonify, send_from_directory
from config.config import Config
import google.generativeai as genai
import traceback
import os
import re
from flask_cors import CORS
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
app = Flask(__name__, static_folder='.', static_url_path='')
app.config["SECRET_KEY"] = Config.SECRET_KEY
CORS(app, resources={r"/*": {"origins": "http://127.0.0.1:5500"}})
# Input validation and sanitization functions
def sanitize_input(text):
"""Sanitize user input to prevent XSS and injection attacks"""
if not text or not isinstance(text, str):
return ""
# Remove HTML tags
text = re.sub(r'<[^>]+>', '', text)
# Escape special characters
text = text.replace('&', '&')
text = text.replace('<', '<')
text = text.replace('>', '>')
text = text.replace('"', '"')
text = text.replace("'", ''')
# Limit length
if len(text) > 1000:
text = text[:1000]
return text.strip()
def validate_input(data):
"""Validate input data structure and content"""
if not data:
return False, "No data provided"
# Check for required fields if needed
# Add specific validation rules here
return True, "Valid input"
# Initialize Gemini API
API_KEY = os.environ.get("GEMINI_API_KEY")
MODEL_ID = "gemini-2.5-flash"
client = None
if API_KEY:
try:
client = genai.Client(api_key=API_KEY)
print("Gemini client initialized successfully.")
except Exception as e:
print("Failed to initialize Gemini client:", e)
else:
print("Warning: GEMINI_API_KEY not set. AI features are disabled.")
"""Secure endpoint to provide Firebase configuration to client"""
@app.route('/api/firebase-config')
def get_firebase_config():
return jsonify({
"apiKey": os.getenv("FIREBASE_API_KEY"),
"authDomain": os.getenv("FIREBASE_AUTH_DOMAIN"),
"projectId": os.getenv("FIREBASE_PROJECT_ID"),
"storageBucket": os.getenv("FIREBASE_STORAGE_BUCKET"),
"messagingSenderId": os.getenv("FIREBASE_MESSAGING_SENDER_ID"),
"appId": os.getenv("FIREBASE_APP_ID"),
"measurementId": os.getenv("FIREBASE_MEASUREMENT_ID")
})
@app.route('/process-loan', methods=['POST'])
def process_loan():
try:
json_data = request.get_json(force=True)
# Validate and sanitize input
is_valid, validation_message = validate_input(json_data)
if not is_valid:
return jsonify({
"status": "error",
"message": validation_message
}), 400
# Sanitize any text fields in the JSON data
if isinstance(json_data, dict):
for key, value in json_data.items():
if isinstance(value, str):
json_data[key] = sanitize_input(value)
print(f"Received JSON: {json_data}")
prompt = f"""
You are a financial loan eligibility advisor specializing in agricultural loans for farmers in India.
You will be given a JSON object that contains information about a farmer's loan application. The fields in this JSON will vary depending on the loan type (e.g., Crop Cultivation, Farm Equipment, Water Resources, Land Purchase).
You will focus only on loan schemes and eligibility criteria followed by:
1. Indian nationalized banks (e.g., SBI, Bank of Baroda)
2. Private sector Indian banks (e.g., ICICI, HDFC)
3. Regional Rural Banks (RRBs)
4. Cooperative Banks
5. NABARD & government schemes
Do not suggest generic or international financing options.
JSON Data = {json_data}
Your task is to:
1. Identify the loan type and understand which fields are important for assessing that particular loan.
2. Analyze the farmer's provided details and assess their loan eligibility.
3. Highlight areas of strength and areas where the farmer may face challenges.
4. If any critical data is missing from the JSON, point it out clearly.
5. Provide simple and actionable suggestions the farmer can follow to improve eligibility.
6. Suggest the government schemes or subsidies applicable to their loan type.
7. Ensure the tone is clear, supportive, and easy to understand for farmers.
8. Respond in a structured format with labeled sections: Loan Type, Eligibility Status, Loan Range, Improvements, Schemes.
9. **IMPORTANT: Return your response in **Markdown format** with:
Headings for each section (Loan Type, Eligibility Status, Loan Range, Improvements, Schemes)
Bullet points ( - ) for lists.
Do not use "\\n" for newlines. Instead, structure properly.
Do not add assumptions that are not supported by the data provided.
"""
response = client.models.generate_content(
model=MODEL_ID,
contents=[{"parts": [{"text": prompt}]}]
)
if not response.candidates:
return jsonify({
"status": "error",
"message": "No response generated from Gemini API"
}), 500
reply = response.candidates[0].content.parts[0].text
return jsonify({
"status": "success",
"message": "Loan processes successfully "
}), 200
except Exception :
traceback.print_exc()
return jsonify({
"status": "error",
"message": "Failed to process loan request. Please try again later."}), 500
# Serve HTML pages
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/farmer')
def farmer():
return send_from_directory('.', 'farmer.html')
@app.route('/shopkeeper')
def shopkeeper():
return send_from_directory('.', 'shopkeeper.html')
@app.route('/main')
def main():
return send_from_directory('.', 'main.html')
@app.route('/about')
def about():
return send_from_directory('.', 'about.html')
@app.route('/blog')
def blog():
return send_from_directory('.', 'blog.html')
@app.route('/contact')
def contact():
return send_from_directory('.', 'contact.html')
@app.route('/chat')
def chat():
return send_from_directory('.', 'chat.html')
@app.route('/<path:filename>')
def serve_static(filename):
return send_from_directory('.', filename)
if __name__ == '__main__':
app.run(port=5000, debug=True)
#Global Error Handling
@app.errorhandler(404)
def not_found(error):
return jsonify({
"status" : "error",
"message" :"Resource not found"
}),404
@app.errorhandler(500)
def internal_error(error):
return jsonify({
"status": "error",
"message": "Internal server error"
}), 500