Spaces:
Sleeping
Sleeping
File size: 7,775 Bytes
de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 de080b7 248b0d6 |
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 |
import streamlit as st
from transformers import pipeline
from PIL import Image
import numpy as np
import cv2
from rag_model import *
from yolo_model import *
@st.cache_resource
def load_image_model():
return pipeline("image-classification", model="Heem2/wound-image-classification")
pipeline = load_image_model()
yolo_model = load_yolo_model()
# Add custom CSS
css = """
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f5f5f5;
}
.main {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.stButton button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 5px;
}
.stButton button:hover {
background-color: #45a049;
}
.stApp > header {
background-color: transparent;
}
.stApp {
margin: auto;
background-color: #D9AFD9;
background-image: linear-gradient(0deg, #D9AFD9 0%, #97D9E1 100%);
}
[data-testid='stFileUploader'] {
width: max-content;
}
[data-testid='stFileUploader'] section {
padding: 0;
float: left;
}
[data-testid='stFileUploader'] section > input + div {
display: none;
}
[data-testid='stFileUploader'] section + div {
float: right;
padding-top: 0;
}
</style>
"""
st.markdown(css, unsafe_allow_html=True)
st.title("**FirstAid-AI**")
# Add a description at the top
st.markdown("""
### Welcome to FirstAid-AI
This application provides medical advice based on images of wounds and medical equipment.
Upload an image of your wound or medical equipment, and the AI will classify the image and provide relevant advice.
""")
st.markdown("## How to Use FirstAid-AI")
st.markdown("### 1. Upload an image of a wound and a piece of equipment (if applicable)")
st.image("images/example3.png", use_container_width=True)
st.caption("The AI model will detect the wound or equipment in the image and provide confidence levels. The AI assistant will then provide treatment or usage advice.")
st.markdown("### 2. Ask follow-up questions and continue the conversation with the AI assistant!")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Dropdown to select the type of images to provide
option = st.selectbox(
"Select the type of images you want to provide:",
("Provide just wound image", "Provide both wound and equipment")
)
# Upload images based on the selected option
file_wound = None
file_equipment = None
if option == "Provide just wound image":
file_wound = st.file_uploader("Upload an image of your wound")
elif option == "Provide both wound and equipment":
file_wound = st.file_uploader("Upload an image of your wound")
file_equipment = st.file_uploader("Upload an image of your equipment")
# Reset chat history if no file is uploaded
if file_wound is None and file_equipment is None:
st.session_state.messages = []
if file_wound is not None and option == "Provide just wound image":
# Display the wound image and predictions
col1, col2 = st.columns(2)
image = Image.open(file_wound)
col1.image(image, use_container_width=True)
# Classify the wound image
predictions = pipeline(image)
detected_wound = predictions[0]['label']
col2.header("Detected Wound")
for p in predictions:
col2.subheader(f"{p['label']}: {round(p['score'] * 100, 1)}%")
# Initial advice for wound
if not st.session_state.messages:
initial_query = f"Provide treatment advice for a {detected_wound} wound"
initial_response = rag_chain.invoke(initial_query)
st.session_state.messages.append({"role": "assistant", "content": initial_response})
# Display chat messages from history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input if an image is uploaded
if (file_wound is not None or file_equipment is not None) and (prompt := st.chat_input("Ask a follow-up question or continue the conversation:")):
# Display user message in chat
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Prepare the conversation history for rag_chain
conversation_history = "\n".join(
f"{message['role']}: {message['content']}" for message in st.session_state.messages
)
# Generate response from rag_chain
query = f"Context:\n{conversation_history}\n\nAssistant, respond to the user's latest query: {prompt}"
response = rag_chain.invoke(query)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
if file_wound is not None and file_equipment is not None and option == "Provide both wound and equipment":
# Display the wound image and predictions
col1, col2 = st.columns(2)
image = Image.open(file_wound)
col1.image(image, use_container_width=True)
# Classify the wound image
predictions = pipeline(image)
detected_wound = predictions[0]['label']
col2.header("Detected Wound")
for p in predictions:
col2.subheader(f"{p['label']}: {round(p['score'] * 100, 1)}%")
# Display the equipment image and predictions
col3, col4 = st.columns(2)
image = Image.open(file_equipment)
col3.image(image, use_container_width=True)
# Convert the image to a format supported by YOLO
image_np = np.array(image)
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
# Classify the equipment image using YOLO model
detected_equipment = get_detected_objects(yolo_model, image_cv)
col4.header("Detected Equipment")
col4.subheader(detected_equipment)
# Initial advice for equipment
if not st.session_state.messages:
initial_query = f"Provide usage advice for {detected_equipment} when treating a {detected_wound} wound"
initial_response = rag_chain.invoke(initial_query)
st.session_state.messages.append({"role": "assistant", "content": initial_response})
# Display chat messages from history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input if an image is uploaded
if (file_wound is not None or file_equipment is not None) and (prompt := st.chat_input("Ask a follow-up question or continue the conversation:")):
# Display user message in chat
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Prepare the conversation history for rag_chain
conversation_history = "\n".join(
f"{message['role']}: {message['content']}" for message in st.session_state.messages
)
# Generate response from rag_chain
query = f"Context:\n{conversation_history}\n\nAssistant, respond to the user's latest query: {prompt}"
response = rag_chain.invoke(query)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response}) |