|
import os |
|
from crewai import Agent, Task, Crew |
|
from langchain_groq import ChatGroq |
|
from PIL import Image |
|
import streamlit as st |
|
|
|
|
|
llm = ChatGroq( |
|
groq_api_key="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", |
|
model_name="llama-3.1-70b-versatile", |
|
) |
|
|
|
|
|
medical_image_agent = Agent( |
|
role='Medical Image Captioning Agent', |
|
goal='Analyze medical images and provide a title, description, and reasons for detected issues.', |
|
backstory=( |
|
"You are a Medical Image Captioning Agent. Your role is to analyze medical-related images " |
|
"Figure out issues from the image of X-rays, skin blemishes, or accident pictures, and then generate a relevant title, description, " |
|
"and reasons why issues might have occurred." |
|
), |
|
verbose=True, |
|
llm=llm, |
|
) |
|
|
|
def process_image_with_agent(image_path, agent): |
|
"""Process the uploaded image using the AI agent.""" |
|
try: |
|
image = Image.open(image_path) |
|
except Exception as e: |
|
return f"Error opening image: {e}" |
|
|
|
|
|
task_description = f"Analyze the medical image and provide a title, description, and reasons for detected issues. Image path: {image_path}" |
|
|
|
|
|
medical_image_task = Task( |
|
description=task_description, |
|
agent=agent, |
|
human_input=False, |
|
expected_output="Title, description, and reasons for detected issues", |
|
) |
|
|
|
|
|
crew = Crew( |
|
agents=[agent], |
|
tasks=[medical_image_task], |
|
verbose=2, |
|
) |
|
|
|
|
|
result = crew.kickoff() |
|
return result |
|
|
|
|
|
def main(): |
|
st.title("Medical Image Analysis and Captioning") |
|
st.write( |
|
"Upload a medical-related image (e.g., X-ray, skin blemish, accident photo), " |
|
"and the AI agent will analyze it to provide relevant insights." |
|
) |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "bmp"]) |
|
|
|
if uploaded_file: |
|
|
|
temp_file_path = f"temp_{uploaded_file.name}" |
|
with open(temp_file_path, "wb") as f: |
|
f.write(uploaded_file.getbuffer()) |
|
|
|
|
|
st.image(temp_file_path, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
with st.spinner("Analyzing the image..."): |
|
result = process_image_with_agent(temp_file_path, medical_image_agent) |
|
|
|
|
|
st.write("### Analysis Result:") |
|
st.write(result) |
|
|
|
|
|
os.remove(temp_file_path) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|