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()