File size: 5,582 Bytes
8413e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
import gradio as gr
import numpy as np
import cv2
import matplotlib.pyplot as plt

import tensorflow as tf
import tensorflow.keras.backend as K
from keras.preprocessing import image

from ResUNet import *

from eff import *
from vit import *

# Define the image transformation
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Resize((224, 224)),
])

examples1 = [
    f"examples/Eff_ViT/Classification_{i}.jpg" for i in range(0, 4)
]

def classification(image):
    input_tensor = transform(image).unsqueeze(0).to(CFG.DEVICE)  # Add batch dimension

    input_batch = input_tensor

    # Perform inference
    with torch.no_grad():
        output1 = efficientnet_model(input_batch).to(CFG.DEVICE)
        output2 = efficientnet_model(input_batch).to(CFG.DEVICE)
        output3 = vit_model(input_batch).to(CFG.DEVICE)

    # You can now use the 'output' tensor as needed (e.g., get predictions)
    # print(output)
    res1 = torch.softmax(output1, dim=1)
    res2 = torch.softmax(output2, dim=1)
    res3 = torch.softmax(output3, dim=1)

    probs1 = {class_names[i]: float(res1[0][i]) for i in range(len(class_names))}
    probs2 = {class_names[i]: float(res2[0][i]) for i in range(len(class_names))}
    probs3 = {class_names[i]: float(res3[0][i]) for i in range(len(class_names))}

    return probs1, probs2, probs3
    

classify = gr.Interface(
    fn=classification,
    inputs=[
        gr.Image(label="Image"),
        # gr.Radio(["EfficientNetB3", "EfficientNetV2", "ViT"], value="ViT")
    ],
    outputs=[
        gr.Label(num_top_classes = 3, label = "EfficientNet-B3"),
        gr.Label(num_top_classes = 3, label = "EfficientNet-V2"),
        gr.Label(num_top_classes = 3, label = "ViT"),
    ],
    examples=examples1,
    cache_examples=True
)

# ---------------------------------------------------------

seg_model = load_model()
seg_model.load_weights("ResUNet-segModel-weights.hdf5")


examples2 = [
    f"examples/ResUNet/{i}.jpg" for i in range(5)
]

def detection(img):
  org_img = img

  img = img *1./255.

  #reshaping
  img = cv2.resize(img, (256,256))

  # converting img into array
  img = np.array(img, dtype=np.float64)

  #reshaping the image from 256,256,3 to 1,256,256,3
  img = np.reshape(img, (1,256,256,3))


  #Creating a empty array of shape 1,256,256,1
  X = np.empty((1,256,256,3))

  # standardising the image
  img -= img.mean()
  img /= img.std()

  #converting the shape of image from 256,256,3 to 1,256,256,3
  X[0,] = img

  #make prediction of mask
  predict = seg_model.predict(X)


  pred = np.array(predict[0]).squeeze().round()


  img_ = cv2.resize(org_img, (256,256))
  img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)
  img_[pred==1] = (0,255,150)

  plt.imshow(img_)
  plt.axis("off")
  image_path = "plot.png"
  plt.savefig(image_path)

  return gr.update(value=image_path, visible=True)


detect = gr.Interface(
    fn=detection,
    inputs=[
        gr.Image(label="Image")
    ],
    outputs=[
        gr.Image(label="Output")
    ],
    examples=examples2,
    cache_examples=True
)

# ##########################################

# def data_viewer(label="Pituitary", count=10):
#   results = []

#   if(label == "Segmentation"):
#     for i in range((count//2)+1):
#       results.append(f"Images/{label}/original_image_{i}.png")
#       results.append(f"Images/{label}/image_with_mask_{i}.png")

#   else:

#     for i in range(count):
#       results.append(f"Images/{label}/{i}.jpg")

#   return results


# view_data = gr.Interface(
#     fn = data_viewer,
#     inputs = [
#         gr.Dropdown(
#             ["Glioma", "Meningioma", "Pituitary", "Segmentation"], label="Category"
#         ),
#         gr.Slider(0, 12, value=4, step=2)
#     ],
#     outputs = [
#         gr.Gallery(columns=2),
#     ]
# )

# ##########################

from huggingface_hub import InferenceClient

client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

def format_prompt(message, history):
  prompt = "<s>"
  for user_prompt, bot_response in history:
    prompt += f"[INST] {user_prompt} [/INST]"
    prompt += f" {bot_response}</s> "
  prompt += f"[INST] {message} [/INST]"
  return prompt

def generate(
    prompt, history, temperature=0.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
):
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=42,
    )

    formatted_prompt = format_prompt(prompt, history)

    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    output = ""

    for response in stream:
        output += response.token.text
        yield output
    return output

    
mychatbot = gr.Chatbot(
    avatar_images=["Chatbot/user.png", "Chatbot/botm.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)

chatbot = gr.ChatInterface(
    fn=generate, 
    chatbot=mychatbot,
    examples=[
        "What is Brain Tumor and its types?",
        "What is a tumor's grade? What does this mean?",
        "What are some of the treatment options for Brain Tumor?",
        "What causes brain tumors?",
        "If I have a brain tumor, can I pass it on to my children?"
    ],
)


demo = gr.TabbedInterface([classify, detect, chatbot], ["Classification", "Detection", "ChatBot"])

demo.launch()