Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,107 @@
|
|
1 |
-
import
|
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 |
-
st.markdown(f"""
|
32 |
-
<style>
|
33 |
-
body {{ background-color: {themes[chat_theme]}; color: black; }}
|
34 |
-
.stButton>button {{ background-color: #4CAF50; color: white; }}
|
35 |
-
.chat-bubble {{ background-color: #f1f1f1; border-radius: 10px; padding: 10px; }}
|
36 |
-
</style>
|
37 |
-
""", unsafe_allow_html=True)
|
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 |
-
st.markdown("### π¨οΈ Chat History")
|
72 |
-
for question, answer in st.session_state.conversation_history:
|
73 |
-
st.write(f"<div class='chat-bubble'><b>{question}</b></div>", unsafe_allow_html=True)
|
74 |
-
st.write(f"<div class='chat-bubble'>{answer}</div>", unsafe_allow_html=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow.keras.models import load_model
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load model
|
7 |
+
model = load_model('xray_image_classifier_model.keras')
|
8 |
|
9 |
+
# Define solutions
|
10 |
+
solutions = {
|
11 |
+
"Pneumonia": "Consult a doctor immediately. Follow prescribed antibiotics if given, rest well, and stay hydrated.",
|
12 |
+
"Normal": "Your X-ray appears normal. However, if you experience symptoms, consult a doctor for further evaluation."
|
13 |
+
}
|
14 |
|
15 |
+
# Prediction function
|
16 |
+
def predict(image):
|
17 |
+
img = image.resize((150, 150))
|
18 |
+
img_array = np.array(img) / 255.0
|
19 |
+
img_array = np.expand_dims(img_array, axis=0)
|
20 |
+
|
21 |
+
prediction = model.predict(img_array)
|
22 |
+
predicted_class = "Pneumonia" if prediction > 0.5 else "Normal"
|
23 |
+
|
24 |
+
# Get the corresponding solution
|
25 |
+
solution = solutions.get(predicted_class, "No specific advice available.")
|
26 |
+
|
27 |
+
return predicted_class, solution
|
28 |
|
29 |
+
# CSS Styling
|
30 |
+
css = """
|
31 |
+
.gradio-container {
|
32 |
+
background-color: #f5f5f5;
|
33 |
+
font-family: Arial, sans-serif;
|
34 |
+
}
|
35 |
+
|
36 |
+
.gr-button {
|
37 |
+
background-color:#007bff;
|
38 |
+
color: white;
|
39 |
+
border: none;
|
40 |
+
border-radius: 5px;
|
41 |
+
font-size: 16px;
|
42 |
+
padding: 10px 20px;
|
43 |
+
cursor: pointer;
|
44 |
+
transition: background-color 0.3s ease;
|
45 |
+
}
|
46 |
|
47 |
+
.gr-button:hover {
|
48 |
+
background-color: #0056b3;
|
49 |
+
}
|
50 |
|
51 |
+
.gr-textbox, .gr-image {
|
52 |
+
border: 2px dashed #007bff;
|
53 |
+
padding: 20px;
|
54 |
+
border-radius: 10px;
|
55 |
+
background-color: #ffffff;
|
56 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
.gr-box-text {
|
59 |
+
color: #007bff;
|
60 |
+
font-size: 22px;
|
61 |
+
font-weight: bold;
|
62 |
+
text-align: center;
|
63 |
+
}
|
64 |
|
65 |
+
h1 {
|
66 |
+
font-size: 36px;
|
67 |
+
color: #007bff;
|
68 |
+
text-align: center;
|
69 |
+
}
|
70 |
|
71 |
+
p {
|
72 |
+
font-size: 20px;
|
73 |
+
color: #333;
|
74 |
+
text-align: center;
|
75 |
+
}
|
76 |
+
"""
|
77 |
+
|
78 |
+
# Description
|
79 |
+
description = """
|
80 |
+
**Automated Pneumonia Detection via Chest X-ray Classification**
|
81 |
+
|
82 |
+
This model leverages deep learning techniques to classify chest X-ray images as either 'Pneumonia' or 'Normal.' By utilizing the InceptionV3 architecture for transfer learning, combined with data preprocessing and augmentation, the model aims to deliver powerful performance in medical image analysis. It enhances the automation of diagnostic processes, aiding in the detection of pneumonia with high accuracy.
|
83 |
|
84 |
+
**Technologies Employed:**
|
85 |
+
- TensorFlow & Keras for model development
|
86 |
+
- InceptionV3 for transfer learning
|
87 |
+
- Numpy, Pandas, and Matplotlib for data handling and visualization
|
88 |
+
- Flask and Gradio for deployment and user interaction
|
89 |
+
"""
|
90 |
|
91 |
+
# Gradio UI
|
92 |
+
with gr.Blocks(css=css) as interface:
|
93 |
+
gr.Markdown("<h1>Automated Pneumonia Detection via Chest X-ray Classification</h1>")
|
94 |
+
gr.Markdown("<p>Submit a chest X-ray image below.</p>")
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
image_input = gr.Image(label="Drop Image Here", type="pil", elem_classes=["gr-image", "gr-box-text"])
|
98 |
+
output_prediction = gr.Textbox(label="Model Analysis Output", elem_classes=["gr-textbox", "gr-box-text"])
|
99 |
+
output_solution = gr.Textbox(label="Recommended Solution", elem_classes=["gr-textbox", "gr-box-text"])
|
100 |
+
|
101 |
+
submit_btn = gr.Button("Initiate Diagnostic Analysis", elem_classes=["gr-button"])
|
102 |
+
submit_btn.click(fn=predict, inputs=image_input, outputs=[output_prediction, output_solution])
|
103 |
+
|
104 |
+
gr.Markdown(description)
|
105 |
|
106 |
+
# Launch the app
|
107 |
+
interface.launch()
|
|
|
|
|
|
|
|