Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,18 @@ from PIL import Image
|
|
5 |
import traceback
|
6 |
import re
|
7 |
import torch
|
|
|
|
|
|
|
8 |
import argparse
|
9 |
from transformers import AutoModel, AutoTokenizer
|
10 |
|
|
|
|
|
|
|
|
|
11 |
# Argparser
|
12 |
-
parser = argparse.ArgumentParser(description='
|
13 |
parser.add_argument('--device', type=str, default='cpu', help='cpu')
|
14 |
parser.add_argument('--dtype', type=str, default='fp32', help='fp32')
|
15 |
args = parser.parse_args()
|
@@ -50,6 +57,27 @@ top_p_slider = {'minimum': 0, 'maximum': 1, 'value': 0.8, 'step': 0.05, 'interac
|
|
50 |
top_k_slider = {'minimum': 0, 'maximum': 200, 'value': 100, 'step': 1, 'interactive': True, 'label': 'Top K'}
|
51 |
temperature_slider = {'minimum': 0, 'maximum': 2, 'value': 0.7, 'step': 0.05, 'interactive': True, 'label': 'Temperature'}
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def create_component(params, comp='Slider'):
|
54 |
if comp == 'Slider':
|
55 |
return gr.Slider(**params)
|
@@ -116,26 +144,33 @@ def clear(chat_bot, app_session):
|
|
116 |
chat_bot.clear()
|
117 |
return chat_bot
|
118 |
|
119 |
-
with gr.Blocks() as
|
120 |
gr.Markdown("<h1 style='text-align: center;'>Medical Assistant</h1>")
|
121 |
|
122 |
-
with gr.
|
123 |
-
with gr.
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
)
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# Launch
|
141 |
-
|
|
|
5 |
import traceback
|
6 |
import re
|
7 |
import torch
|
8 |
+
import numpy as np
|
9 |
+
import tensorflow as tf
|
10 |
+
from tensorflow.keras.models import load_model # type: ignore
|
11 |
import argparse
|
12 |
from transformers import AutoModel, AutoTokenizer
|
13 |
|
14 |
+
# Configuration for image classification model
|
15 |
+
class_names = ['Calculus', 'Dental Caries', 'Gingivitis', 'Hypodontia', 'Tooth Discoloration']
|
16 |
+
cnn_model = load_model('new_model2.h5')
|
17 |
+
|
18 |
# Argparser
|
19 |
+
parser = argparse.ArgumentParser(description='app')
|
20 |
parser.add_argument('--device', type=str, default='cpu', help='cpu')
|
21 |
parser.add_argument('--dtype', type=str, default='fp32', help='fp32')
|
22 |
args = parser.parse_args()
|
|
|
57 |
top_k_slider = {'minimum': 0, 'maximum': 200, 'value': 100, 'step': 1, 'interactive': True, 'label': 'Top K'}
|
58 |
temperature_slider = {'minimum': 0, 'maximum': 2, 'value': 0.7, 'step': 0.05, 'interactive': True, 'label': 'Temperature'}
|
59 |
|
60 |
+
def classify_images(image):
|
61 |
+
# Check if the image is None
|
62 |
+
if image is None:
|
63 |
+
return "No image uploaded. Please upload a dental image."
|
64 |
+
|
65 |
+
# Resize and preprocess the image
|
66 |
+
try:
|
67 |
+
input_image = tf.image.resize(image, (180, 180)) # Resize to expected input size
|
68 |
+
input_image_array = tf.keras.utils.img_to_array(input_image)
|
69 |
+
input_image_exp_dim = tf.expand_dims(input_image_array, axis=0)
|
70 |
+
|
71 |
+
# Make predictions
|
72 |
+
predictions = cnn_model.predict(input_image_exp_dim)
|
73 |
+
result = tf.nn.softmax(predictions[0])
|
74 |
+
|
75 |
+
# Prepare the outcome message
|
76 |
+
outcome = f'The image belongs to {class_names[np.argmax(result)]} with a score of {np.max(result) * 100:.2f}%'
|
77 |
+
return outcome
|
78 |
+
except Exception as e:
|
79 |
+
return f"Error processing the image: {str(e)}"
|
80 |
+
|
81 |
def create_component(params, comp='Slider'):
|
82 |
if comp == 'Slider':
|
83 |
return gr.Slider(**params)
|
|
|
144 |
chat_bot.clear()
|
145 |
return chat_bot
|
146 |
|
147 |
+
with gr.Blocks() as app:
|
148 |
gr.Markdown("<h1 style='text-align: center;'>Medical Assistant</h1>")
|
149 |
|
150 |
+
with gr.Tab("Image Classification"):
|
151 |
+
with gr.Row():
|
152 |
+
image_input = gr.Image(type='numpy', label="Upload Dental Image")
|
153 |
+
classification_output = gr.Label(num_top_classes=5, label="Classification Results")
|
154 |
+
image_input.change(fn=classify_images, inputs=image_input, outputs=classification_output)
|
155 |
+
|
156 |
+
with gr.Tab("Medical Chatbot"):
|
157 |
+
with gr.Row():
|
158 |
+
with gr.Column(scale=2, min_width=300):
|
159 |
+
app_session = gr.State({'sts': None, 'ctx': None, 'img': None})
|
160 |
+
bt_pic = gr.Image(label="Upload an image to start")
|
161 |
+
txt_message = gr.Textbox(label="Ask your question...")
|
162 |
+
|
163 |
+
with gr.Column(scale=2, min_width=300):
|
164 |
+
chat_bot = gr.Chatbot(label=f"Chatbot")
|
165 |
+
clear_button = gr.Button(value='Clear')
|
166 |
+
txt_message.submit(
|
167 |
+
respond,
|
168 |
+
[txt_message, chat_bot, app_session],
|
169 |
+
[txt_message, chat_bot, app_session]
|
170 |
+
)
|
171 |
+
|
172 |
+
bt_pic.upload(lambda: None, None, chat_bot, queue=False).then(upload_img, inputs=[bt_pic, chat_bot, app_session], outputs=[chat_bot, app_session])
|
173 |
+
clear_button.click(clear, [chat_bot, app_session], chat_bot)
|
174 |
|
175 |
# Launch
|
176 |
+
app.launch(share=True, debug=True, show_api=False)
|