Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,63 @@
|
|
| 1 |
-
import
|
| 2 |
-
import google.generativeai as genai
|
| 3 |
-
import
|
| 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 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from transformers import AutoModel, AutoTokenizer
|
| 4 |
+
from pdf2image import convert_from_path
|
| 5 |
+
import torch
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load the OCR model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True)
|
| 10 |
+
model = AutoModel.from_pretrained("ucaslcl/GOT-OCR2_0", trust_remote_code=True, low_cpu_mem_usage=True,
|
| 11 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu",
|
| 12 |
+
use_safetensors=True, pad_token_id=tokenizer.eos_token_id).eval()
|
| 13 |
+
|
| 14 |
+
def extract_text_from_pdf(pdf_path):
|
| 15 |
+
"""Converts PDF pages to images and extracts text using the GOT-OCR2_0 model."""
|
| 16 |
+
text = ""
|
| 17 |
+
try:
|
| 18 |
+
images = convert_from_path(pdf_path)
|
| 19 |
+
for idx, image in enumerate(images):
|
| 20 |
+
image_path = f"temp_page_{idx}.png"
|
| 21 |
+
image.save(image_path, "PNG")
|
| 22 |
+
extracted_text = model.chat(tokenizer, image_path, ocr_type="ocr")
|
| 23 |
+
text += extracted_text + "\n"
|
| 24 |
+
os.remove(image_path) # Clean up the temporary image file
|
| 25 |
+
except Exception as e:
|
| 26 |
+
st.error(f"Error extracting text: {e}")
|
| 27 |
+
return text
|
| 28 |
+
|
| 29 |
+
def analyze_health_data(text):
|
| 30 |
+
"""Analyzes extracted text using Google Generative AI (Free Tier API)."""
|
| 31 |
+
try:
|
| 32 |
+
genai.configure(api_key="AIzaSyAY6ZYxOzVV5N7mBZzDJ96WEPJGfuFx-mU") # Replace with your Google API key
|
| 33 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 34 |
+
response = model.generate_content(
|
| 35 |
+
f"Analyze this medical report and provide trends, risks, and health suggestions:\n{text}"
|
| 36 |
+
)
|
| 37 |
+
return response.text
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error in LLM response: {e}"
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
st.title("Health Report Analyzer")
|
| 43 |
+
uploaded_file = st.file_uploader("Upload your health report (PDF)", type=["pdf"])
|
| 44 |
+
|
| 45 |
+
if uploaded_file is not None:
|
| 46 |
+
pdf_path = "temp.pdf"
|
| 47 |
+
with open(pdf_path, "wb") as f:
|
| 48 |
+
f.write(uploaded_file.getbuffer())
|
| 49 |
+
|
| 50 |
+
with st.spinner("Extracting text from the report..."):
|
| 51 |
+
extracted_text = extract_text_from_pdf(pdf_path)
|
| 52 |
+
|
| 53 |
+
st.subheader("Extracted Report Text:")
|
| 54 |
+
st.text_area("Extracted Text", extracted_text[:1000], height=200)
|
| 55 |
+
|
| 56 |
+
if st.button("Analyze Report"):
|
| 57 |
+
with st.spinner("Analyzing..."):
|
| 58 |
+
analysis = analyze_health_data(extracted_text)
|
| 59 |
+
st.subheader("Health Analysis:")
|
| 60 |
+
st.write(analysis)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
main()
|