mirsaid5455
commited on
Commit
•
1be483f
1
Parent(s):
6235775
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Load the Fastai Learner model
|
9 |
+
learn = torch.load('model.pkl', map_location=torch.device('cpu'))
|
10 |
+
learn.eval()
|
11 |
+
|
12 |
+
# Load the X-ray detection model
|
13 |
+
learn_xray = torch.load("xraydet.pkl", map_location=torch.device('cpu'))
|
14 |
+
learn_xray.eval()
|
15 |
+
|
16 |
+
# Load the DistilBERT model and tokenizer
|
17 |
+
model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased')
|
18 |
+
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
|
19 |
+
|
20 |
+
# Define language constants
|
21 |
+
UZBEK = 'uz'
|
22 |
+
ENGLISH = 'en'
|
23 |
+
RUSSIAN = 'ru'
|
24 |
+
|
25 |
+
uz_welcome = '''- Assalomu alaykum, Men dasturchi Abdrurasulov Mirsaid tomonidan tarbiyalangan sun'iy intellektman.
|
26 |
+
- Bemorning o'pka rengin rasmiga qarab, bemorda o'pka yallig'lanishi bor yoki yo'q ekanligini aniqlab beraman.
|
27 |
+
- Model aniqligi 98% ga teng.
|
28 |
+
- Muhim: Men test rejimida ishlamoqdaman, iltimos, menga ishonib hulosa qilmang, malakalik shifokorga murojat qiling.
|
29 |
+
- Boshlash uchun, iltimos bemor o'pkasining rengin rasmini yuboring. Muhim: rasm ko'rinishida, file emas.'''
|
30 |
+
|
31 |
+
en_welcome = '''- Hello, I am a trained artificial intelligence by developer Abdurasulov Mirsaid.
|
32 |
+
- Looking at the picture of the patient's lungs, I can determine whether the patient has pneumonia or not.
|
33 |
+
- My model accuracy is 98 %.
|
34 |
+
- Important: I am working in test mode, please do not have a conclusion based on my responce, consult a qualified doctor.
|
35 |
+
- To begin, please send the XRay image of the patient's lungs. Note: as a picture, not a file. '''
|
36 |
+
|
37 |
+
rus_welcome = '''- Здравствуйте, я обученный искусственный интеллект от разработчика Мирсаидa.
|
38 |
+
- Глядя на снимок легких больного, я могу определить, есть ли у больного туберкулез или нет.
|
39 |
+
- У меня действительно высокая точность (98 %).
|
40 |
+
- Важно: я работаю в тестовом режиме, пожалуйста, не делайте вывод по моему ответу, обратитесь к квалифицированному врачу.
|
41 |
+
- Для начала отправьте рентгеновский снимок легких пациента. Примечание: как изображение, а не файл.'''
|
42 |
+
|
43 |
+
# Define welcome messages
|
44 |
+
WELCOME_MESSAGES = {
|
45 |
+
UZBEK: uz_welcome,
|
46 |
+
ENGLISH: en_welcome,
|
47 |
+
RUSSIAN: rus_welcome,
|
48 |
+
}
|
49 |
+
|
50 |
+
# Function to make predictions
|
51 |
+
def predict_pneumonia(image):
|
52 |
+
img_array = np.array(image)
|
53 |
+
img_fastai = Image.fromarray(img_array).convert('RGB')
|
54 |
+
img_fastai = img_fastai.resize((224, 224))
|
55 |
+
img_fastai = np.array(img_fastai) / 255.0
|
56 |
+
img_fastai = torch.tensor(img_fastai).permute(2, 0, 1).unsqueeze(0).float()
|
57 |
+
|
58 |
+
pred_xray = learn_xray.predict(img_fastai)[0]
|
59 |
+
|
60 |
+
if pred_xray == 1:
|
61 |
+
inputs = tokenizer(img_array, return_tensors="pt")
|
62 |
+
outputs = model(**inputs)
|
63 |
+
predicted_class_idx = torch.argmax(outputs.logits[0]).item()
|
64 |
+
if predicted_class_idx == 1:
|
65 |
+
return "Pneumonia Positive"
|
66 |
+
else:
|
67 |
+
return "Normal"
|
68 |
+
else:
|
69 |
+
return "Invalid X-ray image. Please upload a clear lung X-ray image."
|
70 |
+
|
71 |
+
# Streamlit app
|
72 |
+
def main():
|
73 |
+
st.title("Pneumonia Detection")
|
74 |
+
st.write("Upload a chest X-ray image to detect pneumonia.")
|
75 |
+
|
76 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
77 |
+
|
78 |
+
if uploaded_file is not None:
|
79 |
+
image = Image.open(uploaded_file)
|
80 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
81 |
+
if st.button('Predict'):
|
82 |
+
prediction = predict_pneumonia(image)
|
83 |
+
st.write(f"Prediction: {prediction}")
|
84 |
+
|
85 |
+
if __name__ == '__main__':
|
86 |
+
main()
|
87 |
+
|