import gradio as gr
import tensorflow as tf
import numpy as np
import os
import tensorflow as tf
import numpy as np
from keras.models import load_model
from tensorflow.keras.utils import load_img

# Charger le modèle

model = load_model('model_multi.h5')


def format_decimal(value):
    decimal_value = format(value, ".2f")
    return decimal_value
    
def detect(img):
    img = np.expand_dims(img, axis=0)
    img = img/255
    prediction = model.predict(img)[0]



    # Initialisation du texte
    texte = ""

    # Détermination du texte et de la couleur pour chaque classe
    if format_decimal(prediction[0]) >= 0.5:
        texte += '<span style="color:red;">Risque d\'infection bactérienne</span><br>'
    if format_decimal(prediction[1]) >= 0.5:
        texte += '<span style="color:green;">Poumon sain</span><br>'
    if format_decimal(prediction[2]) >= 0.5:
        texte += '<span style="color:orange;">Risque d\'infection biologique</span><br>'
    if texte == "":
        texte = "Classe indéterminée"

    return texte



# result = detect(img)
# print(result)
os.system("tar -zxvf examples.tar.gz")
examples = ['examples/n1.jpeg', 'examples/n2.jpeg', 'examples/n3.jpeg', 'examples/n4.jpeg', 'examples/n5.jpeg',
            'examples/n6.jpeg', 'examples/n7.jpeg', 'examples/n8.jpeg', 'examples/p6.jpeg', 'examples/p7.jpeg',]

input = gr.inputs.Image(shape=(100,100))



title = "PneumoDetect: Detection de pneumonie par x-ray"

iface = gr.Interface(fn=detect, inputs=input, outputs="text",examples = examples, examples_per_page=20, title=title)
iface.launch(inline=False)