Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from torchvision import models, transforms
|
|
| 5 |
import fitz # PyMuPDF
|
| 6 |
import logging
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# Setup logging
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -109,28 +110,34 @@ def predict_xray(image):
|
|
| 109 |
logger.error(f"Error in prediction: {e}")
|
| 110 |
return f"Error: {str(e)}"
|
| 111 |
|
| 112 |
-
#
|
| 113 |
def analyze_report(file):
|
| 114 |
if not file or not file.name.endswith(".pdf"):
|
| 115 |
return "Please upload a valid PDF file."
|
|
|
|
| 116 |
try:
|
| 117 |
doc = fitz.open(file.name)
|
| 118 |
-
text = "".join(page.get_text() for page in doc)
|
| 119 |
doc.close()
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if
|
| 124 |
-
condition
|
| 125 |
-
elif
|
| 126 |
-
condition
|
| 127 |
-
elif
|
| 128 |
-
condition
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
preview = text[:300] + "..." if text else "No readable content."
|
| 131 |
-
return f"
|
| 132 |
|
| 133 |
except Exception as e:
|
|
|
|
| 134 |
return f"Failed to process PDF: {str(e)}"
|
| 135 |
|
| 136 |
# Gradio interface
|
|
|
|
| 5 |
import fitz # PyMuPDF
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
+
import re # For better pattern matching
|
| 9 |
|
| 10 |
# Setup logging
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 110 |
logger.error(f"Error in prediction: {e}")
|
| 111 |
return f"Error: {str(e)}"
|
| 112 |
|
| 113 |
+
# Enhanced PDF analysis function
|
| 114 |
def analyze_report(file):
|
| 115 |
if not file or not file.name.endswith(".pdf"):
|
| 116 |
return "Please upload a valid PDF file."
|
| 117 |
+
|
| 118 |
try:
|
| 119 |
doc = fitz.open(file.name)
|
| 120 |
+
text = "".join(page.get_text() for page in doc) # Extract all text from the PDF
|
| 121 |
doc.close()
|
| 122 |
|
| 123 |
+
# Improved keyword matching
|
| 124 |
+
condition = "Unclear"
|
| 125 |
+
if re.search(r'\bstroke\b', text, re.IGNORECASE):
|
| 126 |
+
condition = "Stroke"
|
| 127 |
+
elif re.search(r'\bcancer\b', text, re.IGNORECASE):
|
| 128 |
+
condition = "Cancer"
|
| 129 |
+
elif re.search(r'\bfracture\b', text, re.IGNORECASE):
|
| 130 |
+
condition = "Fracture"
|
| 131 |
+
elif re.search(r'\bpneumonia\b', text, re.IGNORECASE):
|
| 132 |
+
condition = "Pneumonia"
|
| 133 |
+
# Add more keyword matching conditions as needed
|
| 134 |
+
|
| 135 |
+
disease_status = f"Disease Identified: {condition}"
|
| 136 |
preview = text[:300] + "..." if text else "No readable content."
|
| 137 |
+
return f"{disease_status}\n\nPreview:\n{preview}"
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
+
logger.error(f"Failed to process PDF: {e}")
|
| 141 |
return f"Failed to process PDF: {str(e)}"
|
| 142 |
|
| 143 |
# Gradio interface
|