friesti1 commited on
Commit
be1cec5
1 Parent(s): b286488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,28 +1,35 @@
1
  import gradio as gr
2
  import tensorflow as tf
 
3
  import numpy as np
4
 
 
5
 
6
- model_path = "iris_mlp.keras"
7
- model = tf.keras.models.load_model(model_path)
 
 
 
 
 
 
 
 
8
 
9
- labels = ['Setosa', 'Versicolour', 'Virginica']
 
10
 
11
- # Define the core prediction function
12
- def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
13
- features = [sepal_length, sepal_width, petal_length, petal_width]
14
- features = np.array(features)[None, ...]
15
- prediction = model.predict(features)
16
- print(prediction)
17
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
18
- return confidences
19
 
20
- # Create the Gradio interface
 
21
  iface = gr.Interface(
22
- fn=predict_iris,
23
- inputs=["number", "number", "number", "number"],
24
- outputs=gr.Label(),
25
- examples=[[7.7, 2.6, 6.9, 2.3]]
 
26
  )
27
 
 
28
  iface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ from PIL import Image
4
  import numpy as np
5
 
6
+ labels = ['Cubone', 'Ditto', 'Psyduck', 'Snorlax', 'Weedle']
7
 
8
+ def predict_pokemon_type(uploaded_file):
9
+ """Process the uploaded file."""
10
+ if uploaded_file is None:
11
+ return "No file uploaded."
12
+
13
+ model = tf.keras.models.load_model('pokemon-model_transferlearning.keras')
14
+ # Load the image from the file path
15
+ with Image.open(uploaded_file) as img:
16
+ img = img.resize((200, 200))
17
+ img_array = np.array(img)
18
 
19
+ prediction = model.predict(np.expand_dims(img_array, axis=0))
20
+ confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
21
 
22
+ return confidences
 
 
 
 
 
 
 
23
 
24
+
25
+ # Define the Gradio interface
26
  iface = gr.Interface(
27
+ fn=predict_pokemon_type, # Function to process the input
28
+ inputs=gr.File(label="Upload File"), # File upload widget
29
+ outputs="text", # Output type
30
+ title="Pokemon Classifier", # Title of the interface
31
+ description="Upload a picture of a pokemon (preferably Cubone, Ditto, Psyduck, Snorlax or Weedle), because the model was trained on 'em. It has an astonishing accuracy of 16% :)" # Description of the interface
32
  )
33
 
34
+ # Launch the interface
35
  iface.launch()