File size: 2,868 Bytes
c944ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from crewai import Agent, Task, Crew
from langchain_groq import ChatGroq
from PIL import Image
import streamlit as st

# Initialize the LLM (assuming it can also handle image processing)
llm = ChatGroq(
    groq_api_key="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",
    model_name="llama-3.1-70b-versatile",
)

# Define the agent for medical image analysis
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}"

    # Define the task for the agent
    task_description = f"Analyze the medical image and provide a title, description, and reasons for detected issues. Image path: {image_path}"

    # Define the task
    medical_image_task = Task(
        description=task_description,
        agent=agent,
        human_input=False,
        expected_output="Title, description, and reasons for detected issues",
    )

    # Instantiate the crew with the defined agent and task
    crew = Crew(
        agents=[agent],
        tasks=[medical_image_task],
        verbose=2,
    )

    # Execute the task and return the result
    result = crew.kickoff()
    return result

# Streamlit App
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."
    )

    # File uploader
    uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "bmp"])

    if uploaded_file:
        # Save the uploaded file locally
        temp_file_path = f"temp_{uploaded_file.name}"
        with open(temp_file_path, "wb") as f:
            f.write(uploaded_file.getbuffer())

        # Display the uploaded image
        st.image(temp_file_path, caption="Uploaded Image", use_column_width=True)

        # Process the image
        with st.spinner("Analyzing the image..."):
            result = process_image_with_agent(temp_file_path, medical_image_agent)

        # Display the result
        st.write("### Analysis Result:")
        st.write(result)

        # Remove the temporary file
        os.remove(temp_file_path)

if __name__ == "__main__":
    main()