import gradio as gr import numpy as np from PIL import Image import keras # Modell laden (.keras-Datei, die du aus Colab exportiert hast) model = keras.models.load_model("mpi_model.keras") IMG_SIZE = (224, 224) def predict_mpi(img: Image.Image, threshold: float): # Preprocessing wie im Training img = img.resize(IMG_SIZE) arr = np.array(img) / 255.0 if arr.ndim == 2: # falls Graustufen arr = np.expand_dims(arr, axis=-1) arr = np.expand_dims(arr, axis=0) # Modellvorhersage (angenommen: Sigmoid-Output 0..1) proba = float(model.predict(arr)[0][0]) klassifikation = "positiv" if proba >= threshold else "negativ" # erste Ausgabe: Wahrscheinlichkeit, zweite: Textklassifikation return proba, klassifikation demo = gr.Interface( fn=predict_mpi, inputs=[ gr.Image(type="pil", label="Myokardszintigrafie hochladen"), gr.Slider( minimum=0.0, maximum=1.0, step=0.01, value=0.26, # HIER Standard-Schwelle einstellen, z.B. 0.26 statt 0.30 label="Schwelle für 'Positiv' (0–1)", ), ], outputs=[ gr.Number(label="Wahrscheinlichkeit für 'Positiv'"), gr.Textbox(label="Klassifikation"), ], title="Prototypisches KI-Tool zur Einschätzung von MPI-Bildern.", description="Lädt eine Myokardszintigrafie und berechnet die Wahrscheinlichkeit für einen positiven Befund.", ) if __name__ == "__main__": demo.launch()