utkmst commited on
Commit
8017e91
·
verified ·
1 Parent(s): e4b1113

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -49
app.py CHANGED
@@ -1,64 +1,24 @@
1
  ___all___ = ['learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
2
 
3
- from tensorflow.keras.models import load_model
4
- from PIL import Image
5
- import numpy as np
6
  import gradio as gr
7
 
8
- # Define the ETHNICITIES dictionary
9
- ETHNICITIES = {0: "White", 1: "Black", 2: "Asian", 3: "Indian", 4: "Hispanic"}
10
-
11
  # Load the trained model
12
- Model_L = load_model('model_L.h5')
13
 
14
  # Define the categories based on your model's output
15
- categories = list(ETHNICITIES.values())
16
 
17
  # Define the function to classify images
18
  def classify_image(img):
19
- img = img.resize((48, 48)) # Resize the image to match model input shape
20
- img = img.convert('L') # Convert image to grayscale
21
- img = np.array(img) / 255.0 # Normalize the image
22
- img = img.reshape(-1, 48, 48, 1) # Reshape the image to match model input shape
23
- pred = Model_L.predict(img)
24
- probs = pred[0]
25
  return dict(zip(categories, map(float, probs)))
26
 
27
  # Define the Gradio components
28
- image = gr.Image(type='pil', label='Upload an Image')
29
- label = gr.Label(label="Predictions")
30
  examples = ['example1.jpeg', 'example2.jpeg', 'example3.jpeg'] # Replace with your example images
31
 
32
- # Function to create and launch the Gradio interface
33
- def create_interface(theme='default'):
34
- return gr.Interface(
35
- fn=classify_image,
36
- inputs=image,
37
- outputs=label,
38
- title="Face to Race",
39
- description="Upload an image to classify it based on the trained model.",
40
- examples=examples,
41
- theme=theme
42
- )
43
-
44
- # Create the initial interface
45
- intf = create_interface()
46
-
47
- # Define a callback to update the theme
48
- def update_theme(theme):
49
- global intf
50
- intf.close() # Close the current interface
51
- intf = create_interface(theme) # Create a new interface with the selected theme
52
- intf.launch(share=True, inline=False) # Launch the new interface
53
-
54
- # Add a theme selector interface
55
- theme_selector = gr.Interface(
56
- fn=update_theme,
57
- inputs=gr.Dropdown(choices=['default', 'dark', 'huggingface', 'compact'], label="Choose Theme", value='default'),
58
- outputs=None,
59
- live=True
60
- )
61
-
62
- # Launch the initial interface and the theme selector interface
63
- intf.launch(share=True, inline=False)
64
- theme_selector.launch(share=True, inline=False)
 
1
  ___all___ = ['learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
2
 
3
+ from fastai.vision.all import *
 
 
4
  import gradio as gr
5
 
 
 
 
6
  # Load the trained model
7
+ learn = load_learner('second_model.pkl')
8
 
9
  # Define the categories based on your model's output
10
+ categories = learn.dls.vocab
11
 
12
  # Define the function to classify images
13
  def classify_image(img):
14
+ pred, idx, probs = learn.predict(img)
 
 
 
 
 
15
  return dict(zip(categories, map(float, probs)))
16
 
17
  # Define the Gradio components
18
+ image = gr.Image(type='pil', label='Input Image')
19
+ label = gr.Label()
20
  examples = ['example1.jpeg', 'example2.jpeg', 'example3.jpeg'] # Replace with your example images
21
 
22
+ # Create and launch the Gradio interface
23
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, title="Image Classifier", examples=examples)
24
+ intf.launch(inline=False)