yassonee commited on
Commit
3699dc9
·
verified ·
1 Parent(s): 88cf505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -184
app.py CHANGED
@@ -1,199 +1,70 @@
1
- import gradio as gr
 
2
  from transformers import pipeline
3
- from PIL import Image, ImageDraw
4
- import numpy as np
 
 
 
5
 
6
  # Chargement des modèles
7
  def load_models():
8
  return {
9
- "KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
10
- "KnochenWächter": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray"),
11
- "RöntgenMeister": pipeline("image-classification",
12
- model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388")
13
  }
14
 
15
- def translate_label(label):
16
- translations = {
17
- "fracture": "Knochenbruch",
18
- "no fracture": "Kein Knochenbruch",
19
- "normal": "Normal",
20
- "abnormal": "Auffällig",
21
- "F1": "Knochenbruch",
22
- "NF": "Kein Knochenbruch"
23
- }
24
- return translations.get(label.lower(), label)
25
 
26
- def create_heatmap_overlay(image, box, score):
27
- overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
28
- draw = ImageDraw.Draw(overlay)
29
-
30
- x1, y1 = box['xmin'], box['ymin']
31
- x2, y2 = box['xmax'], box['ymax']
32
-
33
- if score > 0.8:
34
- fill_color = (255, 0, 0, 100)
35
- border_color = (255, 0, 0, 255)
36
- elif score > 0.6:
37
- fill_color = (255, 165, 0, 100)
38
- border_color = (255, 165, 0, 255)
39
- else:
40
- fill_color = (255, 255, 0, 100)
41
- border_color = (255, 255, 0, 255)
42
-
43
- draw.rectangle([x1, y1, x2, y2], fill=fill_color)
44
- draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
45
-
46
- return overlay
47
 
48
- def draw_boxes(image, predictions):
49
- result_image = image.copy().convert('RGBA')
50
-
51
- for pred in predictions:
52
- box = pred['box']
53
- score = pred['score']
 
54
 
55
- overlay = create_heatmap_overlay(image, box, score)
56
- result_image = Image.alpha_composite(result_image, overlay)
57
 
58
- draw = ImageDraw.Draw(result_image)
59
- temp = 36.5 + (score * 2.5)
60
- label = f"{translate_label(pred['label'])} ({score:.1%} • {temp:.1f}°C)"
 
 
 
 
61
 
62
- text_bbox = draw.textbbox((box['xmin'], box['ymin']-20), label)
63
- draw.rectangle(text_bbox, fill=(0, 0, 0, 180))
64
-
65
- draw.text(
66
- (box['xmin'], box['ymin']-20),
67
- label,
68
- fill=(255, 255, 255, 255)
69
- )
70
-
71
- return result_image
72
-
73
- # Modèles chargés globalement
74
- models = load_models()
75
-
76
- def analyze_image(image, conf_threshold=0.60):
77
- if image is None:
78
- return None, "Bitte laden Sie ein Bild hoch."
79
-
80
- # Convertir en PIL Image si nécessaire
81
- if not isinstance(image, Image.Image):
82
- image = Image.fromarray(image)
83
-
84
- # Analyses
85
- predictions_watcher = models["KnochenWächter"](image)
86
- predictions_master = models["RöntgenMeister"](image)
87
- predictions_locator = models["KnochenAuge"](image)
88
-
89
- has_fracture = False
90
- max_fracture_score = 0
91
- result_html = "<div style='background: #f8f9fa; padding: 20px; border-radius: 10px;'>"
92
-
93
- # KnochenWächter results
94
- result_html += "<h3>🛡️ KnochenWächter</h3>"
95
- for pred in predictions_watcher:
96
- confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
97
- if pred['score'] >= conf_threshold and 'fracture' in pred['label'].lower():
98
- has_fracture = True
99
- max_fracture_score = max(max_fracture_score, pred['score'])
100
- result_html += f"""
101
- <div style='background: white; padding: 10px; margin: 5px 0; border-radius: 5px; border: 1px solid #e9ecef;'>
102
- <span style='color: {confidence_color}; font-weight: 500;'>
103
- {pred['score']:.1%}
104
- </span> - {translate_label(pred['label'])}
105
- </div>
106
- """
107
-
108
- # RöntgenMeister results
109
- result_html += "<h3>🎓 RöntgenMeister</h3>"
110
- for pred in predictions_master:
111
- confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
112
- result_html += f"""
113
- <div style='background: white; padding: 10px; margin: 5px 0; border-radius: 5px; border: 1px solid #e9ecef;'>
114
- <span style='color: {confidence_color}; font-weight: 500;'>
115
- {pred['score']:.1%}
116
- </span> - {translate_label(pred['label'])}
117
- </div>
118
  """
119
-
120
- # Probabilité
121
- if max_fracture_score > 0:
122
- no_fracture_prob = 1 - max_fracture_score
123
- result_html += f"""
124
- <h3>📊 Wahrscheinlichkeit</h3>
125
- <div style='background: white; padding: 10px; margin: 5px 0; border-radius: 5px; border: 1px solid #e9ecef;'>
126
- Knochenbruch: <strong style='color: #0066cc'>{max_fracture_score:.1%}</strong><br>
127
- Kein Knochenbruch: <strong style='color: #ffa500'>{no_fracture_prob:.1%}</strong>
128
- </div>
129
  """
130
-
131
- result_html += "</div>"
132
-
133
- # Image processing
134
- predictions = models["KnochenAuge"](image)
135
- filtered_preds = [p for p in predictions if p['score'] >= conf_threshold]
136
- if filtered_preds:
137
- result_image = draw_boxes(image, filtered_preds)
138
- return result_image, result_html
139
- else:
140
- return image, result_html
141
-
142
- # Interface Gradio
143
- css = """
144
- .gradio-container {
145
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
146
- background-color: #f0f2f5;
147
- }
148
- .gr-button {
149
- background-color: #f8f9fa !important;
150
- border: 1px solid #e9ecef !important;
151
- color: #1a1a1a !important;
152
- }
153
- .gr-button:hover {
154
- background-color: #e9ecef !important;
155
- transform: translateY(-1px);
156
- }
157
- .output-html {
158
- background: white;
159
- padding: 20px;
160
- border-radius: 10px;
161
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
162
- }
163
- """
164
-
165
- with gr.Blocks(css=css) as demo:
166
- gr.Markdown("### 📤 Fraktur Detektion")
167
-
168
- with gr.Row():
169
- with gr.Column(scale=1):
170
- input_image = gr.Image(type="pil", label="Röntgenbild hochladen")
171
- conf_threshold = gr.Slider(
172
- minimum=0.0,
173
- maximum=1.0,
174
- value=0.60,
175
- step=0.05,
176
- label="Konfidenzschwelle"
177
- )
178
- analyze_button = gr.Button("Analysieren", variant="primary")
179
-
180
- with gr.Column(scale=1):
181
- output_image = gr.Image(type="pil", label="Analysiertes Bild")
182
- output_html = gr.HTML(label="Ergebnisse")
183
-
184
- analyze_button.click(
185
- fn=analyze_image,
186
- inputs=[input_image, conf_threshold],
187
- outputs=[output_image, output_html]
188
- )
189
 
190
- # Lancement de l'interface
191
- demo.launch(
192
- server_name="0.0.0.0",
193
- server_port=7860,
194
- share=False,
195
- favicon_path=None,
196
- show_api=False,
197
- show_error=False,
198
- debug=False
199
- )
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import HTMLResponse
3
  from transformers import pipeline
4
+ from PIL import Image
5
+ import io
6
+ import uvicorn
7
+
8
+ app = FastAPI()
9
 
10
  # Chargement des modèles
11
  def load_models():
12
  return {
13
+ "model": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray")
 
 
 
14
  }
15
 
16
+ models = load_models()
 
 
 
 
 
 
 
 
 
17
 
18
+ # Page d'accueil simple
19
+ @app.get("/", response_class=HTMLResponse)
20
+ async def main():
21
+ content = """
22
+ <body style="font-family: Arial; max-width: 800px; margin: 0 auto; padding: 20px;">
23
+ <h1>Fraktur Detektion</h1>
24
+ <form action="/upload" enctype="multipart/form-data" method="post">
25
+ <input type="file" name="file" accept="image/*"><br><br>
26
+ <input type="submit" value="Analysieren">
27
+ </form>
28
+ </body>
29
+ """
30
+ return content
 
 
 
 
 
 
 
 
31
 
32
+ # Route pour l'upload et l'analyse
33
+ @app.post("/upload", response_class=HTMLResponse)
34
+ async def upload_file(file: UploadFile = File(...)):
35
+ try:
36
+ # Lecture de l'image
37
+ contents = await file.read()
38
+ image = Image.open(io.BytesIO(contents))
39
 
40
+ # Analyse
41
+ predictions = models["model"](image)
42
 
43
+ # Formatage des résultats
44
+ results = "<h2>Ergebnisse:</h2>"
45
+ for pred in predictions:
46
+ confidence = pred['score'] * 100
47
+ label = "Knochenbruch" if "fracture" in pred['label'].lower() else "Kein Knochenbruch"
48
+ color = "red" if "fracture" in pred['label'].lower() else "green"
49
+ results += f'<p style="color: {color};">{label}: {confidence:.1f}%</p>'
50
 
51
+ return f"""
52
+ <body style="font-family: Arial; max-width: 800px; margin: 0 auto; padding: 20px;">
53
+ <h1>Analyse Ergebnisse</h1>
54
+ {results}
55
+ <br>
56
+ <a href="/">Zurück</a>
57
+ </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  """
59
+ except Exception as e:
60
+ return f"""
61
+ <body style="font-family: Arial; max-width: 800px; margin: 0 auto; padding: 20px;">
62
+ <h1>Fehler</h1>
63
+ <p>Ein Fehler ist aufgetreten: {str(e)}</p>
64
+ <br>
65
+ <a href="/">Zurück</a>
66
+ </body>
 
 
67
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ if __name__ == "__main__":
70
+ uvicorn.run(app, host="0.0.0.0", port=7860)