Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -84,7 +84,7 @@ def preprocess_image(image):
|
|
| 84 |
def predict_xray(image):
|
| 85 |
try:
|
| 86 |
if image is None:
|
| 87 |
-
return "Please upload an image."
|
| 88 |
|
| 89 |
img_tensor = preprocess_image(image)
|
| 90 |
with torch.no_grad():
|
|
@@ -103,10 +103,6 @@ def predict_xray(image):
|
|
| 103 |
<p><b>Confidence:</b> {confidence:.2f}%</p>
|
| 104 |
<p><b>Note:</b> The model is not confident enough to provide a clear diagnosis.</p>
|
| 105 |
<p><b>Recommendation:</b> Please consult a radiologist or upload a better-quality image.</p>
|
| 106 |
-
</div>, <div style="font-family:Arial">
|
| 107 |
-
<h4>Summary:</h4>
|
| 108 |
-
<p><b>Diagnosis:</b> Uncertain</p>
|
| 109 |
-
<p><b>Confidence Level:</b> {confidence:.2f}%</p>
|
| 110 |
</div>
|
| 111 |
"""
|
| 112 |
|
|
@@ -117,22 +113,17 @@ def predict_xray(image):
|
|
| 117 |
<p><b>Confidence:</b> {confidence:.2f}%</p>
|
| 118 |
<p><b>Description:</b> {info['description']}</p>
|
| 119 |
<p><b>Recommendation:</b> {info['recommendation']}</p>
|
| 120 |
-
</div>, <div style="font-family:Arial">
|
| 121 |
-
<h4>Summary:</h4>
|
| 122 |
-
<p><b>Diagnosis:</b> {top_condition}</p>
|
| 123 |
-
<p><b>Confidence Level:</b> {confidence:.2f}%</p>
|
| 124 |
-
<p><b>Recommendation:</b> {info['recommendation']}</p>
|
| 125 |
</div>
|
| 126 |
"""
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
logger.error(f"Error in prediction: {e}")
|
| 130 |
-
return f"Error: {str(e)}"
|
| 131 |
|
| 132 |
# Analyze PDF report
|
| 133 |
def analyze_report(file):
|
| 134 |
if not file or not file.name.endswith(".pdf"):
|
| 135 |
-
return "Please upload a valid PDF file."
|
| 136 |
try:
|
| 137 |
doc = fitz.open(file.name)
|
| 138 |
text = "".join(page.get_text() for page in doc)
|
|
@@ -148,10 +139,10 @@ def analyze_report(file):
|
|
| 148 |
condition, disease, status = "Fracture", "Bone Injury", "Orthopedic Attention Required"
|
| 149 |
|
| 150 |
preview = text[:300] + "..." if text else "No readable content."
|
| 151 |
-
return f"Condition: {condition}\nDisease: {disease}\nStatus: {status}\n\nPreview:\n{preview}
|
| 152 |
|
| 153 |
except Exception as e:
|
| 154 |
-
return f"Failed to process PDF: {str(e)}"
|
| 155 |
|
| 156 |
# Gradio interface
|
| 157 |
def create_interface():
|
|
@@ -160,17 +151,14 @@ def create_interface():
|
|
| 160 |
|
| 161 |
with gr.Tabs():
|
| 162 |
with gr.TabItem("X-ray Analysis"):
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
summary_output = gr.HTML(label="Summary Result")
|
| 167 |
-
gr.Button("Analyze X-ray", elem_id="analyze_button", scale=0.5).click(predict_xray, inputs=img_input, outputs=[img_output, summary_output])
|
| 168 |
|
| 169 |
with gr.TabItem("Report Analysis"):
|
| 170 |
pdf_input = gr.File(label="Upload PDF Report", file_types=[".pdf"])
|
| 171 |
-
pdf_output = gr.Textbox(label="Extracted Summary", lines=10)
|
| 172 |
summary_output_report = gr.Textbox(label="Summary Result", lines=5)
|
| 173 |
-
gr.Button("Analyze Report", elem_id="analyze_button", scale=0.5).click(analyze_report, inputs=pdf_input, outputs=
|
| 174 |
|
| 175 |
return demo
|
| 176 |
|
|
|
|
| 84 |
def predict_xray(image):
|
| 85 |
try:
|
| 86 |
if image is None:
|
| 87 |
+
return "Please upload an image."
|
| 88 |
|
| 89 |
img_tensor = preprocess_image(image)
|
| 90 |
with torch.no_grad():
|
|
|
|
| 103 |
<p><b>Confidence:</b> {confidence:.2f}%</p>
|
| 104 |
<p><b>Note:</b> The model is not confident enough to provide a clear diagnosis.</p>
|
| 105 |
<p><b>Recommendation:</b> Please consult a radiologist or upload a better-quality image.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
</div>
|
| 107 |
"""
|
| 108 |
|
|
|
|
| 113 |
<p><b>Confidence:</b> {confidence:.2f}%</p>
|
| 114 |
<p><b>Description:</b> {info['description']}</p>
|
| 115 |
<p><b>Recommendation:</b> {info['recommendation']}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
</div>
|
| 117 |
"""
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
logger.error(f"Error in prediction: {e}")
|
| 121 |
+
return f"Error: {str(e)}"
|
| 122 |
|
| 123 |
# Analyze PDF report
|
| 124 |
def analyze_report(file):
|
| 125 |
if not file or not file.name.endswith(".pdf"):
|
| 126 |
+
return "Please upload a valid PDF file."
|
| 127 |
try:
|
| 128 |
doc = fitz.open(file.name)
|
| 129 |
text = "".join(page.get_text() for page in doc)
|
|
|
|
| 139 |
condition, disease, status = "Fracture", "Bone Injury", "Orthopedic Attention Required"
|
| 140 |
|
| 141 |
preview = text[:300] + "..." if text else "No readable content."
|
| 142 |
+
return f"Condition: {condition}\nDisease: {disease}\nStatus: {status}\n\nPreview:\n{preview}"
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
+
return f"Failed to process PDF: {str(e)}"
|
| 146 |
|
| 147 |
# Gradio interface
|
| 148 |
def create_interface():
|
|
|
|
| 151 |
|
| 152 |
with gr.Tabs():
|
| 153 |
with gr.TabItem("X-ray Analysis"):
|
| 154 |
+
img_input = gr.Image(label="Upload Chest X-ray", type="pil")
|
| 155 |
+
summary_output = gr.HTML(label="Summary Result")
|
| 156 |
+
gr.Button("Analyze X-ray", elem_id="analyze_button", scale=0.5).click(predict_xray, inputs=img_input, outputs=summary_output)
|
|
|
|
|
|
|
| 157 |
|
| 158 |
with gr.TabItem("Report Analysis"):
|
| 159 |
pdf_input = gr.File(label="Upload PDF Report", file_types=[".pdf"])
|
|
|
|
| 160 |
summary_output_report = gr.Textbox(label="Summary Result", lines=5)
|
| 161 |
+
gr.Button("Analyze Report", elem_id="analyze_button", scale=0.5).click(analyze_report, inputs=pdf_input, outputs=summary_output_report)
|
| 162 |
|
| 163 |
return demo
|
| 164 |
|