Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,91 @@
|
|
1 |
-
import os, glob
|
2 |
-
from flask import Flask, request, render_template
|
3 |
-
from transformers import pipeline
|
4 |
-
from ultralytics import YOLO
|
5 |
-
|
6 |
-
app = Flask(__name__)
|
7 |
-
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
8 |
-
app.config['MARKED_FOLDER'] = 'static/marked'
|
9 |
-
PREDICT_FOLDER = os.path.join(app.config['MARKED_FOLDER'], 'predict')
|
10 |
-
|
11 |
-
# สร้างโฟลเดอร์หากยังไม่มี
|
12 |
-
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
13 |
-
os.makedirs(PREDICT_FOLDER, exist_ok=True)
|
14 |
-
|
15 |
-
# โหลดโมเดล
|
16 |
-
pipe = pipeline("image-classification", model="JorgeGIT/Finetuned-Leukemia-cell")
|
17 |
-
yolo_model = YOLO("runs/detect/leukemia-yolo-model/weights/best.pt")
|
18 |
-
|
19 |
-
# 🎯 ฟังก์ชันรวมความเสี่ยง
|
20 |
-
def calculate_combined_risk(blast_risk, huggingface_score, age, sex):
|
21 |
-
score = 0.6 * blast_risk + 0.4 * huggingface_score
|
22 |
-
if age and int(age) > 60:
|
23 |
-
score += 5
|
24 |
-
if sex == 'male':
|
25 |
-
score += 2
|
26 |
-
return min(round(score, 1), 100)
|
27 |
-
|
28 |
-
@app.route('/', methods=['GET'])
|
29 |
-
def home():
|
30 |
-
return render_template('index.html')
|
31 |
-
|
32 |
-
@app.route('/upload', methods=['GET', 'POST'])
|
33 |
-
def upload_file():
|
34 |
-
if request.method == 'POST':
|
35 |
-
file = request.files.get('file')
|
36 |
-
age = request.form.get('age')
|
37 |
-
sex = request.form.get('sex')
|
38 |
-
|
39 |
-
if not file or file.filename == '' or not age or not sex:
|
40 |
-
return "กรุณากรอกข้อมูลให้ครบ"
|
41 |
-
|
42 |
-
filename = file.filename
|
43 |
-
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
44 |
-
file.save(filepath)
|
45 |
-
|
46 |
-
results = yolo_model.predict(
|
47 |
-
source=filepath,
|
48 |
-
save=True,
|
49 |
-
project=app.config['MARKED_FOLDER'],
|
50 |
-
name='predict',
|
51 |
-
exist_ok=True
|
52 |
-
)
|
53 |
-
|
54 |
-
list_of_files = glob.glob(os.path.join(PREDICT_FOLDER, '*.jpg'))
|
55 |
-
latest_file = max(list_of_files, key=os.path.getctime)
|
56 |
-
marked_filename = os.path.relpath(latest_file, 'static').replace('\\', '/')
|
57 |
-
|
58 |
-
total_cells = len(results[0].boxes)
|
59 |
-
abnormal_cells = 0
|
60 |
-
for box in results[0].boxes:
|
61 |
-
cls_id = int(box.cls[0].item())
|
62 |
-
class_name = results[0].names[cls_id]
|
63 |
-
if 'blast' in class_name.lower():
|
64 |
-
abnormal_cells += 1
|
65 |
-
blast_risk = round((abnormal_cells / total_cells) * 100, 1) if total_cells else 0.0
|
66 |
-
|
67 |
-
result = pipe(filepath)
|
68 |
-
lla_result = [
|
69 |
-
{'label': 'โอกาสการเกิดโรค Leukemia = ', 'score': round(res['score'] * 1000, 1)}
|
70 |
-
for res in result if res['label'].lower() == 'lla'
|
71 |
-
]
|
72 |
-
huggingface_score = lla_result[0]['score'] if lla_result else 0.0
|
73 |
-
|
74 |
-
combined_risk = calculate_combined_risk(blast_risk, huggingface_score, age, sex)
|
75 |
-
|
76 |
-
return render_template('result.html',
|
77 |
-
filename=filename,
|
78 |
-
marked_filename=marked_filename,
|
79 |
-
total_cells=total_cells,
|
80 |
-
abnormal_cells=abnormal_cells,
|
81 |
-
blast_risk=blast_risk,
|
82 |
-
huggingface_score=huggingface_score,
|
83 |
-
combined_risk=combined_risk,
|
84 |
-
age=age,
|
85 |
-
sex=sex
|
86 |
-
)
|
87 |
-
|
88 |
-
return render_template('upload.html')
|
89 |
-
if __name__ == '__main__':
|
90 |
-
app.run(
|
|
|
|
1 |
+
import os, glob
|
2 |
+
from flask import Flask, request, render_template
|
3 |
+
from transformers import pipeline
|
4 |
+
from ultralytics import YOLO
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
8 |
+
app.config['MARKED_FOLDER'] = 'static/marked'
|
9 |
+
PREDICT_FOLDER = os.path.join(app.config['MARKED_FOLDER'], 'predict')
|
10 |
+
|
11 |
+
# สร้างโฟลเดอร์หากยังไม่มี
|
12 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
13 |
+
os.makedirs(PREDICT_FOLDER, exist_ok=True)
|
14 |
+
|
15 |
+
# โหลดโมเดล
|
16 |
+
pipe = pipeline("image-classification", model="JorgeGIT/Finetuned-Leukemia-cell")
|
17 |
+
yolo_model = YOLO("runs/detect/leukemia-yolo-model/weights/best.pt")
|
18 |
+
|
19 |
+
# 🎯 ฟังก์ชันรวมความเสี่ยง
|
20 |
+
def calculate_combined_risk(blast_risk, huggingface_score, age, sex):
|
21 |
+
score = 0.6 * blast_risk + 0.4 * huggingface_score
|
22 |
+
if age and int(age) > 60:
|
23 |
+
score += 5
|
24 |
+
if sex == 'male':
|
25 |
+
score += 2
|
26 |
+
return min(round(score, 1), 100)
|
27 |
+
|
28 |
+
@app.route('/', methods=['GET'])
|
29 |
+
def home():
|
30 |
+
return render_template('index.html')
|
31 |
+
|
32 |
+
@app.route('/upload', methods=['GET', 'POST'])
|
33 |
+
def upload_file():
|
34 |
+
if request.method == 'POST':
|
35 |
+
file = request.files.get('file')
|
36 |
+
age = request.form.get('age')
|
37 |
+
sex = request.form.get('sex')
|
38 |
+
|
39 |
+
if not file or file.filename == '' or not age or not sex:
|
40 |
+
return "กรุณากรอกข้อมูลให้ครบ"
|
41 |
+
|
42 |
+
filename = file.filename
|
43 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
44 |
+
file.save(filepath)
|
45 |
+
|
46 |
+
results = yolo_model.predict(
|
47 |
+
source=filepath,
|
48 |
+
save=True,
|
49 |
+
project=app.config['MARKED_FOLDER'],
|
50 |
+
name='predict',
|
51 |
+
exist_ok=True
|
52 |
+
)
|
53 |
+
|
54 |
+
list_of_files = glob.glob(os.path.join(PREDICT_FOLDER, '*.jpg'))
|
55 |
+
latest_file = max(list_of_files, key=os.path.getctime)
|
56 |
+
marked_filename = os.path.relpath(latest_file, 'static').replace('\\', '/')
|
57 |
+
|
58 |
+
total_cells = len(results[0].boxes)
|
59 |
+
abnormal_cells = 0
|
60 |
+
for box in results[0].boxes:
|
61 |
+
cls_id = int(box.cls[0].item())
|
62 |
+
class_name = results[0].names[cls_id]
|
63 |
+
if 'blast' in class_name.lower():
|
64 |
+
abnormal_cells += 1
|
65 |
+
blast_risk = round((abnormal_cells / total_cells) * 100, 1) if total_cells else 0.0
|
66 |
+
|
67 |
+
result = pipe(filepath)
|
68 |
+
lla_result = [
|
69 |
+
{'label': 'โอกาสการเกิดโรค Leukemia = ', 'score': round(res['score'] * 1000, 1)}
|
70 |
+
for res in result if res['label'].lower() == 'lla'
|
71 |
+
]
|
72 |
+
huggingface_score = lla_result[0]['score'] if lla_result else 0.0
|
73 |
+
|
74 |
+
combined_risk = calculate_combined_risk(blast_risk, huggingface_score, age, sex)
|
75 |
+
|
76 |
+
return render_template('result.html',
|
77 |
+
filename=filename,
|
78 |
+
marked_filename=marked_filename,
|
79 |
+
total_cells=total_cells,
|
80 |
+
abnormal_cells=abnormal_cells,
|
81 |
+
blast_risk=blast_risk,
|
82 |
+
huggingface_score=huggingface_score,
|
83 |
+
combined_risk=combined_risk,
|
84 |
+
age=age,
|
85 |
+
sex=sex
|
86 |
+
)
|
87 |
+
|
88 |
+
return render_template('upload.html')
|
89 |
+
if __name__ == '__main__':
|
90 |
+
app.run(host='0.0.0.0', port=7860)
|
91 |
+
|