Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,203 +1,462 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
import numpy as np
|
| 6 |
-
from PIL import Image
|
| 7 |
-
from reportlab.lib.pagesizes import letter
|
| 8 |
-
from reportlab.pdfgen import canvas
|
| 9 |
-
from reportlab.lib import colors
|
| 10 |
-
from reportlab.platypus import Table, TableStyle
|
| 11 |
-
import requests
|
| 12 |
-
import smtplib
|
| 13 |
-
from email.mime.multipart import MIMEMultipart
|
| 14 |
-
from email.mime.text import MIMEText
|
| 15 |
-
from email.mime.base import MIMEBase
|
| 16 |
-
from email import encoders
|
| 17 |
import io
|
|
|
|
| 18 |
import base64
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
try:
|
| 159 |
-
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
|
| 160 |
-
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
| 161 |
-
server.sendmail(SENDER_EMAIL, email, msg.as_string())
|
| 162 |
-
print(f"Report sent successfully to {email}")
|
| 163 |
-
return report_path # Return path for auto-download
|
| 164 |
except Exception as e:
|
| 165 |
-
return f"
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
generate_report,
|
| 197 |
-
inputs=[name, age, gender, weight, height, allergies, cause, xray, email],
|
| 198 |
-
outputs=[output_file],
|
| 199 |
-
)
|
| 200 |
|
| 201 |
-
# Launch the Gradio app
|
| 202 |
if __name__ == "__main__":
|
| 203 |
-
app.
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import io
|
| 7 |
+
import uvicorn
|
| 8 |
import base64
|
| 9 |
+
import random
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Loading the models
|
| 14 |
+
def load_models():
|
| 15 |
+
return {
|
| 16 |
+
"BoneEye": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
|
| 17 |
+
"BoneGuard": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray"),
|
| 18 |
+
"XRayMaster": pipeline("image-classification",
|
| 19 |
+
model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388")
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
models = load_models()
|
| 23 |
+
|
| 24 |
+
def translate_label(label):
|
| 25 |
+
translations = {
|
| 26 |
+
"fracture": "Fracture",
|
| 27 |
+
"no fracture": "No Fracture",
|
| 28 |
+
"normal": "Normal",
|
| 29 |
+
"abnormal": "Abnormal",
|
| 30 |
+
"F1": "Fracture",
|
| 31 |
+
"NF": "No Fracture"
|
| 32 |
+
}
|
| 33 |
+
return translations.get(label.lower(), label)
|
| 34 |
+
|
| 35 |
+
def create_heatmap_overlay(image, box, score):
|
| 36 |
+
overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
| 37 |
+
draw = ImageDraw.Draw(overlay)
|
| 38 |
+
|
| 39 |
+
x1, y1 = box['xmin'], box['ymin']
|
| 40 |
+
x2, y2 = box['xmax'], box['ymax']
|
| 41 |
+
|
| 42 |
+
if score > 0.8:
|
| 43 |
+
fill_color = (255, 0, 0, 100)
|
| 44 |
+
border_color = (255, 0, 0, 255)
|
| 45 |
+
elif score > 0.6:
|
| 46 |
+
fill_color = (255, 165, 0, 100)
|
| 47 |
+
border_color = (255, 165, 0, 255)
|
| 48 |
+
else:
|
| 49 |
+
fill_color = (255, 255, 0, 100)
|
| 50 |
+
border_color = (255, 255, 0, 255)
|
| 51 |
+
|
| 52 |
+
draw.rectangle([x1, y1, x2, y2], fill=fill_color)
|
| 53 |
+
draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
|
| 54 |
+
|
| 55 |
+
return overlay
|
| 56 |
+
|
| 57 |
+
def draw_boxes(image, predictions):
|
| 58 |
+
result_image = image.copy().convert('RGBA')
|
| 59 |
+
|
| 60 |
+
for pred in predictions:
|
| 61 |
+
box = pred['box']
|
| 62 |
+
score = pred['score']
|
| 63 |
+
|
| 64 |
+
overlay = create_heatmap_overlay(image, box, score)
|
| 65 |
+
result_image = Image.alpha_composite(result_image, overlay)
|
| 66 |
+
|
| 67 |
+
draw = ImageDraw.Draw(result_image)
|
| 68 |
+
temp = 36.5 + (score * 2.5)
|
| 69 |
+
label = f"{translate_label(pred['label'])} ({score:.1%} • {temp:.1f}°C)"
|
| 70 |
+
|
| 71 |
+
text_bbox = draw.textbbox((box['xmin'], box['ymin']-20), label)
|
| 72 |
+
draw.rectangle(text_bbox, fill=(0, 0, 0, 180))
|
| 73 |
+
|
| 74 |
+
draw.text(
|
| 75 |
+
(box['xmin'], box['ymin']-20),
|
| 76 |
+
label,
|
| 77 |
+
fill=(255, 255, 255, 255)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
return result_image
|
| 81 |
+
|
| 82 |
+
def image_to_base64(image):
|
| 83 |
+
buffered = io.BytesIO()
|
| 84 |
+
image.save(buffered, format="PNG")
|
| 85 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 86 |
+
return f"data:image/png;base64,{img_str}"
|
| 87 |
+
|
| 88 |
+
COMMON_STYLES = """
|
| 89 |
+
body {
|
| 90 |
+
font-family: system-ui, -apple-system, sans-serif;
|
| 91 |
+
background: #f0f2f5;
|
| 92 |
+
margin: 0;
|
| 93 |
+
padding: 20px;
|
| 94 |
+
color: #1a1a1a;
|
| 95 |
+
}
|
| 96 |
+
::-webkit-scrollbar {
|
| 97 |
+
width: 8px;
|
| 98 |
+
height: 8px;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
::-webkit-scrollbar-track {
|
| 102 |
+
background: transparent;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
::-webkit-scrollbar-thumb {
|
| 106 |
+
background-color: rgba(156, 163, 175, 0.5);
|
| 107 |
+
border-radius: 4px;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
.container {
|
| 111 |
+
max-width: 1200px;
|
| 112 |
+
margin: 0 auto;
|
| 113 |
+
background: white;
|
| 114 |
+
padding: 20px;
|
| 115 |
+
border-radius: 10px;
|
| 116 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 117 |
+
}
|
| 118 |
+
.button {
|
| 119 |
+
background: #2d2d2d;
|
| 120 |
+
color: white;
|
| 121 |
+
border: none;
|
| 122 |
+
padding: 12px 30px;
|
| 123 |
+
border-radius: 8px;
|
| 124 |
+
cursor: pointer;
|
| 125 |
+
font-size: 1.1em;
|
| 126 |
+
transition: all 0.3s ease;
|
| 127 |
+
position: relative;
|
| 128 |
+
}
|
| 129 |
+
.button:hover {
|
| 130 |
+
background: #404040;
|
| 131 |
+
}
|
| 132 |
+
@keyframes progress {
|
| 133 |
+
0% { width: 0; }
|
| 134 |
+
100% { width: 100%; }
|
| 135 |
+
}
|
| 136 |
+
.button-progress {
|
| 137 |
+
position: absolute;
|
| 138 |
+
bottom: 0;
|
| 139 |
+
left: 0;
|
| 140 |
+
height: 4px;
|
| 141 |
+
background: rgba(255, 255, 255, 0.5);
|
| 142 |
+
width: 0;
|
| 143 |
+
}
|
| 144 |
+
.button:active .button-progress {
|
| 145 |
+
animation: progress 2s linear forwards;
|
| 146 |
+
}
|
| 147 |
+
img {
|
| 148 |
+
max-width: 100%;
|
| 149 |
+
height: auto;
|
| 150 |
+
border-radius: 8px;
|
| 151 |
+
}
|
| 152 |
+
@keyframes blink {
|
| 153 |
+
0% { opacity: 1; }
|
| 154 |
+
50% { opacity: 0; }
|
| 155 |
+
100% { opacity: 1; }
|
| 156 |
+
}
|
| 157 |
+
#loading {
|
| 158 |
+
display: none;
|
| 159 |
+
color: white;
|
| 160 |
+
margin-top: 10px;
|
| 161 |
+
animation: blink 1s infinite;
|
| 162 |
+
text-align: center;
|
| 163 |
+
}
|
| 164 |
+
"""
|
| 165 |
+
|
| 166 |
+
SAMPLE_IMAGES = [
|
| 167 |
+
{"id": "sample1", "filename": "sample1.png", "label": "Fracture"},
|
| 168 |
+
{"id": "sample2", "filename": "sample2.png", "label": "No Fracture"},
|
| 169 |
+
{"id": "sample3", "filename": "sample3.png", "label": "Fracture"},
|
| 170 |
+
{"id": "sample4", "filename": "sample4.png", "label": "No Fracture"},
|
| 171 |
+
{"id": "sample5", "filename": "sample5.png", "label": "Fracture"},
|
| 172 |
+
{"id": "sample6", "filename": "sample6.png", "label": "No Fracture"},
|
| 173 |
+
{"id": "sample7", "filename": "sample7.png", "label": "Fracture"},
|
| 174 |
+
{"id": "sample8", "filename": "sample8.png", "label": "No Fracture"},
|
| 175 |
+
{"id": "sample9", "filename": "sample9.png", "label": "Fracture"},
|
| 176 |
+
{"id": "sample10", "filename": "sample10.png", "label": "No Fracture"},
|
| 177 |
+
]
|
| 178 |
+
|
| 179 |
+
@app.get("/", response_class=HTMLResponse)
|
| 180 |
+
async def main():
|
| 181 |
+
image_options = "".join(
|
| 182 |
+
f'<option value="{img["filename"]}">{img["id"]}</option>' for img in SAMPLE_IMAGES
|
| 183 |
+
)
|
| 184 |
+
content = f"""
|
| 185 |
+
<!DOCTYPE html>
|
| 186 |
+
<html>
|
| 187 |
+
<head>
|
| 188 |
+
<title>Fracture Detection</title>
|
| 189 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 190 |
+
<style>
|
| 191 |
+
{COMMON_STYLES}
|
| 192 |
+
|
| 193 |
+
.upload-section {{
|
| 194 |
+
background: #2d2d2d;
|
| 195 |
+
padding: 40px;
|
| 196 |
+
border-radius: 12px;
|
| 197 |
+
margin: 20px 0;
|
| 198 |
+
text-align: center;
|
| 199 |
+
border: 2px dashed #404040;
|
| 200 |
+
transition: all 0.3s ease;
|
| 201 |
+
color: white;
|
| 202 |
+
}}
|
| 203 |
+
.upload-section:hover {{
|
| 204 |
+
border-color: #555;
|
| 205 |
+
}}
|
| 206 |
+
.image-selection {{
|
| 207 |
+
font-size: 1.1em;
|
| 208 |
+
margin: 20px 0;
|
| 209 |
+
color: white;
|
| 210 |
+
}}
|
| 211 |
+
select {{
|
| 212 |
+
padding: 10px;
|
| 213 |
+
border-radius: 8px;
|
| 214 |
+
border: 1px solid #404040;
|
| 215 |
+
background: #2d2d2d;
|
| 216 |
+
color: white;
|
| 217 |
+
transition: all 0.3s ease;
|
| 218 |
+
cursor: pointer;
|
| 219 |
+
font-size: 1em;
|
| 220 |
+
}}
|
| 221 |
+
select:hover {{
|
| 222 |
+
background: #404040;
|
| 223 |
+
}}
|
| 224 |
+
.confidence-slider {{
|
| 225 |
+
width: 100%;
|
| 226 |
+
max-width: 300px;
|
| 227 |
+
margin: 20px auto;
|
| 228 |
+
}}
|
| 229 |
+
input[type="range"] {{
|
| 230 |
+
width: 100%;
|
| 231 |
+
height: 8px;
|
| 232 |
+
border-radius: 4px;
|
| 233 |
+
background: #404040;
|
| 234 |
+
outline: none;
|
| 235 |
+
transition: all 0.3s ease;
|
| 236 |
+
-webkit-appearance: none;
|
| 237 |
+
}}
|
| 238 |
+
input[type="range"]::-webkit-slider-thumb {{
|
| 239 |
+
-webkit-appearance: none;
|
| 240 |
+
width: 20px;
|
| 241 |
+
height: 20px;
|
| 242 |
+
border-radius: 50%;
|
| 243 |
+
background: white;
|
| 244 |
+
cursor: pointer;
|
| 245 |
+
border: none;
|
| 246 |
+
}}
|
| 247 |
+
</style>
|
| 248 |
+
</head>
|
| 249 |
+
<body>
|
| 250 |
+
<div class="container">
|
| 251 |
+
<div class="upload-section">
|
| 252 |
+
<form action="/analyze" method="post" enctype="multipart/form-data" onsubmit="document.getElementById('loading').style.display = 'block';">
|
| 253 |
+
<div class="image-selection">
|
| 254 |
+
<label for="sample_image">Select a Sample X-ray:</label>
|
| 255 |
+
<select name="sample_image" id="sample_image">
|
| 256 |
+
{image_options}
|
| 257 |
+
</select>
|
| 258 |
+
</div>
|
| 259 |
+
<div class="confidence-slider">
|
| 260 |
+
<label for="threshold">Confidence Threshold: <span id="thresholdValue">0.60</span></label>
|
| 261 |
+
<input type="range" id="threshold" name="threshold"
|
| 262 |
+
min="0" max="1" step="0.05" value="0.60"
|
| 263 |
+
oninput="document.getElementById('thresholdValue').textContent = parseFloat(this.value).toFixed(2)">
|
| 264 |
+
</div>
|
| 265 |
+
<button type="submit" class="button">
|
| 266 |
+
Analyze
|
| 267 |
+
<div class="button-progress"></div>
|
| 268 |
+
</button>
|
| 269 |
+
<div id="loading">Loading...</div>
|
| 270 |
+
</form>
|
| 271 |
+
</div>
|
| 272 |
+
</div>
|
| 273 |
+
</body>
|
| 274 |
+
</html>
|
| 275 |
+
"""
|
| 276 |
+
return content
|
| 277 |
+
|
| 278 |
+
@app.post("/analyze", response_class=HTMLResponse)
|
| 279 |
+
async def analyze_file(sample_image: str, threshold: float = 0.60):
|
| 280 |
try:
|
| 281 |
+
# For now, let's just pick a random sample image for demonstration.
|
| 282 |
+
# Replace this with actual loading of the selected image.
|
| 283 |
+
# You will need to place the sample images in a directory (e.g., 'sample_images')
|
| 284 |
+
image_path = f"sample_images/{sample_image}"
|
| 285 |
+
try:
|
| 286 |
+
image = Image.open(image_path)
|
| 287 |
+
except FileNotFoundError:
|
| 288 |
+
return f"""
|
| 289 |
+
<!DOCTYPE html>
|
| 290 |
+
<html>
|
| 291 |
+
<head>
|
| 292 |
+
<title>Error</title>
|
| 293 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 294 |
+
<style>
|
| 295 |
+
{COMMON_STYLES}
|
| 296 |
+
.error-box {{
|
| 297 |
+
background: #fee2e2;
|
| 298 |
+
border: 1px solid #ef4444;
|
| 299 |
+
padding: 20px;
|
| 300 |
+
border-radius: 8px;
|
| 301 |
+
margin: 20px 0;
|
| 302 |
+
}}
|
| 303 |
+
</style>
|
| 304 |
+
</head>
|
| 305 |
+
<body>
|
| 306 |
+
<div class="container">
|
| 307 |
+
<div class="error-box">
|
| 308 |
+
<h3>Error</h3>
|
| 309 |
+
<p>Sample image '{sample_image}' not found. Please ensure the image exists in the 'sample_images' directory.</p>
|
| 310 |
+
</div>
|
| 311 |
+
<a href="/" class="button back-button">
|
| 312 |
+
← Back
|
| 313 |
+
<div class="button-progress"></div>
|
| 314 |
+
</a>
|
| 315 |
+
</div>
|
| 316 |
+
</body>
|
| 317 |
+
</html>
|
| 318 |
+
"""
|
| 319 |
+
|
| 320 |
+
predictions_watcher = models["BoneGuard"](image)
|
| 321 |
+
predictions_master = models["XRayMaster"](image)
|
| 322 |
+
predictions_locator = models["BoneEye"](image)
|
| 323 |
+
|
| 324 |
+
filtered_preds = [p for p in predictions_locator if p['score'] >= threshold]
|
| 325 |
+
if filtered_preds:
|
| 326 |
+
result_image = draw_boxes(image, filtered_preds)
|
| 327 |
else:
|
| 328 |
+
result_image = image
|
| 329 |
+
|
| 330 |
+
# Logic to make fractured area black will be implemented here once images and fracture data are available.
|
| 331 |
+
# For demonstration purposes, let's just mark the detected areas.
|
| 332 |
+
|
| 333 |
+
result_image_b64 = image_to_base64(result_image)
|
| 334 |
+
|
| 335 |
+
results_html = f"""
|
| 336 |
+
<!DOCTYPE html>
|
| 337 |
+
<html>
|
| 338 |
+
<head>
|
| 339 |
+
<title>Results</title>
|
| 340 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 341 |
+
<style>
|
| 342 |
+
{COMMON_STYLES}
|
| 343 |
+
|
| 344 |
+
.results-grid {{
|
| 345 |
+
display: grid;
|
| 346 |
+
grid-template-columns: 1fr 1fr;
|
| 347 |
+
gap: 20px;
|
| 348 |
+
margin-top: 20px;
|
| 349 |
+
}}
|
| 350 |
+
.result-box {{
|
| 351 |
+
background: white;
|
| 352 |
+
padding: 20px;
|
| 353 |
+
border-radius: 12px;
|
| 354 |
+
margin: 10px 0;
|
| 355 |
+
border: 1px solid #e9ecef;
|
| 356 |
+
}}
|
| 357 |
+
.score-high {{
|
| 358 |
+
color: #0066cc;
|
| 359 |
+
font-weight: bold;
|
| 360 |
+
}}
|
| 361 |
+
|
| 362 |
+
.score-medium {{
|
| 363 |
+
color: #ffa500;
|
| 364 |
+
font-weight: bold;
|
| 365 |
+
}}
|
| 366 |
+
.back-button {{
|
| 367 |
+
display: inline-block;
|
| 368 |
+
text-decoration: none;
|
| 369 |
+
margin-top: 20px;
|
| 370 |
+
}}
|
| 371 |
+
h3 {{
|
| 372 |
+
color: #0066cc;
|
| 373 |
+
margin-top: 0;
|
| 374 |
+
}}
|
| 375 |
+
@media (max-width: 768px) {{
|
| 376 |
+
.results-grid {{
|
| 377 |
+
grid-template-columns: 1fr;
|
| 378 |
+
}}
|
| 379 |
+
}}
|
| 380 |
+
</style>
|
| 381 |
+
</head>
|
| 382 |
+
<body>
|
| 383 |
+
<div class="container">
|
| 384 |
+
<div class="results-grid">
|
| 385 |
+
<div>
|
| 386 |
+
<div class="result-box"><h3>BoneGuard</h3>
|
| 387 |
+
"""
|
| 388 |
+
|
| 389 |
+
for pred in predictions_watcher:
|
| 390 |
+
confidence_class = "score-high" if pred['score'] > 0.7 else "score-medium"
|
| 391 |
+
results_html += f"""
|
| 392 |
+
<div>
|
| 393 |
+
<span class="{confidence_class}">{pred['score']:.1%}</span> -
|
| 394 |
+
{translate_label(pred['label'])}
|
| 395 |
+
</div>
|
| 396 |
+
"""
|
| 397 |
+
results_html += "</div>"
|
| 398 |
+
|
| 399 |
+
results_html += "<div class='result-box'><h3>XRayMaster</h3>"
|
| 400 |
+
for pred in predictions_master:
|
| 401 |
+
confidence_class = "score-high" if pred['score'] > 0.7 else "score-medium"
|
| 402 |
+
results_html += f"""
|
| 403 |
+
<div>
|
| 404 |
+
<span class="{confidence_class}">{pred['score']:.1%}</span> -
|
| 405 |
+
{translate_label(pred['label'])}
|
| 406 |
+
</div>
|
| 407 |
+
"""
|
| 408 |
+
results_html += "</div></div>"
|
| 409 |
+
|
| 410 |
+
results_html += f"""
|
| 411 |
+
<div class='result-box'>
|
| 412 |
+
<h3>Fracture Localization</h3>
|
| 413 |
+
<img src="{result_image_b64}" alt="Analyzed image">
|
| 414 |
+
</div>
|
| 415 |
+
</div>
|
| 416 |
+
|
| 417 |
+
<a href="/" class="button back-button">
|
| 418 |
+
← Back
|
| 419 |
+
<div class="button-progress"></div>
|
| 420 |
+
</a>
|
| 421 |
+
</div>
|
| 422 |
+
</body>
|
| 423 |
+
</html>
|
| 424 |
+
"""
|
| 425 |
+
|
| 426 |
+
return results_html
|
| 427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
except Exception as e:
|
| 429 |
+
return f"""
|
| 430 |
+
<!DOCTYPE html>
|
| 431 |
+
<html>
|
| 432 |
+
<head>
|
| 433 |
+
<title>Error</title>
|
| 434 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 435 |
+
<style>
|
| 436 |
+
{COMMON_STYLES}
|
| 437 |
+
.error-box {{
|
| 438 |
+
background: #fee2e2;
|
| 439 |
+
border: 1px solid #ef4444;
|
| 440 |
+
padding: 20px;
|
| 441 |
+
border-radius: 8px;
|
| 442 |
+
margin: 20px 0;
|
| 443 |
+
}}
|
| 444 |
+
</style>
|
| 445 |
+
</head>
|
| 446 |
+
<body>
|
| 447 |
+
<div class="container">
|
| 448 |
+
<div class="error-box">
|
| 449 |
+
<h3>Error</h3>
|
| 450 |
+
<p>{str(e)}</p>
|
| 451 |
+
</div>
|
| 452 |
+
<a href="/" class="button back-button">
|
| 453 |
+
← Back
|
| 454 |
+
<div class="button-progress"></div>
|
| 455 |
+
</a>
|
| 456 |
+
</div>
|
| 457 |
+
</body>
|
| 458 |
+
</html>
|
| 459 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
|
|
|
|
| 461 |
if __name__ == "__main__":
|
| 462 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|