-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
123 lines (98 loc) · 3.72 KB
/
app.py
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
from flask import Flask, request, render_template, jsonify, redirect, url_for, session
from tensorflow.keras.preprocessing import image
import numpy as np
from tensorflow.keras.models import load_model
import os
from werkzeug.utils import secure_filename
import json
import pyrebase
import os
from dotenv import load_dotenv
app = Flask(__name__)
app.secret_key = os.urandom(24)
#
firebase_config = {
"apiKey": os.getenv("FIREBASE_APIKEY"),
"authDomain": os.getenv("FIREBASE_AUTHDOMAIN"),
"projectId": os.getenv("FIREBASE_PROJECTID"),
"storageBucket": os.getenv("FIREBASE_STORAGEBUCKET"),
"messagingSenderId": os.getenv("FIREBASE_MESSAGINGSENDERID"),
"appId": os.getenv("FIREBASE_APPID"),
"measurementId":os.getenv("FIREBASE_MEASUREMENTID"),
"databaseURL":os.getenv("FIREBASE_DATABASEURL")
}
firebase = pyrebase.initialize_app(firebase_config)
auth = firebase.auth()
# Load model
model_path = 'model/model.h5'
model = load_model(model_path)
with open('remedies.json', 'r') as file:
remedies_data = json.load(file)
image_height = 150
image_width = 150
@app.route('/')
def index():
if 'user' in session:
return redirect(url_for('home'))
return render_template('login.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
try:
user = auth.sign_in_with_email_and_password(email, password)
session['user'] = user['idToken']
return redirect(url_for('home'))
except Exception as e:
error_message = str(e)
return render_template('login.html', error=error_message)
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect(url_for('login'))
@app.route('/sign-up', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
try:
auth.create_user_with_email_and_password(email, password)
return redirect(url_for('login'))
except Exception as e:
error_message = str(e)
return render_template('signup.html', error=error_message)
return render_template('signup.html')
@app.route('/predict')
def home():
if 'user' not in session:
return redirect(url_for('login'))
return render_template('index.html')
@app.route('/predict-disease', methods=['POST'])
def predict():
if 'user' not in session:
return jsonify({'error': 'Unauthorized access'}), 403
if 'file' not in request.files:
return jsonify({'error': 'No image file uploaded'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No image file selected'})
if file:
uploads_folder = 'uploads'
os.makedirs(uploads_folder, exist_ok=True)
file_path = os.path.join(uploads_folder, secure_filename(file.filename))
file.save(file_path)
img = image.load_img(file_path, target_size=(image_height, image_width))
os.remove(file_path)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0
prediction = model.predict(img_array)
class_indices = {0: 'Mild', 1: 'Mild', 2: 'Moderate', 3: 'Severe', 4: 'Proliferative DR'}
predicted_class_index = np.argmax(prediction)
predicted_class = class_indices[predicted_class_index]
remedy = remedies_data.get(predicted_class, "No remedy available")
return jsonify({'result': predicted_class, 'remedy': remedy})
if __name__ == '__main__':
app.run(debug=True, port=5000)