Spaces:
Sleeping
Sleeping
AdityaAdaki
commited on
Commit
·
54ce02f
1
Parent(s):
7f912dd
- .gitignore +1 -0
- app.py +73 -1
- templates/base.html +72 -12
- templates/chatbot.html +365 -0
- templates/index.html +8 -3
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template
|
| 2 |
import json
|
| 3 |
import re
|
| 4 |
import os
|
|
@@ -119,6 +119,78 @@ schemes = load_schemes()
|
|
| 119 |
def index():
|
| 120 |
return render_template('index.html', schemes=schemes)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
@app.route('/scheme/<scheme_id>')
|
| 123 |
def scheme_details(scheme_id):
|
| 124 |
scheme = schemes.get(scheme_id)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, jsonify, request
|
| 2 |
import json
|
| 3 |
import re
|
| 4 |
import os
|
|
|
|
| 119 |
def index():
|
| 120 |
return render_template('index.html', schemes=schemes)
|
| 121 |
|
| 122 |
+
# Add this new route for the chatbot
|
| 123 |
+
@app.route('/chatbot')
|
| 124 |
+
def chatbot():
|
| 125 |
+
return render_template('chatbot.html')
|
| 126 |
+
|
| 127 |
+
# Add this new API endpoint to process chatbot queries
|
| 128 |
+
@app.route('/api/recommend', methods=['POST'])
|
| 129 |
+
def recommend_schemes():
|
| 130 |
+
data = request.json
|
| 131 |
+
answers = data.get('answers', {})
|
| 132 |
+
|
| 133 |
+
# Get farmer type
|
| 134 |
+
farmer_type = answers.get('farmer_type', '')
|
| 135 |
+
|
| 136 |
+
# Get farming interests
|
| 137 |
+
interests = answers.get('interests', [])
|
| 138 |
+
|
| 139 |
+
# Get financial needs
|
| 140 |
+
financial_needs = answers.get('financial_needs', [])
|
| 141 |
+
|
| 142 |
+
# Get region/state
|
| 143 |
+
region = answers.get('region', '')
|
| 144 |
+
|
| 145 |
+
# Get land area
|
| 146 |
+
land_area = answers.get('land_area', '')
|
| 147 |
+
|
| 148 |
+
# Algorithm to find matching schemes
|
| 149 |
+
recommended_schemes = []
|
| 150 |
+
|
| 151 |
+
for scheme_id, scheme in schemes.items():
|
| 152 |
+
score = 0
|
| 153 |
+
keywords = scheme['keywords'].lower()
|
| 154 |
+
|
| 155 |
+
# Check farmer type match
|
| 156 |
+
if farmer_type == 'small' and 'small' in keywords:
|
| 157 |
+
score += 3
|
| 158 |
+
elif farmer_type == 'tenant' and 'tenant' in keywords:
|
| 159 |
+
score += 3
|
| 160 |
+
elif farmer_type == 'fpo' and 'fpo' in keywords:
|
| 161 |
+
score += 3
|
| 162 |
+
elif farmer_type == 'all' and 'all farmers' in keywords:
|
| 163 |
+
score += 1
|
| 164 |
+
|
| 165 |
+
# Check interests
|
| 166 |
+
for interest in interests:
|
| 167 |
+
if interest in keywords:
|
| 168 |
+
score += 2
|
| 169 |
+
|
| 170 |
+
# Check financial needs
|
| 171 |
+
for need in financial_needs:
|
| 172 |
+
if need in keywords:
|
| 173 |
+
score += 2
|
| 174 |
+
|
| 175 |
+
# Only include schemes with a score of 2 or higher
|
| 176 |
+
if score >= 2:
|
| 177 |
+
recommended_schemes.append({
|
| 178 |
+
'id': scheme_id,
|
| 179 |
+
'name': scheme['name'],
|
| 180 |
+
'introduction': scheme['introduction'],
|
| 181 |
+
'score': score
|
| 182 |
+
})
|
| 183 |
+
|
| 184 |
+
# Sort schemes by score (highest first)
|
| 185 |
+
recommended_schemes.sort(key=lambda x: x['score'], reverse=True)
|
| 186 |
+
|
| 187 |
+
# Limit to top 5 schemes
|
| 188 |
+
recommended_schemes = recommended_schemes[:5]
|
| 189 |
+
|
| 190 |
+
return jsonify({
|
| 191 |
+
'schemes': recommended_schemes
|
| 192 |
+
})
|
| 193 |
+
|
| 194 |
@app.route('/scheme/<scheme_id>')
|
| 195 |
def scheme_details(scheme_id):
|
| 196 |
scheme = schemes.get(scheme_id)
|
templates/base.html
CHANGED
|
@@ -35,15 +35,9 @@
|
|
| 35 |
font-weight: 600;
|
| 36 |
}
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
gap: 1.5rem;
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
.scheme-grid .col-sm-12 {
|
| 45 |
-
width: 100%;
|
| 46 |
-
padding: 0;
|
| 47 |
}
|
| 48 |
|
| 49 |
.card {
|
|
@@ -103,6 +97,37 @@
|
|
| 103 |
|
| 104 |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
|
| 105 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
// Add to the script section in base.html
|
| 107 |
function filterSchemes() {
|
| 108 |
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
|
|
@@ -123,16 +148,17 @@
|
|
| 123 |
const matchesBeneficiary = !beneficiary || content.includes(beneficiary.toLowerCase());
|
| 124 |
const matchesBenefit = !benefit || content.includes(benefit.toLowerCase());
|
| 125 |
|
|
|
|
| 126 |
if (matchesSearch && matchesType && matchesBeneficiary && matchesBenefit) {
|
| 127 |
-
|
| 128 |
visibleCount++;
|
| 129 |
} else {
|
| 130 |
-
|
| 131 |
}
|
| 132 |
});
|
| 133 |
|
| 134 |
// Show a message when no results are found
|
| 135 |
-
const container = document.querySelector('.scheme-
|
| 136 |
let noResultsMsg = document.querySelector('.no-results-message');
|
| 137 |
|
| 138 |
if (visibleCount === 0 && container) {
|
|
@@ -145,10 +171,44 @@
|
|
| 145 |
} else if (noResultsMsg) {
|
| 146 |
noResultsMsg.remove();
|
| 147 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
}
|
| 149 |
|
| 150 |
// Add event listeners when DOM is loaded
|
| 151 |
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
document.getElementById('searchInput')?.addEventListener('input', filterSchemes);
|
| 153 |
document.getElementById('schemeTypeFilter')?.addEventListener('change', filterSchemes);
|
| 154 |
document.getElementById('beneficiaryFilter')?.addEventListener('change', filterSchemes);
|
|
|
|
| 35 |
font-weight: 600;
|
| 36 |
}
|
| 37 |
|
| 38 |
+
/* Remove the grid layout for scheme-grid */
|
| 39 |
+
.scheme-container {
|
| 40 |
+
/* Using Bootstrap's row system now */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
.card {
|
|
|
|
| 97 |
|
| 98 |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
|
| 99 |
<script>
|
| 100 |
+
// Function to update URL parameters based on filter values
|
| 101 |
+
function updateUrlParams() {
|
| 102 |
+
const searchTerm = document.getElementById('searchInput')?.value || '';
|
| 103 |
+
const schemeType = document.getElementById('schemeTypeFilter')?.value || '';
|
| 104 |
+
const beneficiary = document.getElementById('beneficiaryFilter')?.value || '';
|
| 105 |
+
const benefit = document.getElementById('benefitFilter')?.value || '';
|
| 106 |
+
|
| 107 |
+
// Save to localStorage
|
| 108 |
+
localStorage.setItem('agri_search', searchTerm);
|
| 109 |
+
localStorage.setItem('agri_schemeType', schemeType);
|
| 110 |
+
localStorage.setItem('agri_beneficiary', beneficiary);
|
| 111 |
+
localStorage.setItem('agri_benefit', benefit);
|
| 112 |
+
|
| 113 |
+
// Update URL parameters
|
| 114 |
+
const url = new URL(window.location);
|
| 115 |
+
if (searchTerm) url.searchParams.set('search', searchTerm);
|
| 116 |
+
else url.searchParams.delete('search');
|
| 117 |
+
|
| 118 |
+
if (schemeType) url.searchParams.set('type', schemeType);
|
| 119 |
+
else url.searchParams.delete('type');
|
| 120 |
+
|
| 121 |
+
if (beneficiary) url.searchParams.set('beneficiary', beneficiary);
|
| 122 |
+
else url.searchParams.delete('beneficiary');
|
| 123 |
+
|
| 124 |
+
if (benefit) url.searchParams.set('benefit', benefit);
|
| 125 |
+
else url.searchParams.delete('benefit');
|
| 126 |
+
|
| 127 |
+
// Update URL without reloading the page
|
| 128 |
+
window.history.pushState({}, '', url);
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
// Add to the script section in base.html
|
| 132 |
function filterSchemes() {
|
| 133 |
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
|
|
|
|
| 148 |
const matchesBeneficiary = !beneficiary || content.includes(beneficiary.toLowerCase());
|
| 149 |
const matchesBenefit = !benefit || content.includes(benefit.toLowerCase());
|
| 150 |
|
| 151 |
+
const cardParent = card.closest('.col-sm-12');
|
| 152 |
if (matchesSearch && matchesType && matchesBeneficiary && matchesBenefit) {
|
| 153 |
+
cardParent.style.display = '';
|
| 154 |
visibleCount++;
|
| 155 |
} else {
|
| 156 |
+
cardParent.style.display = 'none';
|
| 157 |
}
|
| 158 |
});
|
| 159 |
|
| 160 |
// Show a message when no results are found
|
| 161 |
+
const container = document.querySelector('.scheme-container');
|
| 162 |
let noResultsMsg = document.querySelector('.no-results-message');
|
| 163 |
|
| 164 |
if (visibleCount === 0 && container) {
|
|
|
|
| 171 |
} else if (noResultsMsg) {
|
| 172 |
noResultsMsg.remove();
|
| 173 |
}
|
| 174 |
+
|
| 175 |
+
// Update URL and localStorage
|
| 176 |
+
updateUrlParams();
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// Function to restore filters from URL or localStorage
|
| 180 |
+
function restoreFilters() {
|
| 181 |
+
const params = new URLSearchParams(window.location.search);
|
| 182 |
+
|
| 183 |
+
// Get values from URL params or localStorage
|
| 184 |
+
const searchValue = params.get('search') || localStorage.getItem('agri_search') || '';
|
| 185 |
+
const typeValue = params.get('type') || localStorage.getItem('agri_schemeType') || '';
|
| 186 |
+
const beneficiaryValue = params.get('beneficiary') || localStorage.getItem('agri_beneficiary') || '';
|
| 187 |
+
const benefitValue = params.get('benefit') || localStorage.getItem('agri_benefit') || '';
|
| 188 |
+
|
| 189 |
+
// Set form values
|
| 190 |
+
const searchInput = document.getElementById('searchInput');
|
| 191 |
+
const schemeTypeFilter = document.getElementById('schemeTypeFilter');
|
| 192 |
+
const beneficiaryFilter = document.getElementById('beneficiaryFilter');
|
| 193 |
+
const benefitFilter = document.getElementById('benefitFilter');
|
| 194 |
+
|
| 195 |
+
if (searchInput) searchInput.value = searchValue;
|
| 196 |
+
if (schemeTypeFilter) schemeTypeFilter.value = typeValue;
|
| 197 |
+
if (beneficiaryFilter) beneficiaryFilter.value = beneficiaryValue;
|
| 198 |
+
if (benefitFilter) benefitFilter.value = benefitValue;
|
| 199 |
+
|
| 200 |
+
// Apply filters if any were set
|
| 201 |
+
if (searchValue || typeValue || beneficiaryValue || benefitValue) {
|
| 202 |
+
filterSchemes();
|
| 203 |
+
}
|
| 204 |
}
|
| 205 |
|
| 206 |
// Add event listeners when DOM is loaded
|
| 207 |
document.addEventListener('DOMContentLoaded', function() {
|
| 208 |
+
// Restore filters
|
| 209 |
+
restoreFilters();
|
| 210 |
+
|
| 211 |
+
// Add event listeners
|
| 212 |
document.getElementById('searchInput')?.addEventListener('input', filterSchemes);
|
| 213 |
document.getElementById('schemeTypeFilter')?.addEventListener('change', filterSchemes);
|
| 214 |
document.getElementById('beneficiaryFilter')?.addEventListener('change', filterSchemes);
|
templates/chatbot.html
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% extends "base.html" %}
|
| 2 |
+
|
| 3 |
+
{% block content %}
|
| 4 |
+
<div class="container">
|
| 5 |
+
<h1 class="page-title text-center mb-4">Find the Right Scheme for You</h1>
|
| 6 |
+
|
| 7 |
+
<div class="row justify-content-center">
|
| 8 |
+
<div class="col-lg-8">
|
| 9 |
+
<div class="card shadow-sm">
|
| 10 |
+
<div class="card-body">
|
| 11 |
+
<div id="chat-container">
|
| 12 |
+
<div id="chat-messages" class="mb-4">
|
| 13 |
+
<div class="chat-message bot">
|
| 14 |
+
<div class="message-content">
|
| 15 |
+
Hello! I can help you find the perfect agricultural scheme for your needs. Let's start with a few questions.
|
| 16 |
+
</div>
|
| 17 |
+
</div>
|
| 18 |
+
</div>
|
| 19 |
+
|
| 20 |
+
<div id="question-container" class="mb-3">
|
| 21 |
+
<!-- Current question will be displayed here -->
|
| 22 |
+
</div>
|
| 23 |
+
|
| 24 |
+
<div id="user-input" class="d-flex">
|
| 25 |
+
<input type="text" id="user-response" class="form-control me-2" placeholder="Type your answer..." disabled>
|
| 26 |
+
<button id="send-button" class="btn btn-primary" disabled>Send</button>
|
| 27 |
+
</div>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<div id="recommendations-container" class="mt-4" style="display: none;">
|
| 31 |
+
<h3 class="section-title">Recommended Schemes</h3>
|
| 32 |
+
<div id="schemes-list" class="mt-3">
|
| 33 |
+
<!-- Recommended schemes will be displayed here -->
|
| 34 |
+
</div>
|
| 35 |
+
<div class="mt-4 text-center">
|
| 36 |
+
<a href="/chatbot" class="btn btn-outline-primary me-2">Start Over</a>
|
| 37 |
+
<a href="/" class="btn btn-primary">View All Schemes</a>
|
| 38 |
+
</div>
|
| 39 |
+
</div>
|
| 40 |
+
</div>
|
| 41 |
+
</div>
|
| 42 |
+
</div>
|
| 43 |
+
</div>
|
| 44 |
+
</div>
|
| 45 |
+
{% endblock %}
|
| 46 |
+
|
| 47 |
+
{% block extra_scripts %}
|
| 48 |
+
<script>
|
| 49 |
+
// Chat flow and questions
|
| 50 |
+
const chatFlow = [
|
| 51 |
+
{
|
| 52 |
+
id: 'farmer_type',
|
| 53 |
+
question: 'What type of farmer are you?',
|
| 54 |
+
type: 'options',
|
| 55 |
+
options: [
|
| 56 |
+
{ value: 'small', label: 'Small or Marginal Farmer' },
|
| 57 |
+
{ value: 'tenant', label: 'Tenant Farmer or Sharecropper' },
|
| 58 |
+
{ value: 'all', label: 'Landholding Farmer' },
|
| 59 |
+
{ value: 'fpo', label: 'Part of a Farmer Producer Organization (FPO)' }
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
id: 'interests',
|
| 64 |
+
question: 'What aspects of farming are you interested in? (Select all that apply)',
|
| 65 |
+
type: 'checkbox',
|
| 66 |
+
options: [
|
| 67 |
+
{ value: 'organic', label: 'Organic Farming' },
|
| 68 |
+
{ value: 'irrigation', label: 'Irrigation & Water Management' },
|
| 69 |
+
{ value: 'infrastructure', label: 'Farm Infrastructure' },
|
| 70 |
+
{ value: 'soil', label: 'Soil Health Management' },
|
| 71 |
+
{ value: 'market', label: 'Market Access & Selling Produce' }
|
| 72 |
+
]
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
id: 'financial_needs',
|
| 76 |
+
question: 'What type of financial support are you looking for? (Select all that apply)',
|
| 77 |
+
type: 'checkbox',
|
| 78 |
+
options: [
|
| 79 |
+
{ value: 'credit', label: 'Loans & Credit' },
|
| 80 |
+
{ value: 'subsidy', label: 'Equipment/Input Subsidies' },
|
| 81 |
+
{ value: 'insurance', label: 'Crop Insurance' },
|
| 82 |
+
{ value: 'income', label: 'Direct Income Support' },
|
| 83 |
+
{ value: 'training', label: 'Training & Education' }
|
| 84 |
+
]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
id: 'region',
|
| 88 |
+
question: 'Which state are you from?',
|
| 89 |
+
type: 'select',
|
| 90 |
+
options: [
|
| 91 |
+
{ value: 'all', label: 'All India (National Scheme)' },
|
| 92 |
+
{ value: 'andhra', label: 'Andhra Pradesh' },
|
| 93 |
+
{ value: 'assam', label: 'Assam' },
|
| 94 |
+
{ value: 'bihar', label: 'Bihar' },
|
| 95 |
+
{ value: 'gujarat', label: 'Gujarat' },
|
| 96 |
+
{ value: 'haryana', label: 'Haryana' },
|
| 97 |
+
{ value: 'karnataka', label: 'Karnataka' },
|
| 98 |
+
{ value: 'kerala', label: 'Kerala' },
|
| 99 |
+
{ value: 'madhya', label: 'Madhya Pradesh' },
|
| 100 |
+
{ value: 'maharashtra', label: 'Maharashtra' },
|
| 101 |
+
{ value: 'punjab', label: 'Punjab' },
|
| 102 |
+
{ value: 'rajasthan', label: 'Rajasthan' },
|
| 103 |
+
{ value: 'tamil', label: 'Tamil Nadu' },
|
| 104 |
+
{ value: 'telangana', label: 'Telangana' },
|
| 105 |
+
{ value: 'uttar', label: 'Uttar Pradesh' },
|
| 106 |
+
{ value: 'west', label: 'West Bengal' },
|
| 107 |
+
{ value: 'other', label: 'Other' }
|
| 108 |
+
]
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
id: 'land_area',
|
| 112 |
+
question: 'How much land do you cultivate (in acres)?',
|
| 113 |
+
type: 'select',
|
| 114 |
+
options: [
|
| 115 |
+
{ value: 'less_than_1', label: 'Less than 1 acre' },
|
| 116 |
+
{ value: '1_to_2', label: '1-2 acres' },
|
| 117 |
+
{ value: '2_to_5', label: '2-5 acres' },
|
| 118 |
+
{ value: '5_to_10', label: '5-10 acres' },
|
| 119 |
+
{ value: 'more_than_10', label: 'More than 10 acres' }
|
| 120 |
+
]
|
| 121 |
+
}
|
| 122 |
+
];
|
| 123 |
+
|
| 124 |
+
// Store answers
|
| 125 |
+
let userAnswers = {};
|
| 126 |
+
let currentQuestionIndex = 0;
|
| 127 |
+
|
| 128 |
+
// DOM elements
|
| 129 |
+
const questionContainer = document.getElementById('question-container');
|
| 130 |
+
const chatMessages = document.getElementById('chat-messages');
|
| 131 |
+
const userResponseInput = document.getElementById('user-response');
|
| 132 |
+
const sendButton = document.getElementById('send-button');
|
| 133 |
+
const recommendationsContainer = document.getElementById('recommendations-container');
|
| 134 |
+
const schemesList = document.getElementById('schemes-list');
|
| 135 |
+
|
| 136 |
+
// Start the conversation
|
| 137 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 138 |
+
displayQuestion(chatFlow[currentQuestionIndex]);
|
| 139 |
+
});
|
| 140 |
+
|
| 141 |
+
// Display a new question
|
| 142 |
+
function displayQuestion(questionObj) {
|
| 143 |
+
questionContainer.innerHTML = '';
|
| 144 |
+
let questionHTML = `<p class="mb-3">${questionObj.question}</p>`;
|
| 145 |
+
|
| 146 |
+
if (questionObj.type === 'options') {
|
| 147 |
+
questionHTML += `<div class="options-container">`;
|
| 148 |
+
questionObj.options.forEach(option => {
|
| 149 |
+
questionHTML += `
|
| 150 |
+
<div class="form-check mb-2">
|
| 151 |
+
<input class="form-check-input" type="radio" name="${questionObj.id}" id="${option.value}" value="${option.value}">
|
| 152 |
+
<label class="form-check-label" for="${option.value}">
|
| 153 |
+
${option.label}
|
| 154 |
+
</label>
|
| 155 |
+
</div>
|
| 156 |
+
`;
|
| 157 |
+
});
|
| 158 |
+
questionHTML += `</div><button class="btn btn-primary mt-2" onclick="submitAnswer()">Next</button>`;
|
| 159 |
+
} else if (questionObj.type === 'checkbox') {
|
| 160 |
+
questionHTML += `<div class="options-container">`;
|
| 161 |
+
questionObj.options.forEach(option => {
|
| 162 |
+
questionHTML += `
|
| 163 |
+
<div class="form-check mb-2">
|
| 164 |
+
<input class="form-check-input" type="checkbox" name="${questionObj.id}" id="${option.value}" value="${option.value}">
|
| 165 |
+
<label class="form-check-label" for="${option.value}">
|
| 166 |
+
${option.label}
|
| 167 |
+
</label>
|
| 168 |
+
</div>
|
| 169 |
+
`;
|
| 170 |
+
});
|
| 171 |
+
questionHTML += `</div><button class="btn btn-primary mt-2" onclick="submitAnswer()">Next</button>`;
|
| 172 |
+
} else if (questionObj.type === 'select') {
|
| 173 |
+
questionHTML += `<select class="form-select" id="${questionObj.id}">`;
|
| 174 |
+
questionHTML += `<option value="" selected disabled>Please select...</option>`;
|
| 175 |
+
questionObj.options.forEach(option => {
|
| 176 |
+
questionHTML += `<option value="${option.value}">${option.label}</option>`;
|
| 177 |
+
});
|
| 178 |
+
questionHTML += `</select><button class="btn btn-primary mt-2" onclick="submitAnswer()">Next</button>`;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
questionContainer.innerHTML = questionHTML;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// Submit answer and move to next question
|
| 185 |
+
function submitAnswer() {
|
| 186 |
+
const currentQuestion = chatFlow[currentQuestionIndex];
|
| 187 |
+
|
| 188 |
+
// Get user's answer
|
| 189 |
+
let answer;
|
| 190 |
+
|
| 191 |
+
if (currentQuestion.type === 'options') {
|
| 192 |
+
const selectedOption = document.querySelector(`input[name="${currentQuestion.id}"]:checked`);
|
| 193 |
+
if (!selectedOption) return; // Require an answer
|
| 194 |
+
answer = selectedOption.value;
|
| 195 |
+
|
| 196 |
+
// Add user message
|
| 197 |
+
const selectedLabel = currentQuestion.options.find(opt => opt.value === answer).label;
|
| 198 |
+
addMessage('user', selectedLabel);
|
| 199 |
+
|
| 200 |
+
} else if (currentQuestion.type === 'checkbox') {
|
| 201 |
+
const selectedOptions = document.querySelectorAll(`input[name="${currentQuestion.id}"]:checked`);
|
| 202 |
+
if (selectedOptions.length === 0) return; // Require at least one selection
|
| 203 |
+
|
| 204 |
+
answer = Array.from(selectedOptions).map(opt => opt.value);
|
| 205 |
+
|
| 206 |
+
// Add user message
|
| 207 |
+
const selectedLabels = Array.from(selectedOptions).map(opt => {
|
| 208 |
+
const option = currentQuestion.options.find(o => o.value === opt.value);
|
| 209 |
+
return option ? option.label : '';
|
| 210 |
+
}).join(', ');
|
| 211 |
+
addMessage('user', selectedLabels);
|
| 212 |
+
|
| 213 |
+
} else if (currentQuestion.type === 'select') {
|
| 214 |
+
const selectElement = document.getElementById(currentQuestion.id);
|
| 215 |
+
if (!selectElement.value) return; // Require a selection
|
| 216 |
+
answer = selectElement.value;
|
| 217 |
+
|
| 218 |
+
// Add user message
|
| 219 |
+
const selectedLabel = currentQuestion.options.find(opt => opt.value === answer).label;
|
| 220 |
+
addMessage('user', selectedLabel);
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
// Store the answer
|
| 224 |
+
userAnswers[currentQuestion.id] = answer;
|
| 225 |
+
|
| 226 |
+
// Move to next question or finish
|
| 227 |
+
currentQuestionIndex++;
|
| 228 |
+
if (currentQuestionIndex < chatFlow.length) {
|
| 229 |
+
// Display next question
|
| 230 |
+
displayQuestion(chatFlow[currentQuestionIndex]);
|
| 231 |
+
} else {
|
| 232 |
+
// Finish conversation and show recommendations
|
| 233 |
+
finishConversation();
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
// Add a message to the chat
|
| 238 |
+
function addMessage(sender, content) {
|
| 239 |
+
const messageDiv = document.createElement('div');
|
| 240 |
+
messageDiv.className = `chat-message ${sender}`;
|
| 241 |
+
messageDiv.innerHTML = `
|
| 242 |
+
<div class="message-content">
|
| 243 |
+
${content}
|
| 244 |
+
</div>
|
| 245 |
+
`;
|
| 246 |
+
chatMessages.appendChild(messageDiv);
|
| 247 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
// Finish conversation and get recommendations
|
| 251 |
+
function finishConversation() {
|
| 252 |
+
// Display loading message
|
| 253 |
+
questionContainer.innerHTML = `
|
| 254 |
+
<div class="text-center">
|
| 255 |
+
<div class="spinner-border text-primary" role="status">
|
| 256 |
+
<span class="visually-hidden">Loading...</span>
|
| 257 |
+
</div>
|
| 258 |
+
<p class="mt-2">Analyzing your needs to find the best schemes...</p>
|
| 259 |
+
</div>
|
| 260 |
+
`;
|
| 261 |
+
|
| 262 |
+
// Add final bot message
|
| 263 |
+
addMessage('bot', "Thanks for answering all the questions. Let me find the best schemes for you based on your needs.");
|
| 264 |
+
|
| 265 |
+
// Make API call to get recommendations
|
| 266 |
+
fetch('/api/recommend', {
|
| 267 |
+
method: 'POST',
|
| 268 |
+
headers: {
|
| 269 |
+
'Content-Type': 'application/json',
|
| 270 |
+
},
|
| 271 |
+
body: JSON.stringify({ answers: userAnswers }),
|
| 272 |
+
})
|
| 273 |
+
.then(response => response.json())
|
| 274 |
+
.then(data => {
|
| 275 |
+
// Hide question area
|
| 276 |
+
questionContainer.style.display = 'none';
|
| 277 |
+
document.getElementById('user-input').style.display = 'none';
|
| 278 |
+
|
| 279 |
+
// Display recommendations
|
| 280 |
+
displayRecommendations(data.schemes);
|
| 281 |
+
})
|
| 282 |
+
.catch(error => {
|
| 283 |
+
console.error('Error:', error);
|
| 284 |
+
questionContainer.innerHTML = `
|
| 285 |
+
<div class="alert alert-danger">
|
| 286 |
+
Sorry, there was an error finding recommendations. Please try again.
|
| 287 |
+
</div>
|
| 288 |
+
<button class="btn btn-primary" onclick="location.reload()">Restart</button>
|
| 289 |
+
`;
|
| 290 |
+
});
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
// Display scheme recommendations
|
| 294 |
+
function displayRecommendations(recommendations) {
|
| 295 |
+
if (recommendations.length === 0) {
|
| 296 |
+
schemesList.innerHTML = `
|
| 297 |
+
<div class="alert alert-info">
|
| 298 |
+
No schemes match your specific criteria. Try broadening your requirements or view all available schemes.
|
| 299 |
+
</div>
|
| 300 |
+
`;
|
| 301 |
+
} else {
|
| 302 |
+
let schemesHTML = '';
|
| 303 |
+
recommendations.forEach(scheme => {
|
| 304 |
+
schemesHTML += `
|
| 305 |
+
<div class="card mb-3">
|
| 306 |
+
<div class="card-body">
|
| 307 |
+
<h5 class="card-title text-primary">${scheme.name}</h5>
|
| 308 |
+
<p class="card-text">${scheme.introduction}</p>
|
| 309 |
+
<a href="/scheme/${scheme.id}" class="btn btn-outline-primary">View Details</a>
|
| 310 |
+
</div>
|
| 311 |
+
</div>
|
| 312 |
+
`;
|
| 313 |
+
});
|
| 314 |
+
schemesList.innerHTML = schemesHTML;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
// Show recommendations container
|
| 318 |
+
recommendationsContainer.style.display = 'block';
|
| 319 |
+
}
|
| 320 |
+
</script>
|
| 321 |
+
|
| 322 |
+
<style>
|
| 323 |
+
.chat-message {
|
| 324 |
+
margin-bottom: 15px;
|
| 325 |
+
display: flex;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.chat-message.user {
|
| 329 |
+
justify-content: flex-end;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
.message-content {
|
| 333 |
+
padding: 10px 15px;
|
| 334 |
+
border-radius: 15px;
|
| 335 |
+
max-width: 80%;
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
.chat-message.bot .message-content {
|
| 339 |
+
background-color: #f0f0f0;
|
| 340 |
+
border-top-left-radius: 5px;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
.chat-message.user .message-content {
|
| 344 |
+
background-color: var(--primary-color);
|
| 345 |
+
color: white;
|
| 346 |
+
border-top-right-radius: 5px;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
#chat-messages {
|
| 350 |
+
max-height: 300px;
|
| 351 |
+
overflow-y: auto;
|
| 352 |
+
padding: 15px;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
#question-container {
|
| 356 |
+
background-color: #f8f9fa;
|
| 357 |
+
padding: 15px;
|
| 358 |
+
border-radius: 10px;
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
.options-container {
|
| 362 |
+
margin-top: 10px;
|
| 363 |
+
}
|
| 364 |
+
</style>
|
| 365 |
+
{% endblock %}
|
templates/index.html
CHANGED
|
@@ -3,7 +3,12 @@
|
|
| 3 |
{% block content %}
|
| 4 |
<div class="container">
|
| 5 |
<h1 class="page-title text-center mb-5">Indian Agricultural Schemes</h1>
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
<div class="filters mb-4">
|
| 8 |
<div class="row g-3">
|
| 9 |
<div class="col-md-4">
|
|
@@ -45,9 +50,9 @@
|
|
| 45 |
</div>
|
| 46 |
</div>
|
| 47 |
|
| 48 |
-
<div class="scheme-
|
| 49 |
{% for scheme_id, scheme in schemes.items() %}
|
| 50 |
-
<div class="col-sm-12 col-md-6 col-lg-4">
|
| 51 |
<a href="{{ url_for('scheme_details', scheme_id=scheme_id) }}" class="text-decoration-none">
|
| 52 |
<div class="card h-100 scheme-card" data-keywords="{{ scheme.keywords }}">
|
| 53 |
<div class="card-body d-flex flex-column">
|
|
|
|
| 3 |
{% block content %}
|
| 4 |
<div class="container">
|
| 5 |
<h1 class="page-title text-center mb-5">Indian Agricultural Schemes</h1>
|
| 6 |
+
<div class="text-center mb-5">
|
| 7 |
+
<p class="lead">Find government schemes tailored to your farming needs</p>
|
| 8 |
+
<a href="{{ url_for('chatbot') }}" class="btn btn-primary btn-lg mt-2">
|
| 9 |
+
<i class="fas fa-robot me-2"></i>Find the Right Scheme for You
|
| 10 |
+
</a>
|
| 11 |
+
</div>
|
| 12 |
<div class="filters mb-4">
|
| 13 |
<div class="row g-3">
|
| 14 |
<div class="col-md-4">
|
|
|
|
| 50 |
</div>
|
| 51 |
</div>
|
| 52 |
|
| 53 |
+
<div class="row g-4 scheme-container">
|
| 54 |
{% for scheme_id, scheme in schemes.items() %}
|
| 55 |
+
<div class="col-sm-12 col-md-6 col-lg-4 mb-4">
|
| 56 |
<a href="{{ url_for('scheme_details', scheme_id=scheme_id) }}" class="text-decoration-none">
|
| 57 |
<div class="card h-100 scheme-card" data-keywords="{{ scheme.keywords }}">
|
| 58 |
<div class="card-body d-flex flex-column">
|