Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,85 @@
|
|
1 |
import streamlit as st
|
2 |
-
from PyPDF2 import PdfReader
|
3 |
-
from transformers import pipeline
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
return text
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
34 |
-
st.write(pdf_text)
|
35 |
-
|
36 |
-
st.subheader("Lab Report Interpretation:")
|
37 |
-
interpretation = get_lab_report_interpretation(pdf_text)
|
38 |
-
st.write(interpretation)
|
39 |
|
40 |
-
|
41 |
-
st.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from PIL import Image
|
3 |
+
import google.generativeai as genai
|
4 |
|
5 |
+
# Configure Google Generative AI
|
6 |
+
genai_api_key = "AIzaSyCOEqA_IZlpWCHhMOGaDJ3iJjl5cRmzKgQ"
|
7 |
+
genai.configure(api_key=genai_api_key)
|
8 |
|
9 |
+
# Initialize Gemini model
|
10 |
+
@st.cache_resource
|
11 |
+
def load_gemini_model():
|
12 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
13 |
+
return model
|
|
|
14 |
|
15 |
+
# Function to extract text from the image using Gemini model
|
16 |
+
def extract_text_from_image(uploaded_file, model):
|
17 |
+
# Open the uploaded file as a PIL image
|
18 |
+
image = Image.open(uploaded_file).convert("RGB")
|
19 |
+
|
20 |
+
# Generate content using the Gemini model with the image
|
21 |
+
response = model.generate_content(["Extract text from this medical report:", image])
|
22 |
+
extracted_text = response.text.strip()
|
23 |
+
|
24 |
+
return extracted_text
|
25 |
|
26 |
+
# Function to interpret the extracted text in layman's language
|
27 |
+
def interpret_medical_report(extracted_text, model):
|
28 |
+
# Provide interpretation in layman's terms
|
29 |
+
prompt = (
|
30 |
+
f"The following is a medical report text:\n\n"
|
31 |
+
f"{extracted_text}\n\n"
|
32 |
+
"Please interpret this report for 7th grader and non native english speaker, "
|
33 |
+
"explaining the main findings in as short as possible"
|
34 |
+
)
|
35 |
+
response = model.generate_content([prompt])
|
36 |
+
interpretation = response.text.strip()
|
37 |
+
|
38 |
+
return interpretation
|
39 |
|
40 |
+
# Function to provide recommendations based on the extracted text
|
41 |
+
def provide_recommendations(extracted_text, model):
|
42 |
+
# Provide recommendations
|
43 |
+
prompt = (
|
44 |
+
f"Based on the medical report text below:\n\n"
|
45 |
+
f"{extracted_text}\n\n"
|
46 |
+
"What recommendations would you give to the patient for managing their health?"
|
47 |
+
"Provide brief suggestions that are easy to understand for someone without medical knowledge."
|
48 |
+
)
|
49 |
+
response = model.generate_content([prompt])
|
50 |
+
recommendations = response.text.strip()
|
51 |
+
|
52 |
+
return recommendations
|
53 |
|
54 |
+
# Streamlit UI for the web app
|
55 |
+
def main():
|
56 |
+
st.title("Medical Report Analyzer Using Gemini Model")
|
57 |
+
st.write("Upload an image of a medical report")
|
58 |
|
59 |
+
# Load the Gemini model
|
60 |
+
model = load_gemini_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
# File uploader for medical report image
|
63 |
+
uploaded_image = st.file_uploader("Upload Medical Report Image", type=["png", "jpg", "jpeg"])
|
64 |
+
|
65 |
+
if uploaded_image is not None:
|
66 |
+
image = Image.open(uploaded_image).convert("RGB")
|
67 |
+
st.image(image, caption="Uploaded Medical Report Image", use_container_width=True)
|
68 |
+
|
69 |
+
if st.button("Analyze Report"):
|
70 |
+
with st.spinner("Processing image and analyzing report..."):
|
71 |
+
# Extract text from image
|
72 |
+
extracted_text = extract_text_from_image(uploaded_image, model)
|
73 |
+
|
74 |
+
# Interpret the extracted text
|
75 |
+
st.subheader("Interpretation in Layman's Language:")
|
76 |
+
interpretation = interpret_medical_report(extracted_text, model)
|
77 |
+
st.text(interpretation)
|
78 |
+
|
79 |
+
# Provide health recommendations
|
80 |
+
st.subheader("Health Recommendations:")
|
81 |
+
recommendations = provide_recommendations(extracted_text, model)
|
82 |
+
st.text(recommendations)
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
main()
|