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
|
| 1 |
import requests
|
| 2 |
-
import numpy as np
|
| 3 |
-
import tensorflow as tf
|
| 4 |
-
import tensorflow_hub as hub
|
| 5 |
import gradio as gr
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
# Load models
|
| 9 |
-
#model_initial = keras.models.load_model(
|
| 10 |
-
# "models/initial_model.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 11 |
-
#)
|
| 12 |
-
#model_tumor = keras.models.load_model(
|
| 13 |
-
# "models/model_tumor.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 14 |
-
#)
|
| 15 |
-
#model_stroke = keras.models.load_model(
|
| 16 |
-
# "models/model_stroke.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 17 |
-
#)
|
| 18 |
-
#model_alzheimer = keras.models.load_model(
|
| 19 |
-
# "models/model_alzheimer.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 20 |
|
| 21 |
# API key and user ID for on-demand
|
| 22 |
api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
|
|
@@ -52,82 +196,25 @@ def submit_query(session_id, query):
|
|
| 52 |
response = requests.post(submit_query_url, headers=submit_query_headers, json=submit_query_body)
|
| 53 |
return response.json()
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
self.sub_models = {
|
| 65 |
-
"Alzheimer's": model_alzheimer,
|
| 66 |
-
'Tumor': model_tumor,
|
| 67 |
-
'Stroke': model_stroke
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
def call(self, inputs):
|
| 71 |
-
initial_probs = self.model_initial(inputs, training=False)
|
| 72 |
-
main_disease_idx = tf.argmax(initial_probs, axis=1)
|
| 73 |
-
main_disease = self.disease_labels[main_disease_idx[0].numpy()]
|
| 74 |
-
main_disease_prob = initial_probs[0, main_disease_idx[0]].numpy()
|
| 75 |
-
|
| 76 |
-
if main_disease == 'No Disease':
|
| 77 |
-
sub_category = "No Disease"
|
| 78 |
-
sub_category_prob = main_disease_prob
|
| 79 |
-
else:
|
| 80 |
-
sub_model = self.sub_models[main_disease]
|
| 81 |
-
sub_category_pred = sub_model(inputs, training=False)
|
| 82 |
-
sub_category = tf.argmax(sub_category_pred, axis=1).numpy()[0]
|
| 83 |
-
sub_category_prob = sub_category_pred[0, sub_category].numpy()
|
| 84 |
-
|
| 85 |
-
if main_disease == "Alzheimer's":
|
| 86 |
-
sub_category_label = ['Very Mild', 'Mild', 'Moderate']
|
| 87 |
-
elif main_disease == 'Tumor':
|
| 88 |
-
sub_category_label = ['Glioma', 'Meningioma', 'Pituitary']
|
| 89 |
-
elif main_disease == 'Stroke':
|
| 90 |
-
sub_category_label = ['Ischemic', 'Hemorrhagic']
|
| 91 |
-
|
| 92 |
-
sub_category = sub_category_label[sub_category]
|
| 93 |
-
|
| 94 |
-
return f"The MRI image shows {main_disease} with a probability of {main_disease_prob*100:.2f}%.\n" \
|
| 95 |
-
f"The subcategory of {main_disease} is {sub_category} with a probability of {sub_category_prob*100:.2f}%."
|
| 96 |
-
|
| 97 |
-
# Placeholder function to process images
|
| 98 |
-
def process_image(image):
|
| 99 |
-
image = image.resize((256, 256))
|
| 100 |
-
image.convert("RGB")
|
| 101 |
-
image_array = np.array(image) / 255.0
|
| 102 |
-
image_array = np.expand_dims(image_array, axis=0)
|
| 103 |
-
# Prediction logic here
|
| 104 |
-
# predictions = cnn_model(image_array)
|
| 105 |
-
return "Mock prediction: Disease identified with a probability of 85%."
|
| 106 |
-
|
| 107 |
-
# Function to handle patient info, query, and image processing
|
| 108 |
-
def gradio_interface(patient_info, query_type, image):
|
| 109 |
-
if image is not None:
|
| 110 |
-
image_response = process_image(image)
|
| 111 |
-
|
| 112 |
-
# Call LLM with patient info and query
|
| 113 |
-
session_id = create_chat_session()
|
| 114 |
-
query = f"Patient Info: {patient_info}\nQuery Type: {query_type}"
|
| 115 |
-
llm_response = submit_query(session_id, query)
|
| 116 |
-
|
| 117 |
-
# Debug: Print the full response to inspect it
|
| 118 |
-
print("LLM Response:", llm_response) # This will print the full response for inspection
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
else:
|
| 130 |
-
return "Please upload an image."
|
| 131 |
|
| 132 |
# Gradio interface
|
| 133 |
iface = gr.Interface(
|
|
@@ -143,14 +230,10 @@ iface = gr.Interface(
|
|
| 143 |
label="Query Type",
|
| 144 |
placeholder="Describe the type of diagnosis or information needed..."
|
| 145 |
),
|
| 146 |
-
gr.Image(
|
| 147 |
-
type="pil",
|
| 148 |
-
label="Upload an MRI Image",
|
| 149 |
-
)
|
| 150 |
],
|
| 151 |
outputs=gr.Textbox(label="Response", placeholder="The response will appear here..."),
|
| 152 |
-
title="Medical Diagnosis with
|
| 153 |
-
description="
|
| 154 |
)
|
| 155 |
|
| 156 |
iface.launch()
|
|
|
|
| 1 |
+
# import requests
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# import tensorflow as tf
|
| 4 |
+
# import tensorflow_hub as hub
|
| 5 |
+
# import gradio as gr
|
| 6 |
+
# from PIL import Image
|
| 7 |
+
|
| 8 |
+
# # Load models
|
| 9 |
+
# #model_initial = keras.models.load_model(
|
| 10 |
+
# # "models/initial_model.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 11 |
+
# #)
|
| 12 |
+
# #model_tumor = keras.models.load_model(
|
| 13 |
+
# # "models/model_tumor.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 14 |
+
# #)
|
| 15 |
+
# #model_stroke = keras.models.load_model(
|
| 16 |
+
# # "models/model_stroke.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 17 |
+
# #)
|
| 18 |
+
# #model_alzheimer = keras.models.load_model(
|
| 19 |
+
# # "models/model_alzheimer.h5", custom_objects={'KerasLayer': hub.KerasLayer}
|
| 20 |
+
|
| 21 |
+
# # API key and user ID for on-demand
|
| 22 |
+
# api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
|
| 23 |
+
# external_user_id = 'plugin-1717464304'
|
| 24 |
+
|
| 25 |
+
# # Step 1: Create a chat session with the API
|
| 26 |
+
# def create_chat_session():
|
| 27 |
+
# create_session_url = 'https://api.on-demand.io/chat/v1/sessions'
|
| 28 |
+
# create_session_headers = {
|
| 29 |
+
# 'apikey': api_key
|
| 30 |
+
# }
|
| 31 |
+
# create_session_body = {
|
| 32 |
+
# "pluginIds": [],
|
| 33 |
+
# "externalUserId": external_user_id
|
| 34 |
+
# }
|
| 35 |
+
# response = requests.post(create_session_url, headers=create_session_headers, json=create_session_body)
|
| 36 |
+
# response_data = response.json()
|
| 37 |
+
# session_id = response_data['data']['id']
|
| 38 |
+
# return session_id
|
| 39 |
+
|
| 40 |
+
# # Step 2: Submit query to the API
|
| 41 |
+
# def submit_query(session_id, query):
|
| 42 |
+
# submit_query_url = f'https://api.on-demand.io/chat/v1/sessions/{session_id}/query'
|
| 43 |
+
# submit_query_headers = {
|
| 44 |
+
# 'apikey': api_key
|
| 45 |
+
# }
|
| 46 |
+
# submit_query_body = {
|
| 47 |
+
# "endpointId": "predefined-openai-gpt4o",
|
| 48 |
+
# "query": query,
|
| 49 |
+
# "pluginIds": ["plugin-1712327325", "plugin-1713962163"],
|
| 50 |
+
# "responseMode": "sync"
|
| 51 |
+
# }
|
| 52 |
+
# response = requests.post(submit_query_url, headers=submit_query_headers, json=submit_query_body)
|
| 53 |
+
# return response.json()
|
| 54 |
+
|
| 55 |
+
# # Combined disease model (placeholder)
|
| 56 |
+
# class CombinedDiseaseModel(tf.keras.Model):
|
| 57 |
+
# def __init__(self, model_initial, model_alzheimer, model_tumor, model_stroke):
|
| 58 |
+
# super(CombinedDiseaseModel, self).__init__()
|
| 59 |
+
# self.model_initial = model_initial
|
| 60 |
+
# self.model_alzheimer = model_alzheimer
|
| 61 |
+
# self.model_tumor = model_tumor
|
| 62 |
+
# self.model_stroke = model_stroke
|
| 63 |
+
# self.disease_labels = ["Alzheimer's", 'No Disease', 'Stroke', 'Tumor']
|
| 64 |
+
# self.sub_models = {
|
| 65 |
+
# "Alzheimer's": model_alzheimer,
|
| 66 |
+
# 'Tumor': model_tumor,
|
| 67 |
+
# 'Stroke': model_stroke
|
| 68 |
+
# }
|
| 69 |
+
|
| 70 |
+
# def call(self, inputs):
|
| 71 |
+
# initial_probs = self.model_initial(inputs, training=False)
|
| 72 |
+
# main_disease_idx = tf.argmax(initial_probs, axis=1)
|
| 73 |
+
# main_disease = self.disease_labels[main_disease_idx[0].numpy()]
|
| 74 |
+
# main_disease_prob = initial_probs[0, main_disease_idx[0]].numpy()
|
| 75 |
+
|
| 76 |
+
# if main_disease == 'No Disease':
|
| 77 |
+
# sub_category = "No Disease"
|
| 78 |
+
# sub_category_prob = main_disease_prob
|
| 79 |
+
# else:
|
| 80 |
+
# sub_model = self.sub_models[main_disease]
|
| 81 |
+
# sub_category_pred = sub_model(inputs, training=False)
|
| 82 |
+
# sub_category = tf.argmax(sub_category_pred, axis=1).numpy()[0]
|
| 83 |
+
# sub_category_prob = sub_category_pred[0, sub_category].numpy()
|
| 84 |
+
|
| 85 |
+
# if main_disease == "Alzheimer's":
|
| 86 |
+
# sub_category_label = ['Very Mild', 'Mild', 'Moderate']
|
| 87 |
+
# elif main_disease == 'Tumor':
|
| 88 |
+
# sub_category_label = ['Glioma', 'Meningioma', 'Pituitary']
|
| 89 |
+
# elif main_disease == 'Stroke':
|
| 90 |
+
# sub_category_label = ['Ischemic', 'Hemorrhagic']
|
| 91 |
+
|
| 92 |
+
# sub_category = sub_category_label[sub_category]
|
| 93 |
+
|
| 94 |
+
# return f"The MRI image shows {main_disease} with a probability of {main_disease_prob*100:.2f}%.\n" \
|
| 95 |
+
# f"The subcategory of {main_disease} is {sub_category} with a probability of {sub_category_prob*100:.2f}%."
|
| 96 |
+
|
| 97 |
+
# # Placeholder function to process images
|
| 98 |
+
# def process_image(image):
|
| 99 |
+
# image = image.resize((256, 256))
|
| 100 |
+
# image.convert("RGB")
|
| 101 |
+
# image_array = np.array(image) / 255.0
|
| 102 |
+
# image_array = np.expand_dims(image_array, axis=0)
|
| 103 |
+
# # Prediction logic here
|
| 104 |
+
# # predictions = cnn_model(image_array)
|
| 105 |
+
# return "Mock prediction: Disease identified with a probability of 85%."
|
| 106 |
+
|
| 107 |
+
# # Function to handle patient info, query, and image processing
|
| 108 |
+
# def gradio_interface(patient_info, query_type, image):
|
| 109 |
+
# if image is not None:
|
| 110 |
+
# image_response = process_image(image)
|
| 111 |
+
|
| 112 |
+
# # Call LLM with patient info and query
|
| 113 |
+
# session_id = create_chat_session()
|
| 114 |
+
# query = f"Patient Info: {patient_info}\nQuery Type: {query_type}"
|
| 115 |
+
# llm_response = submit_query(session_id, query)
|
| 116 |
+
|
| 117 |
+
# # Debug: Print the full response to inspect it
|
| 118 |
+
# print("LLM Response:", llm_response) # This will print the full response for inspection
|
| 119 |
+
|
| 120 |
+
# # Safely handle 'message' if it exists
|
| 121 |
+
# message = llm_response.get('data', {}).get('message', 'No message returned from LLM')
|
| 122 |
+
|
| 123 |
+
# # Check if message is empty and print the complete response if necessary
|
| 124 |
+
# if message == 'No message returned from LLM':
|
| 125 |
+
# print("Full LLM Response Data:", llm_response) # Inspect the full LLM response for any helpful info
|
| 126 |
+
|
| 127 |
+
# response = f"Patient Info: {patient_info}\nQuery Type: {query_type}\n\n{image_response}\n\nLLM Response:\n{message}"
|
| 128 |
+
# return response
|
| 129 |
+
# else:
|
| 130 |
+
# return "Please upload an image."
|
| 131 |
+
|
| 132 |
+
# # Gradio interface
|
| 133 |
+
# iface = gr.Interface(
|
| 134 |
+
# fn=gradio_interface,
|
| 135 |
+
# inputs=[
|
| 136 |
+
# gr.Textbox(
|
| 137 |
+
# label="Patient Information",
|
| 138 |
+
# placeholder="Enter patient details here...",
|
| 139 |
+
# lines=5,
|
| 140 |
+
# max_lines=10
|
| 141 |
+
# ),
|
| 142 |
+
# gr.Textbox(
|
| 143 |
+
# label="Query Type",
|
| 144 |
+
# placeholder="Describe the type of diagnosis or information needed..."
|
| 145 |
+
# ),
|
| 146 |
+
# gr.Image(
|
| 147 |
+
# type="pil",
|
| 148 |
+
# label="Upload an MRI Image",
|
| 149 |
+
# )
|
| 150 |
+
# ],
|
| 151 |
+
# outputs=gr.Textbox(label="Response", placeholder="The response will appear here..."),
|
| 152 |
+
# title="Medical Diagnosis with MRI and LLM",
|
| 153 |
+
# description="Upload MRI images and provide patient information for a combined CNN model and LLM analysis."
|
| 154 |
+
# )
|
| 155 |
+
|
| 156 |
+
# iface.launch()
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
import requests
|
|
|
|
|
|
|
|
|
|
| 163 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# API key and user ID for on-demand
|
| 166 |
api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
|
|
|
|
| 196 |
response = requests.post(submit_query_url, headers=submit_query_headers, json=submit_query_body)
|
| 197 |
return response.json()
|
| 198 |
|
| 199 |
+
# Function to handle patient info, query, and image processing (now focusing on LLM)
|
| 200 |
+
def gradio_interface(patient_info, query_type):
|
| 201 |
+
# Call LLM with patient info and query
|
| 202 |
+
session_id = create_chat_session()
|
| 203 |
+
query = f"Patient Info: {patient_info}\nQuery Type: {query_type}"
|
| 204 |
+
llm_response = submit_query(session_id, query)
|
| 205 |
+
|
| 206 |
+
# Debug: Print the full response to inspect it
|
| 207 |
+
print("LLM Response:", llm_response) # This will print the full response for inspection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
# Safely handle 'message' if it exists
|
| 210 |
+
message = llm_response.get('data', {}).get('message', 'No message returned from LLM')
|
| 211 |
|
| 212 |
+
# Check if message is empty and print the complete response if necessary
|
| 213 |
+
if message == 'No message returned from LLM':
|
| 214 |
+
print("Full LLM Response Data:", llm_response) # Inspect the full LLM response for any helpful info
|
| 215 |
|
| 216 |
+
response = f"Patient Info: {patient_info}\nQuery Type: {query_type}\n\nLLM Response:\n{message}"
|
| 217 |
+
return response
|
|
|
|
|
|
|
| 218 |
|
| 219 |
# Gradio interface
|
| 220 |
iface = gr.Interface(
|
|
|
|
| 230 |
label="Query Type",
|
| 231 |
placeholder="Describe the type of diagnosis or information needed..."
|
| 232 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
],
|
| 234 |
outputs=gr.Textbox(label="Response", placeholder="The response will appear here..."),
|
| 235 |
+
title="Medical Diagnosis with LLM",
|
| 236 |
+
description="Provide patient information and a query type for analysis by the LLM."
|
| 237 |
)
|
| 238 |
|
| 239 |
iface.launch()
|