import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import PIL
import tensorflow as tf


model = tf.keras.models.load_model('model.h5')
class_name_list = ['Edible', 'Inedible', 'Poisonous']
                                   
def predict_image(img):
    # Reescalamos la imagen en 4 dimensiones
    img_4d = img.reshape(-1,224,224,3)
    # Predicción del modelo
    prediction = model.predict(img_4d)[0]
    # Diccionario con todas las clases y las probabilidades correspondientes
    return {class_name_list[i]: float(prediction[i]) for i in range(3)}
image = gr.inputs.Image(shape=(224,224))
label = gr.outputs.Label(num_top_classes=3)
title = 'Mushroom Edibility Classifier'
description = 'Get the edibility classification for the input mushroom image'
examples=[['app_interface/Boletus edulis 15 wf.jpg'],
          ['app_interface/Cantharelluscibarius5 mw.jpg'],
          ['app_interface/Agaricus augustus 2 wf.jpg'],
          ['app_interface/Coprinellus micaceus 8 wf.jpg'],
          ['app_interface/Clavulinopsis fusiformis 2 fp.jpg'],
          ['app_interface/Amanita torrendii 8 fp.jpg'],
          ['app_interface/Russula sanguinea 5 fp.jpg'],
          ['app_interface/Caloceraviscosa1 mw.jpg'],
          ['app_interface/Amanita muscaria 1 wf.jpg'],
          ['app_interface/Amanita pantherina 11 wf.jpg'],
          ['app_interface/Lactarius torminosus 6 fp.jpg'],
          ['app_interface/Amanitaphalloides1 mw.jpg']]
thumbnail = 'app_interface/thumbnail.png'
article = '''
<!DOCTYPE html>
<html>
<body>
<p>The Mushroom Edibility Classifier is an MVP for CNN multiclass classification model.<br>
It has been trained after gathering <b>5500 mushroom images</b> through Web Scraping techniques from the following web sites:</p>
<br>
<p>
<a href="https://www.mushroom.world/">- Mushroom World</a><br>
<a href="https://www.wildfooduk.com/mushroom-guide/">- Wild Food UK</a> <br>
<a href="https://www.fungipedia.org/hongos">- Fungipedia</a> 
</p>
<br>
<p style="color:Orange;">Note: <i>model created solely and exclusively for academic purposes. The results provided by the model should never be considered definitive as the accuracy of the model is not guaranteed.</i></p>

<br>
<p><b>MODEL METRICS:</b></p>  
<table>
  <tr>
    <th> </th>
    <th>precision</th>
    <th>recall</th>
    <th>f1-score</th>
    <th>support</th>
  </tr>
  <tr>
    <th>Edible</th>
    <th>0.61</th>
    <th>0.70</th>
    <th>0.65</th>
    <th>481</th>
  </tr>
  <tr>
    <th>Inedible</th>
    <th>0.67</th>
    <th>0.69</th>
    <th>0.68</th>
    <th>439</th>
  </tr>
  <tr>
    <th>Poisonous</th>
    <th>0.52</th>
    <th>0.28</th>
    <th>0.36</th>
    <th>192</th>
  </tr>
  <tr>
  <th></th>
  </tr>
  <tr>
    <th>Global Accuracy</th>
    <th></th>
    <th></th>
    <th>0.63</th>
    <th>1112</th>
  </tr>
  <tr>
    <th>Macro Average</th>
    <th>0.60</th>
    <th>0.56</th>
    <th>0.57</th>
    <th>1112</th>
  </tr>
  <tr>
    <th>Weighted Average</th>
    <th>0.62</th>
    <th>0.63</th>
    <th>0.61</th>
    <th>1112</th>
  </tr>
</table>
<br>
<p><i>Author: Íñigo Sarralde Alzórriz</i></p> 
</body>
</html>
'''

iface = gr.Interface(fn=predict_image, 
                     inputs=image, 
                     outputs=label,
                     interpretation='default',
                     title = title,
                     description = description,
                     theme = 'darkpeach',
                     examples = examples,
                     thumbnail = thumbnail,
                     article = article,
                     allow_flagging = False,
                     allow_screenshot = False,                     
                    )
iface.launch()