Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,87 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from crewai import Agent, Task, Crew
|
| 3 |
-
from langchain_groq import ChatGroq
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import streamlit as st
|
| 6 |
-
|
| 7 |
-
# Initialize the LLM (assuming it can also handle image processing)
|
| 8 |
-
llm = ChatGroq(
|
| 9 |
-
groq_api_key="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",
|
| 10 |
-
model_name="llama-3.1-70b-versatile",
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# Define the agent for medical image analysis
|
| 14 |
-
medical_image_agent = Agent(
|
| 15 |
-
role='Medical Image Captioning Agent',
|
| 16 |
-
goal='Analyze medical images and provide a title, description, and reasons for detected issues.',
|
| 17 |
-
backstory=(
|
| 18 |
-
"You are a Medical Image Captioning Agent. Your role is to analyze medical-related images "
|
| 19 |
-
"Figure out
|
| 20 |
-
"and reasons why issues might have occurred."
|
| 21 |
-
),
|
| 22 |
-
verbose=True,
|
| 23 |
-
llm=llm,
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
def process_image_with_agent(image_path, agent):
|
| 27 |
-
"""Process the uploaded image using the AI agent."""
|
| 28 |
-
try:
|
| 29 |
-
image = Image.open(image_path)
|
| 30 |
-
except Exception as e:
|
| 31 |
-
return f"Error opening image: {e}"
|
| 32 |
-
|
| 33 |
-
# Define the task for the agent
|
| 34 |
-
task_description = f"Analyze the medical image and provide a title, description, and reasons for detected issues. Image path: {image_path}"
|
| 35 |
-
|
| 36 |
-
# Define the task
|
| 37 |
-
medical_image_task = Task(
|
| 38 |
-
description=task_description,
|
| 39 |
-
agent=agent,
|
| 40 |
-
human_input=False,
|
| 41 |
-
expected_output="Title, description, and reasons for detected issues",
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
# Instantiate the crew with the defined agent and task
|
| 45 |
-
crew = Crew(
|
| 46 |
-
agents=[agent],
|
| 47 |
-
tasks=[medical_image_task],
|
| 48 |
-
verbose=2,
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Execute the task and return the result
|
| 52 |
-
result = crew.kickoff()
|
| 53 |
-
return result
|
| 54 |
-
|
| 55 |
-
# Streamlit App
|
| 56 |
-
def main():
|
| 57 |
-
st.title("Medical Image Analysis and Captioning")
|
| 58 |
-
st.write(
|
| 59 |
-
"Upload a medical-related image (e.g., X-ray, skin blemish, accident photo), "
|
| 60 |
-
"and the AI agent will analyze it to provide relevant insights."
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# File uploader
|
| 64 |
-
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "bmp"])
|
| 65 |
-
|
| 66 |
-
if uploaded_file:
|
| 67 |
-
# Save the uploaded file locally
|
| 68 |
-
temp_file_path = f"temp_{uploaded_file.name}"
|
| 69 |
-
with open(temp_file_path, "wb") as f:
|
| 70 |
-
f.write(uploaded_file.getbuffer())
|
| 71 |
-
|
| 72 |
-
# Display the uploaded image
|
| 73 |
-
st.image(temp_file_path, caption="Uploaded Image", use_column_width=True)
|
| 74 |
-
|
| 75 |
-
# Process the image
|
| 76 |
-
with st.spinner("Analyzing the image..."):
|
| 77 |
-
result = process_image_with_agent(temp_file_path, medical_image_agent)
|
| 78 |
-
|
| 79 |
-
# Display the result
|
| 80 |
-
st.write("### Analysis Result:")
|
| 81 |
-
st.write(result)
|
| 82 |
-
|
| 83 |
-
# Remove the temporary file
|
| 84 |
-
os.remove(temp_file_path)
|
| 85 |
-
|
| 86 |
-
if __name__ == "__main__":
|
| 87 |
-
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from crewai import Agent, Task, Crew
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
# Initialize the LLM (assuming it can also handle image processing)
|
| 8 |
+
llm = ChatGroq(
|
| 9 |
+
groq_api_key="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",
|
| 10 |
+
model_name="llama-3.1-70b-versatile",
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Define the agent for medical image analysis
|
| 14 |
+
medical_image_agent = Agent(
|
| 15 |
+
role='Medical Image Captioning Agent',
|
| 16 |
+
goal='Analyze medical images and provide a title, description, and reasons for detected issues.',
|
| 17 |
+
backstory=(
|
| 18 |
+
"You are a Medical Image Captioning Agent. Your role is to analyze medical-related images "
|
| 19 |
+
"Figure out issues from the image of X-rays, skin blemishes, or accident pictures, and then generate a relevant title, description, "
|
| 20 |
+
"and reasons why issues might have occurred."
|
| 21 |
+
),
|
| 22 |
+
verbose=True,
|
| 23 |
+
llm=llm,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def process_image_with_agent(image_path, agent):
|
| 27 |
+
"""Process the uploaded image using the AI agent."""
|
| 28 |
+
try:
|
| 29 |
+
image = Image.open(image_path)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error opening image: {e}"
|
| 32 |
+
|
| 33 |
+
# Define the task for the agent
|
| 34 |
+
task_description = f"Analyze the medical image and provide a title, description, and reasons for detected issues. Image path: {image_path}"
|
| 35 |
+
|
| 36 |
+
# Define the task
|
| 37 |
+
medical_image_task = Task(
|
| 38 |
+
description=task_description,
|
| 39 |
+
agent=agent,
|
| 40 |
+
human_input=False,
|
| 41 |
+
expected_output="Title, description, and reasons for detected issues",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Instantiate the crew with the defined agent and task
|
| 45 |
+
crew = Crew(
|
| 46 |
+
agents=[agent],
|
| 47 |
+
tasks=[medical_image_task],
|
| 48 |
+
verbose=2,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Execute the task and return the result
|
| 52 |
+
result = crew.kickoff()
|
| 53 |
+
return result
|
| 54 |
+
|
| 55 |
+
# Streamlit App
|
| 56 |
+
def main():
|
| 57 |
+
st.title("Medical Image Analysis and Captioning")
|
| 58 |
+
st.write(
|
| 59 |
+
"Upload a medical-related image (e.g., X-ray, skin blemish, accident photo), "
|
| 60 |
+
"and the AI agent will analyze it to provide relevant insights."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# File uploader
|
| 64 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "bmp"])
|
| 65 |
+
|
| 66 |
+
if uploaded_file:
|
| 67 |
+
# Save the uploaded file locally
|
| 68 |
+
temp_file_path = f"temp_{uploaded_file.name}"
|
| 69 |
+
with open(temp_file_path, "wb") as f:
|
| 70 |
+
f.write(uploaded_file.getbuffer())
|
| 71 |
+
|
| 72 |
+
# Display the uploaded image
|
| 73 |
+
st.image(temp_file_path, caption="Uploaded Image", use_column_width=True)
|
| 74 |
+
|
| 75 |
+
# Process the image
|
| 76 |
+
with st.spinner("Analyzing the image..."):
|
| 77 |
+
result = process_image_with_agent(temp_file_path, medical_image_agent)
|
| 78 |
+
|
| 79 |
+
# Display the result
|
| 80 |
+
st.write("### Analysis Result:")
|
| 81 |
+
st.write(result)
|
| 82 |
+
|
| 83 |
+
# Remove the temporary file
|
| 84 |
+
os.remove(temp_file_path)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
main()
|