-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7256a73
Showing
94 changed files
with
3,224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"CurrentProjectSetting": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ExpandedNodes": [ | ||
"" | ||
], | ||
"SelectedNode": "\\app.py", | ||
"PreviewInSolutionExplorer": false | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
import pandas as pd | ||
from utils.fertilizer import fertilizer_dic | ||
import numpy as np | ||
from flask import Flask, render_template, request, Markup | ||
import requests | ||
import pickle | ||
from flask_mail import Mail, Message | ||
from config import mail_username, mail_password | ||
# --------- | ||
|
||
crop_translator = { | ||
'rice' : 'riz', | ||
'maize' : 'maïs', | ||
'chickpea' : 'pois chiche', | ||
'kidneybeans' : 'haricots rénaux', | ||
'pigeonpeas' : "pois d'Angole", | ||
'mothbeans' : 'haricots papillon', | ||
'mungbean' : 'haricot mungo', | ||
'blackgram' : 'mâle', | ||
'lentil' : 'lentille', | ||
'pomegranate' : 'grenade', | ||
'banana' : 'banane', | ||
'mango' : 'mangue', | ||
'grapes' : 'les raisins', | ||
'watermelon' : 'pastèque', | ||
'muskmelon' : 'cantaloup', | ||
'apple' : 'pomme', | ||
'orange' : 'orange', | ||
'papaya' : 'papaye', | ||
'coconut' : 'noix de coco', | ||
'cotton' : 'coton', | ||
'jute' : 'jute', | ||
'coffee' : 'café' | ||
} | ||
inv_crop_translator = {v : k for k, v in crop_translator.items()} | ||
# --------- | ||
def city_name_separater(city_name): | ||
x = city_name.split(' ') | ||
if len(x) != 1: | ||
result = "" | ||
for i in range(len(x)): | ||
result += x[i] + "+" | ||
return result[:-1] | ||
else : | ||
return city_name | ||
|
||
def weather_fetch(city_name): | ||
city = city_name_separater(city_name) | ||
""" | ||
Fetch and returns the temperature and humidity of a city | ||
:params: city_name | ||
:return: temperature, humidity | ||
""" | ||
api_key = "9d7cde1f6d07ec55650544be1631307e" | ||
base_url = "http://api.openweathermap.org/data/2.5/weather?" | ||
|
||
complete_url = base_url + "appid=" + api_key + "&q=" + city | ||
response = requests.get(complete_url) | ||
x = response.json() | ||
|
||
|
||
if x["cod"] != "404": | ||
y = x["main"] | ||
temperature = round((y["temp"] - 273.15), 2) | ||
humidity = y["humidity"] | ||
return temperature, humidity | ||
else: | ||
return None | ||
|
||
|
||
|
||
crop_recommendation_model_path = 'models/RandomForest.pkl' | ||
crop_recommendation_model = pickle.load( | ||
open(crop_recommendation_model_path, 'rb')) | ||
|
||
|
||
# --------- | ||
app = Flask(__name__) | ||
|
||
app.config['MAIL_SERVER'] = 'smtp.gmail.com' | ||
app.config['MAIL_PORT'] = 587 | ||
app.config['MAIL_USE_TLS'] = True | ||
app.config['MAIL_USE_SSL'] = False | ||
app.config["MAIL_USERNAME"] = mail_username | ||
app.config["MAIL_PASSWORD"] = mail_password | ||
mail = Mail(app) | ||
|
||
#render Accueil page | ||
@ app.route('/') | ||
def Accueil(): | ||
title = 'AIfarm - Accueil' | ||
return render_template('index.html', title=title) | ||
|
||
#render Recommandation de culture form page | ||
@ app.route('/culture-recommandation') | ||
def crop_recommendation(): | ||
title = 'AIfarm - Recommandation de culture' | ||
return render_template('crops.html', title=title) | ||
|
||
#render Recommandation de culture result page | ||
@ app.route('/culture-predire', methods=['POST']) | ||
def crop_prediction(): | ||
title = 'AIfarm - Recommandation de culture' | ||
if request.method == 'POST': | ||
N = int(request.form['nitrogen']) | ||
P = int(request.form['phosphorous']) | ||
K = int(request.form['potassium']) | ||
ph = float(request.form['ph']) | ||
rainfall = float(request.form['rainfall']) | ||
city = request.form['city'] | ||
if weather_fetch(city) != None: | ||
temperature, humidity = weather_fetch(city) | ||
data = np.array([[N, P, K, temperature, humidity, ph, rainfall]]) | ||
my_prediction = crop_recommendation_model.predict(data) | ||
final_prediction = crop_translator[my_prediction[0]] | ||
|
||
return render_template('crop_prediction.html', prediction=final_prediction, title=title) | ||
|
||
else: | ||
return render_template('try_again.html', title=title) | ||
|
||
# -------- | ||
|
||
#render fertilizer recommendation form page | ||
@app.route('/engrais-recommandation') | ||
def fertilizer_recommendation(): | ||
title = 'AIfarm - Engrais recommandatoin' | ||
return render_template('fertilizer.html', title=title) | ||
|
||
#render fertilizer results page | ||
@app.route('/engrais-predire', methods=['POST']) | ||
def fert_recommend(): | ||
title = 'AIfarm - Engrais recommandation' | ||
|
||
crop_name_fr = str(request.form['cropname']) | ||
crop_name = inv_crop_translator[crop_name_fr] | ||
print(crop_name) | ||
N = int(request.form['nitrogen']) | ||
P = int(request.form['phosphorous']) | ||
K = int(request.form['potassium']) | ||
# ph = float(request.form['ph']) | ||
|
||
df = pd.read_csv('data/fertilizer.csv') | ||
|
||
nr = df[df['Crop'] == crop_name]['N'].iloc[0] | ||
pr = df[df['Crop'] == crop_name]['P'].iloc[0] | ||
kr = df[df['Crop'] == crop_name]['K'].iloc[0] | ||
|
||
n = nr - N | ||
p = pr - P | ||
k = kr - K | ||
|
||
temp = {abs(n): "N", abs(p): "P", abs(k): "K"} | ||
max_value = temp[max(temp.keys())] | ||
if max(temp) == 0: | ||
key = 'Allgood' | ||
else: | ||
if max_value == "N": | ||
if n < 0: | ||
key = 'NHigh' | ||
else: | ||
key = "Nlow" | ||
elif max_value == "P": | ||
if p < 0: | ||
key = 'PHigh' | ||
else: | ||
key = "Plow" | ||
else: | ||
if k < 0: | ||
key = 'KHigh' | ||
else: | ||
key = "Klow" | ||
response = Markup(str(fertilizer_dic[key])) | ||
|
||
return render_template('fertilizer_prediction.html', recommendation=response, title=title) | ||
|
||
# -------- | ||
|
||
#render services page | ||
@app.route('/services') | ||
def services(): | ||
title = 'AIfarm - Services' | ||
return render_template('service-page.html', title=title) | ||
|
||
# -------- | ||
|
||
#render contact-us page | ||
@app.route('/contact-us', methods=['GET', 'POST']) | ||
def contact_us(): | ||
title = 'AIfarm - contact-us' | ||
if request.method =='POST': | ||
name = str(request.form['name']) | ||
subject = str(request.form['subject']) | ||
email = request.form['email'] | ||
message = str(request.form['message']) | ||
|
||
msg = Message(subject=f'Mail from {name}', body=f"Nom: {name}\nE-mail: {email}\nObjet: {subject}\n\n\nMessage: {message}", sender=mail_username, recipients=['[email protected]', '[email protected]'] ) | ||
print(msg) | ||
mail.send(msg) | ||
return render_template('contact-us.html', success=True) | ||
|
||
|
||
return render_template('contact-us.html', title=title) | ||
|
||
# -------- | ||
|
||
|
||
# -------- | ||
if __name__ == '__main__': | ||
app.run(debug=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mail_username = "[email protected]" | ||
mail_password = "xgewrxrkpacbqtwu" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
,Crop,N,P,K,pH,soil_moisture | ||
0,rice,80,40,40,5.5,30 | ||
3,maize,80,40,20,5.5,50 | ||
5,chickpea,40,60,80,5.5,60 | ||
12,kidneybeans,20,60,20,5.5,45 | ||
13,pigeonpeas,20,60,20,5.5,45 | ||
14,mothbeans,20,40,20,5.5,30 | ||
15,mungbean,20,40,20,5.5,80 | ||
18,blackgram,40,60,20,5,60 | ||
24,lentil,20,60,20,5.5,90 | ||
60,pomegranate,20,10,40,5.5,30 | ||
61,banana,100,75,50,6.5,40 | ||
62,mango,20,20,30,5,15 | ||
63,grapes,20,125,200,4,60 | ||
66,watermelon,100,10,50,5.5,70 | ||
67,muskmelon,100,10,50,5.5,30 | ||
69,apple,20,125,200,6.5,50 | ||
74,orange,20,10,10,4,60 | ||
75,papaya,50,50,50,6,20 | ||
88,coconut,20,10,30,5,45 | ||
93,cotton,120,40,20,5.5,70 | ||
94,jute,80,40,40,5.5,20 | ||
95,coffee,100,20,30,5.5,20 |
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.