File size: 3,196 Bytes
c953483
914d63a
dd380e5
4fdc44f
dd380e5
 
 
c953483
dd380e5
 
 
 
 
c953483
dd380e5
 
 
 
 
 
 
 
 
 
c953483
dd380e5
 
 
 
 
 
 
 
 
 
 
 
 
c953483
dd380e5
 
 
 
 
 
 
 
 
 
 
 
 
c953483
dd380e5
 
 
 
11ddcf8
dd380e5
 
11ddcf8
dd380e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image
import google.generativeai as genai

# Configure Google Generative AI
genai_api_key = "AIzaSyCOEqA_IZlpWCHhMOGaDJ3iJjl5cRmzKgQ"
genai.configure(api_key=genai_api_key)

# Initialize Gemini model
@st.cache_resource
def load_gemini_model():
    model = genai.GenerativeModel("gemini-1.5-flash")
    return model

# Function to extract text from the image using Gemini model
def extract_text_from_image(uploaded_file, model):
    # Open the uploaded file as a PIL image
    image = Image.open(uploaded_file).convert("RGB")
    
    # Generate content using the Gemini model with the image
    response = model.generate_content(["Extract text from this medical report:", image])
    extracted_text = response.text.strip()
    
    return extracted_text

# Function to interpret the extracted text in layman's language
def interpret_medical_report(extracted_text, model):
    # Provide interpretation in layman's terms
    prompt = (
        f"The following is a medical report text:\n\n"
        f"{extracted_text}\n\n"
        "Please interpret this report for 7th grader and non native english speaker, "
        "explaining the main findings in as short as possible"
    )
    response = model.generate_content([prompt])
    interpretation = response.text.strip()
    
    return interpretation

# Function to provide recommendations based on the extracted text
def provide_recommendations(extracted_text, model):
    # Provide recommendations
    prompt = (
        f"Based on the medical report text below:\n\n"
        f"{extracted_text}\n\n"
        "What recommendations would you give to the patient for managing their health?"
        "Provide brief suggestions that are easy to understand for someone without medical knowledge."
    )
    response = model.generate_content([prompt])
    recommendations = response.text.strip()
    
    return recommendations

# Streamlit UI for the web app
def main():
    st.title("Medical Report Analyzer Using Gemini Model")
    st.write("Upload an image of a medical report")

    # Load the Gemini model
    model = load_gemini_model()

    # File uploader for medical report image
    uploaded_image = st.file_uploader("Upload Medical Report Image", type=["png", "jpg", "jpeg"])

    if uploaded_image is not None:
        image = Image.open(uploaded_image).convert("RGB")
        st.image(image, caption="Uploaded Medical Report Image", use_container_width=True)

        if st.button("Analyze Report"):
            with st.spinner("Processing image and analyzing report..."):
                # Extract text from image
                extracted_text = extract_text_from_image(uploaded_image, model)

                # Interpret the extracted text
                st.subheader("Interpretation in Layman's Language:")
                interpretation = interpret_medical_report(extracted_text, model)
                st.text(interpretation)

                # Provide health recommendations
                st.subheader("Health Recommendations:")
                recommendations = provide_recommendations(extracted_text, model)
                st.text(recommendations)

if __name__ == "__main__":
    main()