Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,12 +15,13 @@ def create_synthetic_dataset():
|
|
| 15 |
'Vizianagaram', 'West Godavari', 'YSR Kadapa'
|
| 16 |
]
|
| 17 |
|
| 18 |
-
# Common crops in Andhra Pradesh
|
| 19 |
crops = [
|
| 20 |
-
'Rice', 'Maize', 'Cotton', 'Groundnut', 'Red Gram (Toor Dal)',
|
| 21 |
'Green Gram (Moong Dal)', 'Black Gram (Urad Dal)', 'Sugarcane',
|
| 22 |
-
'Chilli', '
|
| 23 |
-
'
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
# Months
|
|
@@ -75,6 +76,18 @@ def create_synthetic_dataset():
|
|
| 75 |
if crop == 'Rice' and data['Rainfall'][i] < 100:
|
| 76 |
data['Suitability'][i] = 0
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Groundnut grows well in Anantapur
|
| 79 |
if crop == 'Groundnut' and district == 'Anantapur':
|
| 80 |
data['Suitability'][i] = 1
|
|
@@ -86,6 +99,18 @@ def create_synthetic_dataset():
|
|
| 86 |
# Chilli grows well in Guntur
|
| 87 |
if crop == 'Chilli' and district == 'Guntur':
|
| 88 |
data['Suitability'][i] = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
df = pd.DataFrame(data)
|
| 91 |
return df, crops, districts, months
|
|
@@ -108,7 +133,7 @@ def train_model(df):
|
|
| 108 |
|
| 109 |
model = train_model(df)
|
| 110 |
|
| 111 |
-
# Crop information and precautions
|
| 112 |
crop_info = {
|
| 113 |
'Rice': {
|
| 114 |
'description': 'Staple food crop requiring abundant water',
|
|
@@ -128,6 +153,16 @@ crop_info = {
|
|
| 128 |
'Watch for fall armyworm infestation'
|
| 129 |
]
|
| 130 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
'Cotton': {
|
| 132 |
'description': 'Important cash crop known as "white gold"',
|
| 133 |
'precautions': [
|
|
@@ -191,6 +226,16 @@ crop_info = {
|
|
| 191 |
'Harvest at color break stage'
|
| 192 |
]
|
| 193 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
'Turmeric': {
|
| 195 |
'description': 'Important spice crop with medicinal value',
|
| 196 |
'precautions': [
|
|
@@ -209,6 +254,16 @@ crop_info = {
|
|
| 209 |
'Practice crop rotation'
|
| 210 |
]
|
| 211 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
'Mango': {
|
| 213 |
'description': 'Important fruit crop of Andhra Pradesh',
|
| 214 |
'precautions': [
|
|
@@ -281,7 +336,7 @@ crop_info = {
|
|
| 281 |
}
|
| 282 |
}
|
| 283 |
|
| 284 |
-
# District-wise climate information
|
| 285 |
district_climate = {
|
| 286 |
'Anantapur': {
|
| 287 |
'description': 'Hot and dry climate with low rainfall',
|
|
@@ -363,7 +418,7 @@ district_climate = {
|
|
| 363 |
}
|
| 364 |
}
|
| 365 |
|
| 366 |
-
# Prediction function
|
| 367 |
def predict_crop(district, month, crop_choice=None):
|
| 368 |
# Get current temperature and rainfall based on district and month
|
| 369 |
temp = df[(df['District'] == district) & (df['Month'] == month)]['Temperature'].mean()
|
|
@@ -460,13 +515,12 @@ def get_alternative_crops(district, month):
|
|
| 460 |
crop_scores.sort(key=lambda x: x[1], reverse=True)
|
| 461 |
return [crop for crop, score in crop_scores if score > 0.7]
|
| 462 |
|
| 463 |
-
# Custom CSS for styling
|
| 464 |
css = """
|
| 465 |
.gradio-container {
|
| 466 |
font-family: 'Poppins', sans-serif;
|
| 467 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 468 |
}
|
| 469 |
-
|
| 470 |
.title {
|
| 471 |
text-align: center;
|
| 472 |
color: #2c3e50;
|
|
@@ -477,14 +531,12 @@ css = """
|
|
| 477 |
-webkit-background-clip: text;
|
| 478 |
-webkit-text-fill-color: transparent;
|
| 479 |
}
|
| 480 |
-
|
| 481 |
.description {
|
| 482 |
text-align: center;
|
| 483 |
color: #4a5568;
|
| 484 |
margin-bottom: 30px;
|
| 485 |
font-size: 16px;
|
| 486 |
}
|
| 487 |
-
|
| 488 |
.input-section {
|
| 489 |
background: white;
|
| 490 |
padding: 20px;
|
|
@@ -492,14 +544,12 @@ css = """
|
|
| 492 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 493 |
margin-bottom: 20px;
|
| 494 |
}
|
| 495 |
-
|
| 496 |
.input-label {
|
| 497 |
font-weight: 500;
|
| 498 |
color: #2d3748;
|
| 499 |
margin-bottom: 8px;
|
| 500 |
display: block;
|
| 501 |
}
|
| 502 |
-
|
| 503 |
.output-section {
|
| 504 |
background: white;
|
| 505 |
padding: 25px;
|
|
@@ -510,7 +560,6 @@ css = """
|
|
| 510 |
line-height: 1.6;
|
| 511 |
white-space: pre-wrap;
|
| 512 |
}
|
| 513 |
-
|
| 514 |
.output-title {
|
| 515 |
color: #2c3e50;
|
| 516 |
font-weight: 600;
|
|
@@ -519,7 +568,6 @@ css = """
|
|
| 519 |
border-bottom: 2px solid #e2e8f0;
|
| 520 |
padding-bottom: 8px;
|
| 521 |
}
|
| 522 |
-
|
| 523 |
.btn-primary {
|
| 524 |
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 525 |
border: none;
|
|
@@ -531,12 +579,10 @@ css = """
|
|
| 531 |
transition: all 0.3s ease;
|
| 532 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 533 |
}
|
| 534 |
-
|
| 535 |
.btn-primary:hover {
|
| 536 |
transform: translateY(-2px);
|
| 537 |
box-shadow: 0 7px 14px rgba(0, 0, 0, 0.1);
|
| 538 |
}
|
| 539 |
-
|
| 540 |
.select-dropdown, .text-input {
|
| 541 |
width: 100%;
|
| 542 |
padding: 12px;
|
|
@@ -545,28 +591,23 @@ css = """
|
|
| 545 |
font-size: 16px;
|
| 546 |
transition: all 0.3s ease;
|
| 547 |
}
|
| 548 |
-
|
| 549 |
.select-dropdown:focus, .text-input:focus {
|
| 550 |
border-color: #4b6cb7;
|
| 551 |
box-shadow: 0 0 0 3px rgba(75, 108, 183, 0.2);
|
| 552 |
outline: none;
|
| 553 |
}
|
| 554 |
-
|
| 555 |
.footer {
|
| 556 |
text-align: center;
|
| 557 |
margin-top: 30px;
|
| 558 |
color: #718096;
|
| 559 |
font-size: 14px;
|
| 560 |
}
|
| 561 |
-
|
| 562 |
.success {
|
| 563 |
color: #2e7d32;
|
| 564 |
}
|
| 565 |
-
|
| 566 |
.warning {
|
| 567 |
color: #d32f2f;
|
| 568 |
}
|
| 569 |
-
|
| 570 |
.recommendation {
|
| 571 |
background: #f0f4f8;
|
| 572 |
padding: 15px;
|
|
@@ -574,7 +615,6 @@ css = """
|
|
| 574 |
margin-top: 15px;
|
| 575 |
border-left: 4px solid #4b6cb7;
|
| 576 |
}
|
| 577 |
-
|
| 578 |
.crop-image {
|
| 579 |
max-width: 100%;
|
| 580 |
border-radius: 8px;
|
|
|
|
| 15 |
'Vizianagaram', 'West Godavari', 'YSR Kadapa'
|
| 16 |
]
|
| 17 |
|
| 18 |
+
# Common crops in Andhra Pradesh (including new crops)
|
| 19 |
crops = [
|
| 20 |
+
'Rice', 'Maize', 'Corn', 'Cotton', 'Groundnut', 'Red Gram (Toor Dal)',
|
| 21 |
'Green Gram (Moong Dal)', 'Black Gram (Urad Dal)', 'Sugarcane',
|
| 22 |
+
'Chilli', 'Pepper', 'Turmeric', 'Tobacco', 'Sweet Potato', 'Mango',
|
| 23 |
+
'Banana', 'Coconut', 'Cashew', 'Soybean', 'Sunflower',
|
| 24 |
+
'Jowar (Sorghum)', 'Bajra (Pearl Millet)'
|
| 25 |
]
|
| 26 |
|
| 27 |
# Months
|
|
|
|
| 76 |
if crop == 'Rice' and data['Rainfall'][i] < 100:
|
| 77 |
data['Suitability'][i] = 0
|
| 78 |
|
| 79 |
+
# Corn needs moderate water and warm temperature
|
| 80 |
+
if crop == 'Corn' and (data['Rainfall'][i] < 50 or data['Temperature'][i] < 20):
|
| 81 |
+
data['Suitability'][i] = 0
|
| 82 |
+
|
| 83 |
+
# Pepper needs warm, humid conditions
|
| 84 |
+
if crop == 'Pepper' and (data['Temperature'][i] < 20 or data['Rainfall'][i] < 100):
|
| 85 |
+
data['Suitability'][i] = 0
|
| 86 |
+
|
| 87 |
+
# Sweet Potato grows well in warm conditions with moderate rainfall
|
| 88 |
+
if crop == 'Sweet Potato' and (data['Temperature'][i] < 20 or data['Rainfall'][i] > 250):
|
| 89 |
+
data['Suitability'][i] = 0
|
| 90 |
+
|
| 91 |
# Groundnut grows well in Anantapur
|
| 92 |
if crop == 'Groundnut' and district == 'Anantapur':
|
| 93 |
data['Suitability'][i] = 1
|
|
|
|
| 99 |
# Chilli grows well in Guntur
|
| 100 |
if crop == 'Chilli' and district == 'Guntur':
|
| 101 |
data['Suitability'][i] = 1
|
| 102 |
+
|
| 103 |
+
# Corn grows well in Krishna and Guntur
|
| 104 |
+
if crop == 'Corn' and district in ['Krishna', 'Guntur', 'West Godavari']:
|
| 105 |
+
data['Suitability'][i] = 1
|
| 106 |
+
|
| 107 |
+
# Pepper grows well in coastal and hilly areas
|
| 108 |
+
if crop == 'Pepper' and district in ['Visakhapatnam', 'Srikakulam', 'Vizianagaram']:
|
| 109 |
+
data['Suitability'][i] = 1
|
| 110 |
+
|
| 111 |
+
# Sweet Potato grows well in various districts
|
| 112 |
+
if crop == 'Sweet Potato' and district in ['East Godavari', 'West Godavari', 'Krishna']:
|
| 113 |
+
data['Suitability'][i] = 1
|
| 114 |
|
| 115 |
df = pd.DataFrame(data)
|
| 116 |
return df, crops, districts, months
|
|
|
|
| 133 |
|
| 134 |
model = train_model(df)
|
| 135 |
|
| 136 |
+
# Crop information and precautions (updated with new crops)
|
| 137 |
crop_info = {
|
| 138 |
'Rice': {
|
| 139 |
'description': 'Staple food crop requiring abundant water',
|
|
|
|
| 153 |
'Watch for fall armyworm infestation'
|
| 154 |
]
|
| 155 |
},
|
| 156 |
+
'Corn': {
|
| 157 |
+
'description': 'Sweet corn variety popular for direct consumption',
|
| 158 |
+
'precautions': [
|
| 159 |
+
'Plant in well-drained loamy soil',
|
| 160 |
+
'Maintain spacing of 60x25 cm',
|
| 161 |
+
'Harvest when silks turn brown and dry',
|
| 162 |
+
'Control corn earworm and aphids',
|
| 163 |
+
'Irrigate regularly during grain filling stage'
|
| 164 |
+
]
|
| 165 |
+
},
|
| 166 |
'Cotton': {
|
| 167 |
'description': 'Important cash crop known as "white gold"',
|
| 168 |
'precautions': [
|
|
|
|
| 226 |
'Harvest at color break stage'
|
| 227 |
]
|
| 228 |
},
|
| 229 |
+
'Pepper': {
|
| 230 |
+
'description': 'Black pepper, important spice crop',
|
| 231 |
+
'precautions': [
|
| 232 |
+
'Plant in well-drained red loamy soil',
|
| 233 |
+
'Provide support with standards or trellis',
|
| 234 |
+
'Control quick wilt and pollu beetle',
|
| 235 |
+
'Harvest when berries turn orange-red',
|
| 236 |
+
'Provide shade during initial growth'
|
| 237 |
+
]
|
| 238 |
+
},
|
| 239 |
'Turmeric': {
|
| 240 |
'description': 'Important spice crop with medicinal value',
|
| 241 |
'precautions': [
|
|
|
|
| 254 |
'Practice crop rotation'
|
| 255 |
]
|
| 256 |
},
|
| 257 |
+
'Sweet Potato': {
|
| 258 |
+
'description': 'Nutritious root vegetable rich in vitamins',
|
| 259 |
+
'precautions': [
|
| 260 |
+
'Plant in well-drained sandy loam soil',
|
| 261 |
+
'Use vine cuttings for propagation',
|
| 262 |
+
'Control sweet potato weevil',
|
| 263 |
+
'Harvest when leaves turn yellow',
|
| 264 |
+
'Cure properly before storage'
|
| 265 |
+
]
|
| 266 |
+
},
|
| 267 |
'Mango': {
|
| 268 |
'description': 'Important fruit crop of Andhra Pradesh',
|
| 269 |
'precautions': [
|
|
|
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
+
# District-wise climate information (unchanged)
|
| 340 |
district_climate = {
|
| 341 |
'Anantapur': {
|
| 342 |
'description': 'Hot and dry climate with low rainfall',
|
|
|
|
| 418 |
}
|
| 419 |
}
|
| 420 |
|
| 421 |
+
# Prediction function (unchanged)
|
| 422 |
def predict_crop(district, month, crop_choice=None):
|
| 423 |
# Get current temperature and rainfall based on district and month
|
| 424 |
temp = df[(df['District'] == district) & (df['Month'] == month)]['Temperature'].mean()
|
|
|
|
| 515 |
crop_scores.sort(key=lambda x: x[1], reverse=True)
|
| 516 |
return [crop for crop, score in crop_scores if score > 0.7]
|
| 517 |
|
| 518 |
+
# Custom CSS for styling (unchanged)
|
| 519 |
css = """
|
| 520 |
.gradio-container {
|
| 521 |
font-family: 'Poppins', sans-serif;
|
| 522 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 523 |
}
|
|
|
|
| 524 |
.title {
|
| 525 |
text-align: center;
|
| 526 |
color: #2c3e50;
|
|
|
|
| 531 |
-webkit-background-clip: text;
|
| 532 |
-webkit-text-fill-color: transparent;
|
| 533 |
}
|
|
|
|
| 534 |
.description {
|
| 535 |
text-align: center;
|
| 536 |
color: #4a5568;
|
| 537 |
margin-bottom: 30px;
|
| 538 |
font-size: 16px;
|
| 539 |
}
|
|
|
|
| 540 |
.input-section {
|
| 541 |
background: white;
|
| 542 |
padding: 20px;
|
|
|
|
| 544 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 545 |
margin-bottom: 20px;
|
| 546 |
}
|
|
|
|
| 547 |
.input-label {
|
| 548 |
font-weight: 500;
|
| 549 |
color: #2d3748;
|
| 550 |
margin-bottom: 8px;
|
| 551 |
display: block;
|
| 552 |
}
|
|
|
|
| 553 |
.output-section {
|
| 554 |
background: white;
|
| 555 |
padding: 25px;
|
|
|
|
| 560 |
line-height: 1.6;
|
| 561 |
white-space: pre-wrap;
|
| 562 |
}
|
|
|
|
| 563 |
.output-title {
|
| 564 |
color: #2c3e50;
|
| 565 |
font-weight: 600;
|
|
|
|
| 568 |
border-bottom: 2px solid #e2e8f0;
|
| 569 |
padding-bottom: 8px;
|
| 570 |
}
|
|
|
|
| 571 |
.btn-primary {
|
| 572 |
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 573 |
border: none;
|
|
|
|
| 579 |
transition: all 0.3s ease;
|
| 580 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 581 |
}
|
|
|
|
| 582 |
.btn-primary:hover {
|
| 583 |
transform: translateY(-2px);
|
| 584 |
box-shadow: 0 7px 14px rgba(0, 0, 0, 0.1);
|
| 585 |
}
|
|
|
|
| 586 |
.select-dropdown, .text-input {
|
| 587 |
width: 100%;
|
| 588 |
padding: 12px;
|
|
|
|
| 591 |
font-size: 16px;
|
| 592 |
transition: all 0.3s ease;
|
| 593 |
}
|
|
|
|
| 594 |
.select-dropdown:focus, .text-input:focus {
|
| 595 |
border-color: #4b6cb7;
|
| 596 |
box-shadow: 0 0 0 3px rgba(75, 108, 183, 0.2);
|
| 597 |
outline: none;
|
| 598 |
}
|
|
|
|
| 599 |
.footer {
|
| 600 |
text-align: center;
|
| 601 |
margin-top: 30px;
|
| 602 |
color: #718096;
|
| 603 |
font-size: 14px;
|
| 604 |
}
|
|
|
|
| 605 |
.success {
|
| 606 |
color: #2e7d32;
|
| 607 |
}
|
|
|
|
| 608 |
.warning {
|
| 609 |
color: #d32f2f;
|
| 610 |
}
|
|
|
|
| 611 |
.recommendation {
|
| 612 |
background: #f0f4f8;
|
| 613 |
padding: 15px;
|
|
|
|
| 615 |
margin-top: 15px;
|
| 616 |
border-left: 4px solid #4b6cb7;
|
| 617 |
}
|
|
|
|
| 618 |
.crop-image {
|
| 619 |
max-width: 100%;
|
| 620 |
border-radius: 8px;
|