dharak003 commited on
Commit
fddd5eb
·
verified ·
1 Parent(s): d807463

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +96 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import base64
4
+ from langchain_google_genai import ChatGoogleGenerativeAI
5
+ from langchain_core.messages import HumanMessage
6
+ from dotenv import load_dotenv
7
+
8
+ # Load API key from .env file
9
+ load_dotenv()
10
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
11
+
12
+ # Initialize the Gemini model
13
+ llm = ChatGoogleGenerativeAI(
14
+ model="gemini-2.0-flash",
15
+ max_tokens=4000,
16
+ api_key=GEMINI_API_KEY
17
+ )
18
+
19
+ def encode_file(file):
20
+ return base64.b64encode(file.read()).decode()
21
+
22
+ def summarize_report(file, file_type):
23
+ encoded_string = encode_file(file)
24
+
25
+ prompt = """You are a highly advanced medical document analysis AI designed to assist live agents and doctors by extracting and summarizing fluctuating or improper test results from uploaded medical reports. Your task is to carefully parse the document, identify irregularities in test values, highlight trends that indicate instability, and generate a clear, structured summary.
26
+ Ensure the summary includes:
27
+ 1) The test names with significant fluctuations or abnormal values.
28
+ 2) A brief comparison of fluctuating values over time (if historical data is available).
29
+ 3) Possible concerns based on deviations from normal ranges.
30
+ 4) A concise, professional summary that can be quickly reviewed by a medical expert or triage bot for primary screening.
31
+ Format the output to be clear, concise, and medically relevant. Do not make any medical diagnoses—only summarize fluctuations and abnormalities in an easily interpretable manner."""
32
+
33
+ content = [{"type": "text", "text": prompt}]
34
+
35
+ if file_type in ["image/jpeg", "image/png", "image/webp"]:
36
+ content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_string}"}})
37
+ else:
38
+ content.append({"type": "text", "text": encoded_string})
39
+
40
+ message = HumanMessage(content=content)
41
+ ai_msg = llm.invoke([message])
42
+ return ai_msg.content
43
+
44
+ # Streamlit UI
45
+ st.set_page_config(page_title="Medical Report Summarizer", layout="wide", page_icon="📄")
46
+
47
+ # Custom CSS for styling
48
+ st.markdown("""
49
+ <style>
50
+ .main {
51
+ background-color: #f5f5f5;
52
+ }
53
+ .stButton>button {
54
+ background-color: #4CAF50;
55
+ color: white;
56
+ border-radius: 8px;
57
+ padding: 10px 20px;
58
+ }
59
+ .stSpinner {
60
+ color: #4CAF50;
61
+ }
62
+ </style>
63
+ """, unsafe_allow_html=True)
64
+
65
+ # Header Section
66
+ st.title("📄 Medical Report Summarizer")
67
+ st.markdown("""
68
+ <div style="background-color: #4CAF50; padding: 10px; border-radius: 10px;">
69
+ <h2 style="color: white; text-align: center;">Upload your medical report and get a summarized analysis!</h2>
70
+ </div>
71
+ """, unsafe_allow_html=True)
72
+
73
+ # File Upload Section
74
+ st.write("### Upload your medical report (image, PDF, or Word document):")
75
+ uploaded_file = st.file_uploader("", type=["pdf", "doc", "docx", "jpg", "jpeg", "png", "webp"])
76
+
77
+ # Process Uploaded File
78
+ if uploaded_file is not None:
79
+ file_type = uploaded_file.type
80
+ with st.spinner("🔄 Processing the document..."):
81
+ summary = summarize_report(uploaded_file, file_type)
82
+
83
+ # Display Summary
84
+ st.success("✅ Document processed successfully!")
85
+ st.subheader("Summarized Report:")
86
+ st.write(summary)
87
+
88
+ # Download Button for Summary
89
+ st.download_button(
90
+ label="📥 Download Summary",
91
+ data=summary,
92
+ file_name="summary.txt",
93
+ mime="text/plain"
94
+ )
95
+ else:
96
+ st.info("ℹ️ Please upload a file to get started.")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit==1.44.1
2
+ python-dotenv==1.1.0
3
+ langchain-google-genai==2.1.2