Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ condition_details = {
|
|
| 26 |
"Normal": {"description": "No abnormal signs detected.", "recommendation": "Routine check-ups recommended."},
|
| 27 |
"Pneumonia": {"description": "Lung inflammation detected, possibly infectious.", "recommendation": "Seek medical attention for treatment."},
|
| 28 |
"Cancer": {"description": "Suspicious masses suggest cancer; further imaging needed.", "recommendation": "Consult an oncologist."},
|
| 29 |
-
"TB": {"description": "Cavitary lesions indicate tuberculosis.", "recommendation": "Immediate medical evaluation required."},
|
| 30 |
"Other": {"description": "Unclear abnormality; further investigation needed.", "recommendation": "Consult a radiologist."},
|
| 31 |
"Coronary Artery Disease": {"description": "Narrowing of coronary arteries detected.", "recommendation": "Cardiology consultation required."},
|
| 32 |
"Aortic Aneurysm": {"description": "Abnormal enlargement of the aorta.", "recommendation": "Vascular surgery evaluation."},
|
|
@@ -41,7 +41,7 @@ condition_details = {
|
|
| 41 |
"Pulmonary Embolism": {"description": "Blockage in pulmonary arteries.", "recommendation": "Urgent medical attention."},
|
| 42 |
"Fractures": {"description": "Bone break detected.", "recommendation": "Orthopedic evaluation."},
|
| 43 |
"Arthritis": {"description": "Joint inflammation detected.", "recommendation": "Rheumatology consultation."},
|
| 44 |
-
"Osteoporosis": {"description": "Reduced bone density observed.", "recommendation
|
| 45 |
"Appendicitis": {"description": "Inflammation of the appendix.", "recommendation": "Surgical evaluation."},
|
| 46 |
"Gallstones": {"description": "Stones in the gallbladder detected.", "recommendation": "Gastroenterology consultation."},
|
| 47 |
"Kidney Stones": {"description": "Stones in the kidneys detected.", "recommendation": "Urology consultation."},
|
|
@@ -110,7 +110,7 @@ def predict_xray(image):
|
|
| 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."
|
|
@@ -120,21 +120,74 @@ def analyze_report(file):
|
|
| 120 |
text = "".join(page.get_text() for page in doc) # Extract all text from the PDF
|
| 121 |
doc.close()
|
| 122 |
|
| 123 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
condition = "Unclear"
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = "
|
| 131 |
elif re.search(r'\bpneumonia\b', text, re.IGNORECASE):
|
| 132 |
condition = "Pneumonia"
|
| 133 |
-
# Add more
|
| 134 |
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
logger.error(f"Failed to process PDF: {e}")
|
|
@@ -155,7 +208,7 @@ def create_interface():
|
|
| 155 |
|
| 156 |
with gr.TabItem("Report Analysis"):
|
| 157 |
pdf_input = gr.File(label="Upload PDF Report", file_types=[".pdf"])
|
| 158 |
-
summary_output_report = gr.
|
| 159 |
with gr.Row():
|
| 160 |
gr.Button("Analyze Report", elem_id="analyze_button", scale=0.3).click(analyze_report, inputs=pdf_input, outputs=summary_output_report)
|
| 161 |
gr.Button("Clear", elem_id="clear_button", scale=0.3).click(lambda: [None, ""], inputs=None, outputs=[pdf_input, summary_output_report])
|
|
@@ -164,4 +217,4 @@ def create_interface():
|
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
demo = create_interface()
|
| 167 |
-
demo.launch(server_port=7860, ssr_mode=False)
|
|
|
|
| 26 |
"Normal": {"description": "No abnormal signs detected.", "recommendation": "Routine check-ups recommended."},
|
| 27 |
"Pneumonia": {"description": "Lung inflammation detected, possibly infectious.", "recommendation": "Seek medical attention for treatment."},
|
| 28 |
"Cancer": {"description": "Suspicious masses suggest cancer; further imaging needed.", "recommendation": "Consult an oncologist."},
|
| 29 |
+
"TB": {"description": "Cavitary lesions or nodular opacities indicate tuberculosis.", "recommendation": "Immediate medical evaluation and anti-TB therapy required."},
|
| 30 |
"Other": {"description": "Unclear abnormality; further investigation needed.", "recommendation": "Consult a radiologist."},
|
| 31 |
"Coronary Artery Disease": {"description": "Narrowing of coronary arteries detected.", "recommendation": "Cardiology consultation required."},
|
| 32 |
"Aortic Aneurysm": {"description": "Abnormal enlargement of the aorta.", "recommendation": "Vascular surgery evaluation."},
|
|
|
|
| 41 |
"Pulmonary Embolism": {"description": "Blockage in pulmonary arteries.", "recommendation": "Urgent medical attention."},
|
| 42 |
"Fractures": {"description": "Bone break detected.", "recommendation": "Orthopedic evaluation."},
|
| 43 |
"Arthritis": {"description": "Joint inflammation detected.", "recommendation": "Rheumatology consultation."},
|
| 44 |
+
"Osteoporosis": {"description": "Reduced bone density observed.", "recommendation: Bone health specialist consultation."},
|
| 45 |
"Appendicitis": {"description": "Inflammation of the appendix.", "recommendation": "Surgical evaluation."},
|
| 46 |
"Gallstones": {"description": "Stones in the gallbladder detected.", "recommendation": "Gastroenterology consultation."},
|
| 47 |
"Kidney Stones": {"description": "Stones in the kidneys detected.", "recommendation": "Urology consultation."},
|
|
|
|
| 110 |
logger.error(f"Error in prediction: {e}")
|
| 111 |
return f"Error: {str(e)}"
|
| 112 |
|
| 113 |
+
# Enhanced PDF analysis function to extract patient details and disease
|
| 114 |
def analyze_report(file):
|
| 115 |
if not file or not file.name.endswith(".pdf"):
|
| 116 |
return "Please upload a valid PDF file."
|
|
|
|
| 120 |
text = "".join(page.get_text() for page in doc) # Extract all text from the PDF
|
| 121 |
doc.close()
|
| 122 |
|
| 123 |
+
# Initialize variables for patient details and condition
|
| 124 |
+
patient_info = {
|
| 125 |
+
"Name": "Not found",
|
| 126 |
+
"Age": "Not found",
|
| 127 |
+
"Gender": "Not found",
|
| 128 |
+
"Medical Record Number": "Not found",
|
| 129 |
+
"Date of Exam": "Not found"
|
| 130 |
+
}
|
| 131 |
condition = "Unclear"
|
| 132 |
+
confidence = "Not specified"
|
| 133 |
+
|
| 134 |
+
# Extract patient details using regex patterns
|
| 135 |
+
name_match = re.search(r'Name:\s*([^\n]+)', text, re.IGNORECASE)
|
| 136 |
+
if name_match:
|
| 137 |
+
patient_info["Name"] = name_match.group(1).strip()
|
| 138 |
+
|
| 139 |
+
age_match = re.search(r'Age:\s*(\d+)', text, re.IGNORECASE)
|
| 140 |
+
if age_match:
|
| 141 |
+
patient_info["Age"] = age_match.group(1).strip()
|
| 142 |
+
|
| 143 |
+
gender_match = re.search(r'Gender:\s*(Male|Female|Other)', text, re.IGNORECASE)
|
| 144 |
+
if gender_match:
|
| 145 |
+
patient_info["Gender"] = gender_match.group(1).strip()
|
| 146 |
+
|
| 147 |
+
mrn_match = re.search(r'Medical Record Number:\s*([^\n]+)', text, re.IGNORECASE)
|
| 148 |
+
if mrn_match:
|
| 149 |
+
patient_info["Medical Record Number"] = mrn_match.group(1).strip()
|
| 150 |
+
|
| 151 |
+
date_match = re.search(r'Date of Exam:\s*([^\n]+)', text, re.IGNORECASE)
|
| 152 |
+
if date_match:
|
| 153 |
+
patient_info["Date of Exam"] = date_match.group(1).strip()
|
| 154 |
+
|
| 155 |
+
# Improved condition matching with regex
|
| 156 |
+
if re.search(r'\btuberculosis\b|\bTB\b', text, re.IGNORECASE):
|
| 157 |
+
condition = "TB"
|
| 158 |
+
# Look for percentage of lung involvement
|
| 159 |
+
percentage_match = re.search(r'lung involvement:\s*approximately\s*(\d+)%', text, re.IGNORECASE)
|
| 160 |
+
if percentage_match:
|
| 161 |
+
confidence = f"{percentage_match.group(1)}% lung involvement"
|
| 162 |
+
elif re.search(r'\bstroke\b', text, re.IGNORECASE):
|
| 163 |
condition = "Stroke"
|
| 164 |
elif re.search(r'\bcancer\b', text, re.IGNORECASE):
|
| 165 |
condition = "Cancer"
|
| 166 |
elif re.search(r'\bfracture\b', text, re.IGNORECASE):
|
| 167 |
+
condition = "Fractures"
|
| 168 |
elif re.search(r'\bpneumonia\b', text, re.IGNORECASE):
|
| 169 |
condition = "Pneumonia"
|
| 170 |
+
# Add more conditions as needed from the conditions list
|
| 171 |
|
| 172 |
+
# Fetch condition details
|
| 173 |
+
info = condition_details.get(condition, condition_details["Other"])
|
| 174 |
+
|
| 175 |
+
# Construct summary output
|
| 176 |
+
summary = f"""
|
| 177 |
+
<div style="font-family:Arial">
|
| 178 |
+
<h3>Summary</h3>
|
| 179 |
+
<p><b>Disease Identified:</b> {condition}</p>
|
| 180 |
+
<p><b>Cause/Status:</b> {info['description']} {f'({confidence})' if confidence != 'Not specified' else ''}</p>
|
| 181 |
+
<p><b>Treatment/Recommendation:</b> {info['recommendation']}</p>
|
| 182 |
+
<h4>Patient Details</h4>
|
| 183 |
+
<p><b>Name:</b> {patient_info['Name']}</p>
|
| 184 |
+
<p><b>Age:</b> {patient_info['Age']}</p>
|
| 185 |
+
<p><b>Gender:</b> {patient_info['Gender']}</p>
|
| 186 |
+
<p><b>Medical Record Number:</b> {patient_info['Medical Record Number']}</p>
|
| 187 |
+
<p><b>Date of Exam:</b> {patient_info['Date of Exam']}</p>
|
| 188 |
+
</div>
|
| 189 |
+
"""
|
| 190 |
+
return summary
|
| 191 |
|
| 192 |
except Exception as e:
|
| 193 |
logger.error(f"Failed to process PDF: {e}")
|
|
|
|
| 208 |
|
| 209 |
with gr.TabItem("Report Analysis"):
|
| 210 |
pdf_input = gr.File(label="Upload PDF Report", file_types=[".pdf"])
|
| 211 |
+
summary_output_report = gr.HTML(label="Summary Result") # Changed to HTML for consistent formatting
|
| 212 |
with gr.Row():
|
| 213 |
gr.Button("Analyze Report", elem_id="analyze_button", scale=0.3).click(analyze_report, inputs=pdf_input, outputs=summary_output_report)
|
| 214 |
gr.Button("Clear", elem_id="clear_button", scale=0.3).click(lambda: [None, ""], inputs=None, outputs=[pdf_input, summary_output_report])
|
|
|
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|
| 219 |
demo = create_interface()
|
| 220 |
+
demo.launch(server_port=7860, ssr_mode=False)
|