[b018ba]: / docpat / views.py

Download this file

215 lines (182 with data), 9.3 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
from django.shortcuts import render
import pickle
import os
from django.core.files.storage import FileSystemStorage
import numpy as np
import tensorflow
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import os
from django.conf import settings
from xc import predict_image # Import the function from c.py
# Path to your single image
def load_model(file_path):
if not os.path.exists(file_path):
raise FileNotFoundError(f"The model file at {file_path} does not exist.")
with open(file_path, 'rb') as file:
model = pickle.load(file)
return model
def homepage(request):
return render(request,"main.html")
def breast(request):
return render(request,"breast.html")
def faq(request):
return render(request,"faqs.html")
def heart(request):
return render(request,"heart.html")
def liver(request):
return render(request,"liver.html")
def diabetes(request):
return render(request,"diabetes.html")
def predictb(request):
if request.method == 'POST':
try:
model_path = r'breast_cancer_svm_model.pkl'
breast_model = load_model(model_path)
texture_mean = float(request.POST.get('texture_mean'))
smoothness_mean = float(request.POST.get('smoothness_mean'))
compactness_mean = float(request.POST.get('compactness_mean'))
concave_points_mean = float(request.POST.get('concave_points_mean'))
symmetry_mean = float(request.POST.get('symmetry_mean'))
fractal_dimension_mean = float(request.POST.get('fractal_dimension_mean'))
texture_se = float(request.POST.get('texture_se'))
area_se = float(request.POST.get('area_se'))
smoothness_se = float(request.POST.get('smoothness_se'))
compactness_se = float(request.POST.get('compactness_se'))
concavity_se = float(request.POST.get('concavity_se'))
concave_points_se = float(request.POST.get('concave_points_se'))
symmetry_se = float(request.POST.get('symmetry_se'))
fractal_dimension_se =float(request.POST.get('fractal_dimension_se'))
texture_worst = float(request.POST.get('texture_worst'))
area_worst = float(request.POST.get('area_worst'))
smoothness_worst = float(request.POST.get('smoothness_worst'))
compactness_worst = float(request.POST.get('compactness_worst'))
concavity_worst = float(request.POST.get('concavity_worst'))
concave_points_worst = float(request.POST.get('concave_points_worst'))
symmetry_worst = float(request.POST.get('symmetry_worst'))
fractal_dimension_worst = float(request.POST.get('fractal_dimension_worst'))
data = [ texture_mean, smoothness_mean, compactness_mean,
concave_points_mean, symmetry_mean, fractal_dimension_mean, texture_se,
area_se, smoothness_se, compactness_se, concavity_se, concave_points_se, symmetry_se,
fractal_dimension_se, texture_worst, area_worst, smoothness_worst,
compactness_worst, concavity_worst, concave_points_worst, symmetry_worst, fractal_dimension_worst]
prediction_result = breast_model.predict([data])
context = {
'prediction_result': prediction_result[0]
}
return render(request, 'predict.html', context)
except Exception as e:
return render(request, 'error.html', {'error': str(e)})
def predictd(request):
if request.method == 'POST':
try:
model_path = r'diabetes_model.pkl'
diabetes_model = load_model(model_path)
Pregnancies = float(request.POST.get('Pregnancies'))
Glucose =float(request.POST.get('Glucose'))
BloodPressure = float(request.POST.get('BloodPressure'))
SkinThickness = float(request.POST.get('SkinThickness'))
Insulin = float(request.POST.get('Insulin'))
BMI = float(request.POST.get('BMI'))
DiabetesPedigreeFunction = float(request.POST.get('DiabetesPedigreeFunction'))
Age = float(request.POST.get('Age'))
inputs = [[Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin,
BMI, DiabetesPedigreeFunction, Age
]]
prediction_result = diabetes_model.predict(inputs)
context = {
'prediction_result': prediction_result[0]
}
return render(request, 'predict.html', context)
except Exception as e:
return render(request, 'error.html', {'error': str(e)})
def predictl(request):
if request.method == 'POST':
try:
model_path = r'liver_prediction.pkl'
liver_model = load_model(model_path)
age = float(request.POST.get('Age'))
gender = float(request.POST.get('Gender'))
total_bilirubin = float(request.POST.get('Total_Bilirubin'))
direct_bilirubin = float(request.POST.get('Direct_Bilirubin'))
alkaline_phosphotase = float(request.POST.get('Alkaline_Phosphotase'))
alamine_aminotransferase = float(request.POST.get('Alamine_Aminotransferase'))
aspartate_aminotransferase = float(request.POST.get('Aspartate_Aminotransferase'))
total_proteins = float(request.POST.get('Total_Protiens'))
albumin = float(request.POST.get('Albumin'))
albumin_and_globulin_ratio = float(request.POST.get('Albumin_and_Globulin_Ratio'))
inputs = [[age, gender, total_bilirubin, direct_bilirubin, alkaline_phosphotase,
alamine_aminotransferase, aspartate_aminotransferase, total_proteins,
albumin, albumin_and_globulin_ratio]]
prediction_result =liver_model.predict(inputs)
context = {
'prediction_result': prediction_result[0]
}
return render(request, 'predict.html', context)
except Exception as e:
return render(request, 'error.html', {'error': str(e)})
def predicth(request):
if request.method == 'POST':
try:
model_path = r'heart_disease_model.pkl'
heart_model =(load_model(model_path))
age = float(request.POST.get('age'))
sex = float(request.POST.get('sex'))
cp = float(request.POST.get('cp'))
trestbps = float(request.POST.get('trestbps'))
chol = float(request.POST.get('chol'))
fbs = float(request.POST.get('fbs'))
restecg = float(request.POST.get('restecg'))
thalach = float(request.POST.get('thalach'))
exang = float(request.POST.get('exang'))
oldpeak = float(request.POST.get('oldpeak'))
slope = float(request.POST.get('slope'))
ca = float(request.POST.get('ca'))
thal = float(request.POST.get('thal'))
inputs = [[age, sex,cp,trestbps, chol, fbs, restecg,
thalach, exang, oldpeak,
slope, ca,thal]]
prediction_result = heart_model.predict(inputs)
context = {
'prediction_result': prediction_result[0]
}
return render(request, 'predict.html', context)
except Exception as e:
return render(request, 'error.html', {'error': str(e)})
def gi(request):
return render(request, 'GI_diseases.html')
# View to handle file upload and prediction
# views.py
def predictg(request):
if request.method == 'POST' and request.FILES.get('uploadedImage'):
# Handle the uploaded file
uploaded_file = request.FILES['uploadedImage']
file_name = uploaded_file.name
# Save the uploaded file temporarily
fs = FileSystemStorage()
file_path = fs.save(file_name, uploaded_file)
full_file_path = fs.path(file_path)
# Debugging: Ensure the file path is correct
print(f"File uploaded to: {full_file_path}")
# Call the predict_image function from c.py to process the image
predicted_class = predict_image(full_file_path)
if predicted_class is not None:
print(f"Predicted class: {predicted_class}") # Debugging: print the predicted class
return render(request, 'p.html', {
'predicted_class': predicted_class # Only send predicted class
})
else:
print("Prediction failed") # Debugging: print if prediction failed
return render(request, 'error.html', {'error': 'Failed to process the image or make predictions.'})
else:
return render(request, 'p.html')
def GI_Diseases_info(request):
return render(request,"gi.html")
def diabetes_info(request):
return render(request,"diabetes_info.html")
def breast_cancer_info(request):
return render(request,'breast_cancer_info.html')
def heart_disease_info(request):
return render(request,'heart_disease_info.html')
def liver_disease_info(request):
return render(request,'liver_disease_info.html')