File size: 9,296 Bytes
0b15508
 
2ee10cf
0b15508
 
 
 
 
 
10cfdb2
2ee10cf
0b15508
 
51301fb
0b15508
10cfdb2
0b15508
 
0ee0776
51301fb
 
 
 
 
 
 
0ee0776
51301fb
 
 
 
 
 
2ee10cf
10cfdb2
0b15508
 
 
 
 
 
 
 
 
 
 
 
 
 
2ee10cf
 
 
 
 
 
 
 
 
 
 
 
 
 
0b15508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ee10cf
0b15508
 
 
 
2ee10cf
 
 
0b15508
2ee10cf
 
 
 
 
 
 
0b15508
2ee10cf
 
 
 
 
 
 
0b15508
48661eb
 
 
 
0b15508
2ee10cf
0b15508
2ee10cf
 
 
 
0b15508
 
 
 
 
 
 
 
 
2ee10cf
0b15508
 
 
2ee10cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b15508
 
 
 
 
2ee10cf
0b15508
e0e7f99
 
 
 
0b15508
 
 
2ee10cf
0b15508
 
 
 
 
 
 
 
 
 
 
 
10cfdb2
0b15508
 
10cfdb2
2ee10cf
0b15508
10cfdb2
0b15508
 
 
 
 
 
 
 
2ee10cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b15508
 
 
 
 
 
 
 
10cfdb2
0b15508
 
 
 
 
51301fb
0b15508
 
10cfdb2
0b15508
 
 
 
2ee10cf
0b15508
 
 
 
 
2ee10cf
0b15508
 
 
 
 
10cfdb2
0b15508
2c3deb2
0b15508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ee10cf
0b15508
 
 
 
 
10cfdb2
0b15508
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
159
160
161
162
163
164
165
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from transformers import pipeline
from PIL import Image
import io
import uvicorn
import base64

app = FastAPI()

# Chargement des modèles
def load_models():
    return {
        "chest_classifier": pipeline("image-classification", model="codewithdark/vit-chest-xray")
    }

models = load_models()

# Liste des maladies dans l'ordre des labels du modèle
LABEL_MAP = {
    'LABEL_0': 'Kardiomegalie',  # Élargissement du cœur
    'LABEL_1': 'Ödem',           # Œdème
    'LABEL_2': 'Konsolidierung', # Consolidation
    'LABEL_3': 'Lungenentzündung', # Pneumonie
    'LABEL_4': 'Kein Befund'     # Aucune anomalie
}

def translate_label(label):
    # Si le label est au format "LABEL_X", on le traduit
    if isinstance(label, str) and label in LABEL_MAP:
        return LABEL_MAP[label]
    # Sinon on retourne le label original avec un avertissement
    return f"Unbekannt ({label})"
    return translations.get(label, label)

def image_to_base64(image):
    buffered = io.BytesIO()
    image.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode()
    return f"data:image/png;base64,{img_str}"

COMMON_STYLES = """
    body {
        font-family: system-ui, -apple-system, sans-serif;
        background: #f0f2f5;
        margin: 0;
        padding: 20px;
        color: #1a1a1a;
    }
    ::-webkit-scrollbar {
        width: 8px;
        height: 8px;
    }
    
    ::-webkit-scrollbar-track {
        background: transparent;
    }
    
    ::-webkit-scrollbar-thumb {
        background-color: rgba(156, 163, 175, 0.5);
        border-radius: 4px;
    }
    
    .container {
        max-width: 1200px;
        margin: 0 auto;
        background: white;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .button {
        background: #2d2d2d;
        color: white;
        border: none;
        padding: 12px 30px;
        border-radius: 8px;
        cursor: pointer;
        font-size: 1.1em;
        transition: all 0.3s ease;
        position: relative;
    }
    .button:hover {
        background: #404040;
    }
    @keyframes progress {
        0% { width: 0; }
        100% { width: 100%; }
    }
    .button-progress {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 4px;
        background: rgba(255, 255, 255, 0.5);
        width: 0;
    }
    .button:active .button-progress {
        animation: progress 2s linear forwards;
    }
    img {
        max-width: 100%;
        height: auto;
        border-radius: 8px;
    }
    @keyframes blink {
        0% { opacity: 1; }
        50% { opacity: 0; }
        100% { opacity: 1; }
    }
    #loading {
        display: none;
        color: white;
        margin-top: 10px;
        animation: blink 1s infinite;
        text-align: center;
    }
"""

@app.get("/", response_class=HTMLResponse)
async def main():
    content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>Thorax-Röntgen Analyse</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            {COMMON_STYLES}
            
            .upload-section {{
                background: #2d2d2d;
                padding: 40px;
                border-radius: 12px;
                margin: 20px 0;
                text-align: center;
                border: 2px dashed #404040;
                transition: all 0.3s ease;
                color: white;
            }}
            .upload-section:hover {{
                border-color: #555;
            }}
            input[type="file"] {{
                width: 0.1px;
                height: 0.1px;
                opacity: 0;
                overflow: hidden;
                position: absolute;
                z-index: -1;
            }}
            .file-upload-label {{
                display: inline-block;
                padding: 12px 30px;
                background: #404040;
                color: white;
                border-radius: 8px;
                cursor: pointer;
                font-size: 1.1em;
                transition: all 0.3s ease;
                margin: 20px 0;
            }}
            .file-upload-label:hover {{
                background: #555;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="upload-section">
                <form action="/analyze" method="post" enctype="multipart/form-data" onsubmit="document.getElementById('loading').style.display = 'block';">
                    <div>
                        <label for="file-upload" class="file-upload-label">
                            Röntgenbild auswählen
                        </label>
                        <input id="file-upload" type="file" name="file" accept="image/*" required>
                    </div>
                    <button type="submit" class="button">
                        Analysieren
                        <div class="button-progress"></div>
                    </button>
                    <div id="loading">Wird geladen...</div>
                </form>
            </div>
        </div>
    </body>
    </html>
    """
    return content

@app.post("/analyze", response_class=HTMLResponse)
async def analyze_file(file: UploadFile = File(...)):
    try:
        contents = await file.read()
        image = Image.open(io.BytesIO(contents))
        
        predictions = models["chest_classifier"](image)
        result_image_b64 = image_to_base64(image)
        
        results_html = f"""
        <!DOCTYPE html>
        <html>
        <head>
            <title>Ergebnisse</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                {COMMON_STYLES}
                
                .results-grid {{
                    display: grid;
                    grid-template-columns: 1fr 1fr;
                    gap: 20px;
                    margin-top: 20px;
                }}
                .result-box {{
                    background: white;
                    padding: 20px;
                    border-radius: 12px;
                    margin: 10px 0;
                    border: 1px solid #e9ecef;
                }}
                .score-high {{ 
                    color: #0066cc; 
                    font-weight: bold;
                }}
                .score-medium {{ 
                    color: #ffa500; 
                    font-weight: bold;
                }}
                .back-button {{
                    display: inline-block;
                    text-decoration: none;
                    margin-top: 20px;
                }}
                h3 {{
                    color: #0066cc;
                    margin-top: 0;
                }}
                @media (max-width: 768px) {{
                    .results-grid {{
                        grid-template-columns: 1fr;
                    }}
                }}
            </style>
        </head>
        <body>
            <div class="container">
                <div class="results-grid">
                    <div class="result-box">
                        <h3>Analyse-Ergebnisse</h3>
        """
        
        for pred in predictions:
            confidence_class = "score-high" if pred['score'] > 0.7 else "score-medium"
            results_html += f"""
                <div>
                    <span class="{confidence_class}">{pred['score']:.1%}</span> - 
                    {translate_label(pred['label'])}
                </div>
            """
        
        results_html += f"""
                    </div>
                    <div class="result-box">
                        <h3>Röntgenbild</h3>
                        <img src="{result_image_b64}" alt="Analysiertes Röntgenbild">
                    </div>
                </div>
                
                <a href="/" class="button back-button">
                    ← Zurück
                    <div class="button-progress"></div>
                </a>
            </div>
        </body>
        </html>
        """
        
        return results_html
        
    except Exception as e:
        return f"""
        <!DOCTYPE html>
        <html>
        <head>
            <title>Fehler</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                {COMMON_STYLES}
                .error-box {{
                    background: #fee2e2;
                    border: 1px solid #ef4444;
                    padding: 20px;
                    border-radius: 8px;
                    margin: 20px 0;
                }}
            </style>
        </head>
        <body>
            <div class="container">
                <div class="error-box">
                    <h3>Fehler</h3>
                    <p>{str(e)}</p>
                </div>
                <a href="/" class="button back-button">
                    ← Zurück
                    <div class="button-progress"></div>
                </a>
            </div>
        </body>
        </html>
        """

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)