-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
218 lines (167 loc) 路 5.83 KB
/
Copy pathapp.py
File metadata and controls
218 lines (167 loc) 路 5.83 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
210
211
212
213
214
215
216
217
218
from flask import Flask, render_template, jsonify, request
#from flask_cors import CORS, cross_origin
from src.rag import * # Import all defined functions
import joblib
import numpy as np
import pandas as pd
from src.model import NeuralNet
from src.Sentence_Processing import *
from src import *
import json
import torch
import random
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
from deep_translator import GoogleTranslator
from langdetect import detect
from langchain.vectorstores import Pinecone
import pinecone
import os
from langchain.llms import HuggingFaceHub
from langchain import PromptTemplate
import tensorflow as tf
from data.desases import diseases
app = Flask(__name__)
#English
#Home page
@app.route("/")
@app.route("/Home.html")
def home():
return render_template('Home.html')
#Fertify
@app.route("/Fertify.html", methods=["GET", "POST"])
def Fertify():
return render_template('Fertify.html')
@app.route("/predict",methods=["POST","GET"])
def predict_fertilizer():
# loading the model
model = joblib.load('./models/Fertify.pkl')
# Get the data from the POST request
input_data = request.get_json(force=True)
input_data = input_data['input_data']
# Convert data into numpy array
input_data_df = pd.DataFrame(input_data)
print(input_data_df)
#Make prediction
prediction = model.predict(input_data_df)
print(f"prediction {prediction}")
prediction = prediction[0]
# Return the prediction as JSON
result = {
'fertilizer' : prediction
}
return jsonify(result)
#FertiBot
@app.route("/FertyBot.html", methods=["GET", "POST"])
def FertiBot():
return render_template('FertyBot.html')
#PlantGuard
@app.route("/PlantGuard.html", methods=["GET", "POST"])
def PlanGuard():
return render_template('PlantGuard.html')
#contact
@app.route("/Contact.html", methods=["GET", "POST"])
def Contact():
return render_template('Contact.html')
#help chat
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#chatbot
@app.route("/chatbot",methods=["POST","GET"])
def answer_query():
# loading the model
model = joblib.load('./models/chat.pkl')
# Get the data from the POST request
user_input_dict = request.get_json(force=True)
user_input = user_input_dict['input_user']
print(user_input)
#Make prediction
with open('data/intents.json', 'r') as f:
intents = json.load(f)
File = "models/chat.pkl"
data = joblib.load(File)
input_size = data['input_size']
hidden_size = data['hidden_size']
output_size = data['output_size']
all_words = data['all_words']
model_state = data['model_state']
tags = data['tags']
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
#running the chat
sentence = user_input
sentence = Tokenize(sentence)
X = Bag_of_words(sentence,all_words)
X = X.reshape(1,X.shape[0])
X = torch.from_numpy(X)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
response = random.choice(intent["answers"])
else:
response = "I鈥檓 here to assist you as a chatbot, if I can鈥檛 address your query, please contact our support team via the 'Contact Us' link for further assistance."
return jsonify(response)
##Fertibot
@app.route("/fertibot",methods=["POST","GET"])
def Send_Response():
question_dict = request.get_json(force=True)
user_question = question_dict['user_question']
print(f"user question {user_question}")
data_path = './data/Fertilizers'
chroma_path = 'chroma'
documents = load_documents(data_path)
chunks = split_text(documents)
#save_to_chroma(chunks, chroma_path)
#Env Variables
load_dotenv()
response = query_rag(user_question,chroma_path)
truncate_response = truncate_from_keyword(response,'Answer:')
translate_response = detect_language(user_question,truncate_response)
final_response = generate_final_response(translate_response)
print(final_response)
return jsonify(final_response)
##plantguard
@app.route("/plantguard",methods=["POST","GET"])
def Show_Diases():
file = request.files['image']
file.save(os.path.join('uploaded', 'leaf.png'))
cnn = tf.keras.models.load_model('./models/trained_plant_disease_model.keras')
image = tf.keras.preprocessing.image.load_img('uploaded/leaf.png',target_size=(128,128))
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr]) # Convert single image to a batch.
predictions = cnn.predict(input_arr)
# Import the dictionary from desease_desc.py
result_index = np.argmax(predictions) #Return index of max element
print(result_index)
print(diseases[result_index])
return jsonify(diseases[result_index])
# French
@app.route("/Home-fr.html")
def Home_fr():
return render_template('Home-fr.html')
#ferify
@app.route("/Fertify-fr.html")
def Fertify_fr():
return render_template('Fertify-fr.html')
#fertibot
@app.route("/FertyBot-fr.html")
def FertifyBot_fr():
return render_template('FertyBot-fr.html')
#plantguard
@app.route("/PlantGuard-fr.html")
def PlantGuard_fr():
return render_template('PlantGuard-fr.html')
#contact
@app.route("/Contact-fr.html")
def Contact_fr():
return render_template('Contact-fr.html')
if __name__ == '__main__':
app.run(debug=True)