Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import torch
|
| 5 |
|
|
@@ -20,12 +20,30 @@ st.markdown("""
|
|
| 20 |
--background-color: #ffffff;
|
| 21 |
--text-color: #1f2937;
|
| 22 |
--border-color: #e5e7eb;
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
[data-theme="dark"] {
|
| 26 |
--background-color: #1f2937;
|
| 27 |
--text-color: #f3f4f6;
|
| 28 |
--border-color: #4b5563;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
.block-container {
|
|
@@ -66,6 +84,11 @@ st.markdown("""
|
|
| 66 |
|
| 67 |
.row-widget.stButton {
|
| 68 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
</style>
|
| 71 |
""", unsafe_allow_html=True)
|
|
@@ -74,8 +97,12 @@ st.markdown("""
|
|
| 74 |
def load_models():
|
| 75 |
return {
|
| 76 |
"KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
|
| 77 |
-
"KnochenWächter": pipeline("image-classification",
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
def draw_boxes(image, predictions):
|
|
@@ -101,36 +128,77 @@ def main():
|
|
| 101 |
models = load_models()
|
| 102 |
|
| 103 |
st.markdown("### 📤 Röntgenbilder Upload")
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
if uploaded_files:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
result_image = draw_boxes(result_image, predictions)
|
| 121 |
-
st.image(result_image, caption=f"Bild {idx + 1}", use_column_width=True)
|
| 122 |
-
|
| 123 |
-
# Analyse KnochenWächter et RöntgenMeister
|
| 124 |
-
pred_wachter = models["KnochenWächter"](image)[0]
|
| 125 |
-
pred_meister = models["RöntgenMeister"](image)[0]
|
| 126 |
-
|
| 127 |
-
if pred_wachter['score'] > 0.6 or pred_meister['score'] > 0.6:
|
| 128 |
-
st.markdown(f"""
|
| 129 |
-
<div class='result-box'>
|
| 130 |
-
<span style='color: #2563eb'>KnochenWächter:</span> {pred_wachter['score']:.1%}<br>
|
| 131 |
-
<span style='color: #2563eb'>RöntgenMeister:</span> {pred_meister['score']:.1%}
|
| 132 |
-
</div>
|
| 133 |
-
""", unsafe_allow_html=True)
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoImageProcessor
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 20 |
--background-color: #ffffff;
|
| 21 |
--text-color: #1f2937;
|
| 22 |
--border-color: #e5e7eb;
|
| 23 |
+
--button-color: #2563eb;
|
| 24 |
+
--button-hover: #1d4ed8;
|
| 25 |
}
|
| 26 |
|
| 27 |
[data-theme="dark"] {
|
| 28 |
--background-color: #1f2937;
|
| 29 |
--text-color: #f3f4f6;
|
| 30 |
--border-color: #4b5563;
|
| 31 |
+
--button-color: #3b82f6;
|
| 32 |
+
--button-hover: #2563eb;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.stButton > button {
|
| 36 |
+
background-color: var(--button-color) !important;
|
| 37 |
+
color: white !important;
|
| 38 |
+
border: none !important;
|
| 39 |
+
padding: 0.5rem 1rem !important;
|
| 40 |
+
border-radius: 0.375rem !important;
|
| 41 |
+
font-weight: 500 !important;
|
| 42 |
+
transition: background-color 0.2s !important;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
.stButton > button:hover {
|
| 46 |
+
background-color: var(--button-hover) !important;
|
| 47 |
}
|
| 48 |
|
| 49 |
.block-container {
|
|
|
|
| 84 |
|
| 85 |
.row-widget.stButton {
|
| 86 |
text-align: center;
|
| 87 |
+
margin: 1rem 0;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
div[data-testid="stFileUploader"] {
|
| 91 |
+
width: 100%;
|
| 92 |
}
|
| 93 |
</style>
|
| 94 |
""", unsafe_allow_html=True)
|
|
|
|
| 97 |
def load_models():
|
| 98 |
return {
|
| 99 |
"KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
|
| 100 |
+
"KnochenWächter": pipeline("image-classification",
|
| 101 |
+
model="Heem2/bone-fracture-detection-using-xray",
|
| 102 |
+
image_processor=AutoImageProcessor.from_pretrained("Heem2/bone-fracture-detection-using-xray")),
|
| 103 |
+
"RöntgenMeister": pipeline("image-classification",
|
| 104 |
+
model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388",
|
| 105 |
+
image_processor=AutoImageProcessor.from_pretrained("nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388"))
|
| 106 |
}
|
| 107 |
|
| 108 |
def draw_boxes(image, predictions):
|
|
|
|
| 128 |
models = load_models()
|
| 129 |
|
| 130 |
st.markdown("### 📤 Röntgenbilder Upload")
|
| 131 |
+
|
| 132 |
+
# File uploader avec label caché
|
| 133 |
+
uploaded_files = st.file_uploader(
|
| 134 |
+
"Wählen Sie Röntgenbilder aus",
|
| 135 |
+
type=['png', 'jpg', 'jpeg'],
|
| 136 |
+
accept_multiple_files=True,
|
| 137 |
+
label_visibility="collapsed"
|
| 138 |
+
)
|
| 139 |
|
| 140 |
if uploaded_files:
|
| 141 |
+
# Bouton d'analyse
|
| 142 |
+
if st.button("Bilder analysieren", key="analyze_button"):
|
| 143 |
+
col1, col2 = st.columns([1, 1])
|
| 144 |
+
|
| 145 |
+
for idx, uploaded_file in enumerate(uploaded_files):
|
| 146 |
+
image = Image.open(uploaded_file)
|
| 147 |
+
|
| 148 |
+
# Analyse avec KnochenAuge (localisierung)
|
| 149 |
+
predictions = models["KnochenAuge"](image)
|
| 150 |
+
fractures_found = any(p['label'].lower() == 'fracture' and p['score'] > 0.6 for p in predictions)
|
| 151 |
+
|
| 152 |
+
# Afficher uniquement si des fractures sont détectées
|
| 153 |
+
if fractures_found:
|
| 154 |
+
with col1 if idx % 2 == 0 else col2:
|
| 155 |
+
result_image = image.copy()
|
| 156 |
+
result_image = draw_boxes(result_image, predictions)
|
| 157 |
+
st.image(result_image, caption=f"Bild {idx + 1}", use_column_width=True)
|
| 158 |
+
|
| 159 |
+
# Analyse KnochenWächter et RöntgenMeister
|
| 160 |
+
pred_wachter = models["KnochenWächter"](image)[0]
|
| 161 |
+
pred_meister = models["RöntgenMeister"](image)[0]
|
| 162 |
+
|
| 163 |
+
if pred_wachter['score'] > 0.6 or pred_meister['score'] > 0.6:
|
| 164 |
+
st.markdown(f"""
|
| 165 |
+
<div class='result-box'>
|
| 166 |
+
<span style='color: #2563eb'>KnochenWächter:</span> {pred_wachter['score']:.1%}<br>
|
| 167 |
+
<span style='color: #2563eb'>RöntgenMeister:</span> {pred_meister['score']:.1%}
|
| 168 |
+
</div>
|
| 169 |
+
""", unsafe_allow_html=True)
|
| 170 |
+
|
| 171 |
+
# Script pour la synchronisation du thème
|
| 172 |
+
st.markdown("""
|
| 173 |
+
<script>
|
| 174 |
+
// Fonction pour mettre à jour le thème
|
| 175 |
+
function updateTheme(isDark) {
|
| 176 |
+
document.documentElement.setAttribute('data-theme', isDark ? 'dark' : 'light');
|
| 177 |
+
|
| 178 |
+
// Mise à jour des styles en fonction du thème
|
| 179 |
+
const root = document.documentElement;
|
| 180 |
+
if (isDark) {
|
| 181 |
+
root.style.setProperty('--background-color', '#1f2937');
|
| 182 |
+
root.style.setProperty('--text-color', '#f3f4f6');
|
| 183 |
+
root.style.setProperty('--border-color', '#4b5563');
|
| 184 |
+
} else {
|
| 185 |
+
root.style.setProperty('--background-color', '#ffffff');
|
| 186 |
+
root.style.setProperty('--text-color', '#1f2937');
|
| 187 |
+
root.style.setProperty('--border-color', '#e5e7eb');
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
|
| 191 |
+
// Écouter les messages du parent
|
| 192 |
+
window.addEventListener('message', function(e) {
|
| 193 |
+
if (e.data.type === 'theme-change') {
|
| 194 |
+
updateTheme(e.data.theme === 'dark');
|
| 195 |
+
}
|
| 196 |
+
});
|
| 197 |
|
| 198 |
+
// Thème initial basé sur les préférences système
|
| 199 |
+
updateTheme(window.matchMedia('(prefers-color-scheme: dark)').matches);
|
| 200 |
+
</script>
|
| 201 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
main()
|