bravewiki commited on
Commit
dd380e5
·
verified ·
1 Parent(s): 7616a71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -35
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
- # Load Hugging Face model
7
- model = pipeline("text-generation", model="Ketak-ZoomRx/Drug_Prompt_Ollama_67k",torch_dtype="auto", trust_remote_code=True, use_fast=True, device_map={"": "cuda:0"},)
 
8
 
9
- def extract_text_from_pdf(pdf_file):
10
- reader = PdfReader(pdf_file)
11
- text = ""
12
- for page in reader.pages:
13
- text += page.extract_text()
14
- return text
15
 
16
- def get_lab_report_interpretation(text):
17
- result = model(text)
18
- return result[0]['label']
 
 
 
 
 
 
 
19
 
20
- def process_image(image_file):
21
- img = Image.open(image_file)
22
- return img
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Streamlit UI
25
- st.title("Medical Lab Report Analyzer")
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- # Upload image and PDF
28
- uploaded_pdf = st.file_uploader("Upload your PDF Medical Lab Report", type="pdf")
29
- uploaded_image = st.file_uploader("Upload Image of Medical Report", type="jpg png jpeg")
 
30
 
31
- if uploaded_pdf is not None:
32
- st.subheader("Extracted Text from PDF:")
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
- elif uploaded_image is not None:
41
- st.image(process_image(uploaded_image), caption="Uploaded Image", use_column_width=True)
42
- st.warning("For text extraction from image, please upload a PDF.")
43
-
44
- else:
45
- st.info("Please upload a medical PDF or image for analysis.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()