Update app.py
Browse files
app.py
CHANGED
|
@@ -4,120 +4,27 @@ from PIL import Image, ImageDraw
|
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="Fraktur Detektion",
|
| 10 |
layout="wide",
|
| 11 |
-
initial_sidebar_state="collapsed"
|
| 12 |
-
menu_items=None
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
st.markdown("""
|
| 17 |
-
<style>
|
| 18 |
-
.stApp {
|
| 19 |
-
background: #f0f2f5 !important;
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
.block-container {
|
| 23 |
-
padding-top: 0 !important;
|
| 24 |
-
padding-bottom: 0 !important;
|
| 25 |
-
max-width: 1400px !important;
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
.upload-container {
|
| 29 |
-
background: white;
|
| 30 |
-
padding: 1.5rem;
|
| 31 |
-
border-radius: 10px;
|
| 32 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 33 |
-
margin-bottom: 1rem;
|
| 34 |
-
text-align: center;
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
.results-container {
|
| 38 |
-
background: white;
|
| 39 |
-
padding: 1.5rem;
|
| 40 |
-
border-radius: 10px;
|
| 41 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
.result-box {
|
| 45 |
-
background: #f8f9fa;
|
| 46 |
-
padding: 0.75rem;
|
| 47 |
-
border-radius: 8px;
|
| 48 |
-
margin: 0.5rem 0;
|
| 49 |
-
border: 1px solid #e9ecef;
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
h1, h2, h3, h4, p {
|
| 53 |
-
color: #1a1a1a !important;
|
| 54 |
-
margin: 0.5rem 0 !important;
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
.stImage {
|
| 58 |
-
background: white;
|
| 59 |
-
padding: 0.5rem;
|
| 60 |
-
border-radius: 8px;
|
| 61 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
.stImage > img {
|
| 65 |
-
max-height: 300px !important;
|
| 66 |
-
width: auto !important;
|
| 67 |
-
margin: 0 auto !important;
|
| 68 |
-
display: block !important;
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
[data-testid="stFileUploader"] {
|
| 72 |
-
width: 100% !important;
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
.stFileUploaderFileName {
|
| 76 |
-
color: #1a1a1a !important;
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
.stButton > button {
|
| 80 |
-
width: 200px;
|
| 81 |
-
background-color: #f8f9fa !important;
|
| 82 |
-
color: #1a1a1a !important;
|
| 83 |
-
border: 1px solid #e9ecef !important;
|
| 84 |
-
padding: 0.5rem 1rem !important;
|
| 85 |
-
border-radius: 5px !important;
|
| 86 |
-
transition: all 0.3s ease !important;
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
.stButton > button:hover {
|
| 90 |
-
background-color: #e9ecef !important;
|
| 91 |
-
transform: translateY(-1px);
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
#MainMenu, footer, header {
|
| 95 |
-
display: none !important;
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
section[data-testid="stSidebar"] {
|
| 99 |
-
display: none !important;
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
/* Hide deprecation warning */
|
| 103 |
-
[data-testid="stExpander"], .element-container:has(>.stAlert) {
|
| 104 |
-
display: none !important;
|
| 105 |
-
}
|
| 106 |
-
</style>
|
| 107 |
-
""", unsafe_allow_html=True)
|
| 108 |
-
|
| 109 |
-
@st.cache_resource(show_spinner=True)
|
| 110 |
def load_models():
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
"
|
| 116 |
-
|
| 117 |
-
}
|
| 118 |
-
except Exception as e:
|
| 119 |
-
st.error(f"Fehler beim Laden der Modelle: {str(e)}")
|
| 120 |
-
return None
|
| 121 |
|
| 122 |
def translate_label(label):
|
| 123 |
translations = {
|
|
@@ -178,117 +85,114 @@ def draw_boxes(image, predictions):
|
|
| 178 |
return result_image
|
| 179 |
|
| 180 |
def main():
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
try:
|
| 184 |
models = load_models()
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
with
|
| 191 |
-
st.
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
label_visibility="collapsed"
|
| 196 |
)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
with col1:
|
| 200 |
-
conf_threshold = st.slider(
|
| 201 |
-
"Konfidenzschwelle",
|
| 202 |
-
min_value=0.0,
|
| 203 |
-
max_value=1.0,
|
| 204 |
-
value=0.60,
|
| 205 |
-
step=0.05,
|
| 206 |
-
label_visibility="visible"
|
| 207 |
-
)
|
| 208 |
-
with col2:
|
| 209 |
-
analyze_button = st.button("Analysieren", use_container_width=True)
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
|
|
|
| 228 |
for pred in predictions_watcher:
|
| 229 |
-
if pred['score']
|
|
|
|
|
|
|
| 230 |
has_fracture = True
|
| 231 |
max_fracture_score = max(max_fracture_score, pred['score'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
|
| 234 |
-
st.write("
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
st.markdown("#### 🛡️ KnochenWächter")
|
| 241 |
-
for pred in predictions_watcher:
|
| 242 |
-
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
| 243 |
-
label_lower = pred['label'].lower()
|
| 244 |
-
if pred['score'] >= conf_threshold and 'fracture' in label_lower:
|
| 245 |
-
has_fracture = True
|
| 246 |
-
max_fracture_score = max(max_fracture_score, pred['score'])
|
| 247 |
-
st.markdown(f"""
|
| 248 |
-
<div class="result-box" style="color: #1a1a1a;">
|
| 249 |
-
<span style="color: {confidence_color}; font-weight: 500;">
|
| 250 |
-
{pred['score']:.1%}
|
| 251 |
-
</span> - {translate_label(pred['label'])}
|
| 252 |
-
</div>
|
| 253 |
-
""", unsafe_allow_html=True)
|
| 254 |
-
|
| 255 |
-
st.markdown("#### 🎓 RöntgenMeister")
|
| 256 |
-
for pred in predictions_master:
|
| 257 |
-
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
| 258 |
-
st.markdown(f"""
|
| 259 |
-
<div class="result-box" style="color: #1a1a1a;">
|
| 260 |
-
<span style="color: {confidence_color}; font-weight: 500;">
|
| 261 |
-
{pred['score']:.1%}
|
| 262 |
-
</span> - {translate_label(pred['label'])}
|
| 263 |
-
</div>
|
| 264 |
-
""", unsafe_allow_html=True)
|
| 265 |
-
|
| 266 |
-
if max_fracture_score > 0:
|
| 267 |
-
st.write("#### 📊 Wahrscheinlichkeit")
|
| 268 |
-
no_fracture_prob = 1 - max_fracture_score
|
| 269 |
-
st.markdown(f"""
|
| 270 |
-
<div class="result-box" style="color: #1a1a1a;">
|
| 271 |
-
Knochenbruch: <strong style="color: #0066cc">{max_fracture_score:.1%}</strong><br>
|
| 272 |
-
Kein Knochenbruch: <strong style="color: #ffa500">{no_fracture_prob:.1%}</strong>
|
| 273 |
-
</div>
|
| 274 |
-
""", unsafe_allow_html=True)
|
| 275 |
-
|
| 276 |
-
with col2:
|
| 277 |
-
predictions = models["KnochenAuge"](image)
|
| 278 |
-
filtered_preds = [p for p in predictions if p['score'] >= conf_threshold]
|
| 279 |
-
|
| 280 |
-
if filtered_preds:
|
| 281 |
-
st.write("#### 🎯 Fraktur Lokalisation")
|
| 282 |
-
result_image = draw_boxes(image, filtered_preds)
|
| 283 |
-
st.image(result_image, use_container_width=True)
|
| 284 |
-
else:
|
| 285 |
-
st.write("#### 🖼️ Röntgenbild")
|
| 286 |
-
st.image(image, use_container_width=True)
|
| 287 |
-
except Exception as e:
|
| 288 |
-
st.error(f"Fehler bei der Bildanalyse: {str(e)}")
|
| 289 |
except Exception as e:
|
| 290 |
st.error(f"Ein Fehler ist aufgetreten: {str(e)}")
|
| 291 |
|
| 292 |
if __name__ == "__main__":
|
| 293 |
-
st.set_page_config(page_title="Fraktur Detektion", layout="wide", initial_sidebar_state="collapsed")
|
| 294 |
main()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Configuration de base pour HF Spaces
|
| 8 |
+
os.environ["STREAMLIT_SERVER_PORT"] = "7860"
|
| 9 |
+
os.environ["STREAMLIT_SERVER_ADDRESS"] = "0.0.0.0"
|
| 10 |
+
os.environ["STREAMLIT_SERVER_HEADLESS"] = "true"
|
| 11 |
+
os.environ["STREAMLIT_SERVER_ENABLE_CORS"] = "true"
|
| 12 |
+
|
| 13 |
+
# Configuration de la page
|
| 14 |
st.set_page_config(
|
| 15 |
page_title="Fraktur Detektion",
|
| 16 |
layout="wide",
|
| 17 |
+
initial_sidebar_state="collapsed"
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
@st.cache_resource
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def load_models():
|
| 22 |
+
return {
|
| 23 |
+
"KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
|
| 24 |
+
"KnochenWächter": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray"),
|
| 25 |
+
"RöntgenMeister": pipeline("image-classification",
|
| 26 |
+
model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388")
|
| 27 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def translate_label(label):
|
| 30 |
translations = {
|
|
|
|
| 85 |
return result_image
|
| 86 |
|
| 87 |
def main():
|
| 88 |
+
st.markdown("""
|
| 89 |
+
<style>
|
| 90 |
+
.stApp {background: #f0f2f5}
|
| 91 |
+
.block-container {max-width: 1400px; padding-top: 1rem; padding-bottom: 1rem}
|
| 92 |
+
div[data-testid="stToolbar"] {display: none}
|
| 93 |
+
#MainMenu {visibility: hidden}
|
| 94 |
+
footer {visibility: hidden}
|
| 95 |
+
.result-box {
|
| 96 |
+
background: #f8f9fa;
|
| 97 |
+
padding: 0.75rem;
|
| 98 |
+
border-radius: 8px;
|
| 99 |
+
margin: 0.5rem 0;
|
| 100 |
+
border: 1px solid #e9ecef;
|
| 101 |
+
}
|
| 102 |
+
</style>
|
| 103 |
+
""", unsafe_allow_html=True)
|
| 104 |
|
| 105 |
try:
|
| 106 |
models = load_models()
|
| 107 |
+
|
| 108 |
+
st.write("### 📤 Röntgenbild hochladen")
|
| 109 |
+
uploaded_file = st.file_uploader("Bild auswählen", type=['png', 'jpg', 'jpeg'], label_visibility="collapsed")
|
| 110 |
+
|
| 111 |
+
col1, col2 = st.columns([2, 1])
|
| 112 |
+
with col1:
|
| 113 |
+
conf_threshold = st.slider(
|
| 114 |
+
"Konfidenzschwelle",
|
| 115 |
+
min_value=0.0, max_value=1.0,
|
| 116 |
+
value=0.60, step=0.05
|
|
|
|
| 117 |
)
|
| 118 |
+
with col2:
|
| 119 |
+
analyze_button = st.button("Analysieren")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
if uploaded_file and analyze_button:
|
| 122 |
+
with st.spinner("Bild wird analysiert..."):
|
| 123 |
+
image = Image.open(uploaded_file)
|
| 124 |
+
results_container = st.container()
|
| 125 |
+
|
| 126 |
+
predictions_watcher = models["KnochenWächter"](image)
|
| 127 |
+
predictions_master = models["RöntgenMeister"](image)
|
| 128 |
+
predictions_locator = models["KnochenAuge"](image)
|
| 129 |
+
|
| 130 |
+
has_fracture = False
|
| 131 |
+
max_fracture_score = 0
|
| 132 |
+
filtered_locations = [p for p in predictions_locator
|
| 133 |
+
if p['score'] >= conf_threshold]
|
| 134 |
+
|
| 135 |
+
for pred in predictions_watcher:
|
| 136 |
+
if pred['score'] >= conf_threshold and 'fracture' in pred['label'].lower():
|
| 137 |
+
has_fracture = True
|
| 138 |
+
max_fracture_score = max(max_fracture_score, pred['score'])
|
| 139 |
+
|
| 140 |
+
with results_container:
|
| 141 |
+
st.write("### 🔍 Analyse Ergebnisse")
|
| 142 |
+
col1, col2 = st.columns(2)
|
| 143 |
+
|
| 144 |
+
with col1:
|
| 145 |
+
st.write("#### 🤖 KI-Diagnose")
|
| 146 |
|
| 147 |
+
st.markdown("#### 🛡️ KnochenWächter")
|
| 148 |
for pred in predictions_watcher:
|
| 149 |
+
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
| 150 |
+
label_lower = pred['label'].lower()
|
| 151 |
+
if pred['score'] >= conf_threshold and 'fracture' in label_lower:
|
| 152 |
has_fracture = True
|
| 153 |
max_fracture_score = max(max_fracture_score, pred['score'])
|
| 154 |
+
st.markdown(f"""
|
| 155 |
+
<div class="result-box">
|
| 156 |
+
<span style="color: {confidence_color}; font-weight: 500;">
|
| 157 |
+
{pred['score']:.1%}
|
| 158 |
+
</span> - {translate_label(pred['label'])}
|
| 159 |
+
</div>
|
| 160 |
+
""", unsafe_allow_html=True)
|
| 161 |
+
|
| 162 |
+
st.markdown("#### 🎓 RöntgenMeister")
|
| 163 |
+
for pred in predictions_master:
|
| 164 |
+
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
| 165 |
+
st.markdown(f"""
|
| 166 |
+
<div class="result-box">
|
| 167 |
+
<span style="color: {confidence_color}; font-weight: 500;">
|
| 168 |
+
{pred['score']:.1%}
|
| 169 |
+
</span> - {translate_label(pred['label'])}
|
| 170 |
+
</div>
|
| 171 |
+
""", unsafe_allow_html=True)
|
| 172 |
+
|
| 173 |
+
if max_fracture_score > 0:
|
| 174 |
+
st.write("#### 📊 Wahrscheinlichkeit")
|
| 175 |
+
no_fracture_prob = 1 - max_fracture_score
|
| 176 |
+
st.markdown(f"""
|
| 177 |
+
<div class="result-box">
|
| 178 |
+
Knochenbruch: <strong style="color: #0066cc">{max_fracture_score:.1%}</strong><br>
|
| 179 |
+
Kein Knochenbruch: <strong style="color: #ffa500">{no_fracture_prob:.1%}</strong>
|
| 180 |
+
</div>
|
| 181 |
+
""", unsafe_allow_html=True)
|
| 182 |
+
|
| 183 |
+
with col2:
|
| 184 |
+
predictions = models["KnochenAuge"](image)
|
| 185 |
+
filtered_preds = [p for p in predictions if p['score'] >= conf_threshold]
|
| 186 |
|
| 187 |
+
if filtered_preds:
|
| 188 |
+
st.write("#### 🎯 Fraktur Lokalisation")
|
| 189 |
+
result_image = draw_boxes(image, filtered_preds)
|
| 190 |
+
st.image(result_image, use_container_width=True)
|
| 191 |
+
else:
|
| 192 |
+
st.write("#### 🖼️ Röntgenbild")
|
| 193 |
+
st.image(image, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
except Exception as e:
|
| 195 |
st.error(f"Ein Fehler ist aufgetreten: {str(e)}")
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
|
|
|
| 198 |
main()
|