-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
164 lines (128 loc) · 6.21 KB
/
app.py
File metadata and controls
164 lines (128 loc) · 6.21 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
from flask import Flask, render_template, url_for, request, jsonify, make_response, flash, redirect
from sklearn.externals import joblib
import librosa
import requests
import uuid
import json
import cough as CP
import text_api
import breath as bm
import os
from ip2geotools.databases.noncommercial import DbIpCity
from urllib.request import urlopen
from pymongo import MongoClient
import pandas as pd
import numpy as np
from werkzeug.utils import secure_filename
application = Flask(__name__)
client = MongoClient("localhost", 27017)
db = client.SentencesDatabase
users = db["Users"]
# UPLOAD_FOLDER = './uploads'
# ALLOWED_EXTENSIONS = {'mp3', 'wav'}
# application.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#def allowed_file(filename):
# return '.' in filename and \
# filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@application.route('/', methods=['GET', 'POST'])
def index():
dicti = data()
return render_template('app.html',predic=dicti)
@application.route('/data', methods=['GET', 'POST'])
def data():
if request.method == 'POST':
try:
age = request.form.get('age')
gender = request.form.get('gender')
smoker = request.form.get('smoker')
symptoms = request.form.getlist('reported_symptoms')
medical_history = request.form.getlist('medical_history')
symptoms = ",".join(symptoms) + ","
medical_history = ",".join(medical_history) + ","
# hasham = request.files
hasham = request.files.get("cough_data")
breath = request.files.get("breath_data")
location = request.form.get("user_locations")
# Textual model
response = {"age": [int(age)], "gender": [gender],
"smoker": [smoker], "patient_reported_symptoms": [symptoms],
"medical_history": [medical_history]
}
if location == "furqan":
# location = f"{loc_response.country}, {loc_response.region}, {loc_response.city}"
location = "Empty"
df1 = pd.DataFrame(response)
prediction = round(text_api.predict(df1, "./model81.pkl"), 2)
# pp = os.getcwd()
hash = uuid.uuid4().hex
cough_path = "./uploads/cough/hasham"
breath_path = "./uploads/breath/breath"
with open(cough_path + hash + ".wav", 'wb') as ft:
ft.write(hasham.read())
with open(breath_path + hash + ".wav", 'wb') as ft:
ft.write(breath.read())
# return symptoms
# return jsonify(hasham.read())
# check if the post request has the file part
# if 'file' not in request.files:
# flash('No file part')
# return redirect(request.url)
# file = request.files['file']
# # if user does not select file, browser also
# # submit an empty part without filename
# if file.filename == '':
# flash('No selected file')
# return redirect(request.url)
# if file and allowed_file(file.filename):
# filename = secure_filename(file.filename)
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
####### Predictions
cough_result = CP.predict(cough_path+ hash + ".wav", './cough_model.pkl')
breath_result = bm.predict(breath_path+ hash + ".wav", './breath_model.pkl')
cough_result = round(cough_result, 2)
breath_result = round(breath_result, 2)
####### DB insertion
users.insert_one({
"age": age,
"gender" : gender,
"smoker" : smoker,
"symptoms": symptoms,
"medical_history": medical_history,
"cough_path": cough_path+ hash + ".wav",
"breath_path": breath_path+ hash + ".wav",
"statistical_result": prediction,
"cough_results": cough_result,
"breath_results": breath_result,
"location": location
})
msg = ""
######## Conditions
if prediction == 0 and cough_result == 0 and breath_result == 0:
msg = "Hooray! You are safe. You are Covid free!!!"
elif prediction == 0 and cough_result > 0 and breath_result > 0:
msg = "We are worried! You need to visit doctor.!!!"
elif prediction > 0 and cough_result > 0 and breath_result > 0:
msg = "Your health condition seems Serious. You need to visit doctor!!!"
elif prediction > 0 and cough_result == 0 and breath_result == 0:
msg = "Hooray! You are safe. You are Covid free, Just take rest and eat healthy..!!!"
elif prediction > 0 and cough_result == 0 and breath_result > 0:
msg = "There are very mild Symptoms, Don't worry, we suggest you to Isolate yourself and eat healthy Food!!!"
elif prediction > 0 and cough_result > 0 and breath_result == 0:
msg = "There are mild Symptoms of Corona, we suggest you to Isolate yourself and eat healthy Food!!!"
elif prediction == 0 and cough_result > 0 and breath_result == 0:
msg = "There are very mild Symptoms of Corona, Don't worry, we suggest you to Isolate yourself and eat healthy Food!!!"
elif prediction == 0 and cough_result == 0 and breath_result > 0:
msg = "There are extremely low symptoms, Don't worry, Stay at Home and eat healthy Food!!!"
############
return jsonify({
"prediction": round((prediction * 100), 2),
"cough_result": round((cough_result * 100), 2),
"breath_result": round((breath_result * 100), 2),
"msg": msg
})
except:
return "Please check if the values are entered correctly"
# if __name__ == "__main__":
# application.run(debug=True)
# if __name__ == '__main__':
# application.run(host='0.0.0.0', port=80)