Spaces:
Paused
Paused
Update tabs/report_analyzer.py
Browse files- tabs/report_analyzer.py +27 -15
tabs/report_analyzer.py
CHANGED
|
@@ -2,16 +2,17 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import fitz
|
| 4 |
import pytesseract
|
| 5 |
-
import pickle
|
| 6 |
import re
|
|
|
|
|
|
|
| 7 |
from PIL import Image, ImageEnhance, ImageFilter
|
| 8 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
#
|
| 15 |
translation_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
|
| 16 |
translation_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
|
| 17 |
translator = pipeline("translation", model=translation_model, tokenizer=translation_tokenizer)
|
|
@@ -96,6 +97,22 @@ def preprocess_image(image_path):
|
|
| 96 |
image = ImageEnhance.Contrast(image).enhance(2)
|
| 97 |
return image
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def ocr_and_explain(file, language):
|
| 100 |
if not file:
|
| 101 |
return "Please upload a valid report.", ""
|
|
@@ -132,25 +149,19 @@ def ocr_and_explain(file, language):
|
|
| 132 |
f"<i>Reference Range: {values['low']}-{values['high']} {values['unit']}</i><br><br>"
|
| 133 |
)
|
| 134 |
rule_lines.append(html_line)
|
| 135 |
-
|
| 136 |
cleaned_lines.append(f"{term}: {value:.2f} {values['unit']} → {status} (Normal: {values['low']}-{values['high']} {values['unit']})")
|
| 137 |
except:
|
| 138 |
continue
|
| 139 |
|
| 140 |
rule_explanation = "\n".join(rule_lines) if rule_lines else "No known lab terms detected."
|
| 141 |
-
simplified_prompt = "You are a medical assistant. Summarize the following lab test results:\n\n" + "\n".join(cleaned_lines[:6])
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
outputs = gpt_model.generate(**inputs, max_new_tokens=300)
|
| 146 |
-
gpt_summary = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 147 |
-
except Exception as e:
|
| 148 |
-
gpt_summary = f"(FLAN-T5 summarization failed: {e})"
|
| 149 |
|
| 150 |
final_output = (
|
| 151 |
"<h4 style='color:#ffa500;'>📌 Rule-Based Results:</h4><br>" +
|
| 152 |
rule_explanation +
|
| 153 |
-
"<hr><h4 style='color:#77dd77;'>🧠
|
| 154 |
gpt_summary
|
| 155 |
)
|
| 156 |
|
|
@@ -183,7 +194,8 @@ def report_analyzer_tab():
|
|
| 183 |
|
| 184 |
with gr.Column():
|
| 185 |
processing_status = gr.HTML()
|
| 186 |
-
output_box = gr.HTML("""<div style="background:#1e1e1e; padding:15px; border-radius:10px;">
|
|
|
|
| 187 |
output_explanation = gr.HTML()
|
| 188 |
output_close = gr.HTML("</div>")
|
| 189 |
|
|
|
|
| 2 |
import torch
|
| 3 |
import fitz
|
| 4 |
import pytesseract
|
|
|
|
| 5 |
import re
|
| 6 |
+
import os
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
from PIL import Image, ImageEnhance, ImageFilter
|
| 9 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 10 |
|
| 11 |
+
# Configure Gemini (PaLM) API
|
| 12 |
+
genai.configure(api_key=os.getenv("PALM_API_KEY"))
|
| 13 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 14 |
|
| 15 |
+
# Translation model (e.g., for Hindi)
|
| 16 |
translation_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
|
| 17 |
translation_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
|
| 18 |
translator = pipeline("translation", model=translation_model, tokenizer=translation_tokenizer)
|
|
|
|
| 97 |
image = ImageEnhance.Contrast(image).enhance(2)
|
| 98 |
return image
|
| 99 |
|
| 100 |
+
def summarize_with_gemini(cleaned_lines):
|
| 101 |
+
prompt = f"""
|
| 102 |
+
You are a medical assistant. Summarize this lab report in clear, simple language:
|
| 103 |
+
1. Summary in 2–3 lines
|
| 104 |
+
2. Explain abnormal values
|
| 105 |
+
3. List health concerns (if any) in bullet points
|
| 106 |
+
|
| 107 |
+
Data:
|
| 108 |
+
{chr(10).join(cleaned_lines[:6])}
|
| 109 |
+
"""
|
| 110 |
+
try:
|
| 111 |
+
response = model.generate_content(prompt)
|
| 112 |
+
return response.text.strip() if response and response.text else "(No summary returned)"
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return f"(Gemini summarization failed: {e})"
|
| 115 |
+
|
| 116 |
def ocr_and_explain(file, language):
|
| 117 |
if not file:
|
| 118 |
return "Please upload a valid report.", ""
|
|
|
|
| 149 |
f"<i>Reference Range: {values['low']}-{values['high']} {values['unit']}</i><br><br>"
|
| 150 |
)
|
| 151 |
rule_lines.append(html_line)
|
|
|
|
| 152 |
cleaned_lines.append(f"{term}: {value:.2f} {values['unit']} → {status} (Normal: {values['low']}-{values['high']} {values['unit']})")
|
| 153 |
except:
|
| 154 |
continue
|
| 155 |
|
| 156 |
rule_explanation = "\n".join(rule_lines) if rule_lines else "No known lab terms detected."
|
|
|
|
| 157 |
|
| 158 |
+
# 🔁 Gemini summary
|
| 159 |
+
gpt_summary = summarize_with_gemini(cleaned_lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
final_output = (
|
| 162 |
"<h4 style='color:#ffa500;'>📌 Rule-Based Results:</h4><br>" +
|
| 163 |
rule_explanation +
|
| 164 |
+
"<hr><h4 style='color:#77dd77;'>🧠 Gemini Summary:</h4><br>" +
|
| 165 |
gpt_summary
|
| 166 |
)
|
| 167 |
|
|
|
|
| 194 |
|
| 195 |
with gr.Column():
|
| 196 |
processing_status = gr.HTML()
|
| 197 |
+
output_box = gr.HTML("""<div style="background:#1e1e1e; padding:15px; border-radius:10px;">
|
| 198 |
+
<h4 style="color:#ffffff;">📋 Final Explanation Output</h4>""")
|
| 199 |
output_explanation = gr.HTML()
|
| 200 |
output_close = gr.HTML("</div>")
|
| 201 |
|