utkmst commited on
Commit
bd42d22
·
verified ·
1 Parent(s): b1b94ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -28
app.py CHANGED
@@ -1,42 +1,53 @@
 
 
 
1
  import gradio as gr
2
- import tensorflow as tf
3
- from tensorflow.keras.models import load_model
4
- from tensorflow.keras.preprocessing import image as keras_image
5
- import numpy as np
6
 
7
  # Load the trained model
8
- model = load_model('race_prediction_model.h5')
9
 
10
  # Define the categories based on your model's output
11
- categories = [
12
- 'Black', 'Indian', 'Southeast Asian',
13
- 'East Asian', 'White', 'Middle Eastern', 'Latino_Hispanic'
14
- ] # Replace with your actual categories
15
 
16
  # Define the function to classify images
17
  def classify_image(img):
18
- img = img.resize((224, 224))
19
- img_array = keras_image.img_to_array(img)
20
- img_array = np.expand_dims(img_array, axis=0)
21
- img_array /= 255.0
22
-
23
- predictions = model.predict(img_array)
24
- return {category: float(pred) for category, pred in zip(categories, predictions[0])}
25
 
26
  # Define the Gradio components
27
  image = gr.Image(type='pil', label='Upload an Image')
28
  label = gr.Label(label="Predictions")
29
  examples = ['example1.jpeg', 'example2.jpeg', 'example3.jpeg'] # Replace with your example images
30
 
31
- # Define the Gradio interface
32
- intf = gr.Interface(
33
- fn=classify_image,
34
- inputs=image,
35
- outputs=label,
36
- title="Face to Race",
37
- description="Upload an image to classify it based on the trained model.",
38
- examples=examples
39
- )
40
-
41
- # Launch the interface
42
- intf.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='Upload an Image')
19
  label = gr.Label(label="Predictions")
20
  examples = ['example1.jpeg', 'example2.jpeg', 'example3.jpeg'] # Replace with your example images
21
 
22
+ # Define a theme selector
23
+ theme_selector = gr.Dropdown(choices=['default', 'dark', 'huggingface', 'compact'], label="Choose Theme", value='default')
24
+
25
+ # Function to create and launch the Gradio interface
26
+ def create_interface(theme):
27
+ return gr.Interface(
28
+ fn=classify_image,
29
+ inputs=image,
30
+ outputs=label,
31
+ title="Image Classifier",
32
+ description="Upload an image to classify it based on the trained model.",
33
+ examples=examples,
34
+ layout="horizontal",
35
+ theme=theme
36
+ )
37
+
38
+ # Create the initial interface
39
+ intf = create_interface('default')
40
+
41
+ # Define a callback to update the theme
42
+ def update_theme(theme):
43
+ global intf
44
+ intf.close() # Close the current interface
45
+ intf = create_interface(theme) # Create a new interface with the selected theme
46
+ intf.launch(share=True, inline=False) # Launch the new interface
47
+
48
+ # Launch the initial interface
49
+ intf.launch(share=True, inline=False)
50
+
51
+ # Add a separate interface for theme selection
52
+ theme_intf = gr.Interface(fn=update_theme, inputs=theme_selector, outputs=None, live=True)
53
+ theme_intf.launch(share=True, inline=False)