-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (104 loc) · 4.86 KB
/
app.py
File metadata and controls
133 lines (104 loc) · 4.86 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
from flask import Flask, request, jsonify
from google import 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__)
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', 'YOUR-API-KEY')
MODEL_ID = 'gemini-2.5-flash'
# Configure Gemini Client
client = genai.Client(api_key=API_KEY)
@app.route('/api/firebase-config')
def get_firebase_config():
"""Secure endpoint to provide Firebase configuration to client"""
return jsonify({
'apiKey': os.environ.get('FIREBASE_API_KEY'),
'authDomain': os.environ.get('FIREBASE_AUTH_DOMAIN'),
'projectId': os.environ.get('FIREBASE_PROJECT_ID'),
'storageBucket': os.environ.get('FIREBASE_STORAGE_BUCKET'),
'messagingSenderId': os.environ.get('FIREBASE_MESSAGING_SENDER_ID'),
'appId': os.environ.get('FIREBASE_APP_ID'),
'measurementId': os.environ.get('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}]}]
)
reply = response.candidates[0].content.parts[0].text
return jsonify({"status": "success", "message": reply}), 200
except Exception as e:
print(f"Unexpected Error: {e}")
traceback.print_exc()
return jsonify({"status": "error", "message": str(e)}), 500
if __name__ == '__main__':
app.run(port=5000, debug=True)