huntrezz commited on
Commit
5af709c
·
verified ·
1 Parent(s): dd80c1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -15
app.py CHANGED
@@ -1,30 +1,71 @@
1
- import gradio as gr
2
- from fastai.vision.all import *
 
3
 
4
- # Define the GrayscaleTransform class
5
  class GrayscaleTransform(Transform):
 
 
 
 
 
6
  def encodes(self, img: PILImage):
7
- return img.convert("L")
 
 
 
 
 
 
 
 
 
8
 
9
- # Load the exported model
10
  learn = load_learner('clocker.pkl')
 
 
 
 
 
11
 
12
  def classify_image(img):
 
 
 
 
 
 
 
 
 
 
13
  pred, _, probs = learn.predict(img)
 
14
  return {
15
- "average woman": float(probs[0]),
16
- "transgender woman": float(probs[1])
17
  }
18
 
19
  # Create the Gradio interface
20
  iface = gr.Interface(
21
- fn=classify_image,
22
- inputs=gr.Image(),
23
- outputs=gr.Label(num_top_classes=2),
24
- title="Transfem Clocker AI",
25
- description="Upload an image of a woman and this will guess if she is trans.",
26
-
27
  )
 
 
 
 
 
 
 
28
 
29
- # Launch the interface
30
- iface.launch()
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import gradio as gr # Gradio for creating web interfaces
3
+ from fastai.vision.all import * # FastAI library for deep learning tasks
4
 
5
+ # Define a custom transformation class for converting images to grayscale
6
  class GrayscaleTransform(Transform):
7
+ """
8
+ Custom transformation class to convert images to grayscale.
9
+ This is used to ensure that the input images match the format
10
+ used during model training.
11
+ """
12
  def encodes(self, img: PILImage):
13
+ """
14
+ Convert the input image to grayscale.
15
+
16
+ Args:
17
+ img (PILImage): The input image in PIL format.
18
+
19
+ Returns:
20
+ PIL.Image: The grayscale version of the input image.
21
+ """
22
+ return img.convert("L") # 'L' mode represents grayscale images
23
 
24
+ # Load the pre-trained model
25
  learn = load_learner('clocker.pkl')
26
+ """
27
+ load_learner function loads a saved FastAI learner object.
28
+ The 'clocker.pkl' file contains the trained model, including
29
+ its architecture, weights, and any necessary preprocessing steps.
30
+ """
31
 
32
  def classify_image(img):
33
+ """
34
+ Classify the input image using the loaded model.
35
+
36
+ Args:
37
+ img: The input image to be classified.
38
+
39
+ Returns:
40
+ dict: A dictionary containing the prediction probabilities for each class.
41
+ """
42
+ # Make a prediction using the loaded model
43
  pred, _, probs = learn.predict(img)
44
+ # Return a dictionary with class probabilities
45
  return {
46
+ "average woman": float(probs[0]), # Probability for "average woman" class
47
+ "transgender woman": float(probs[1]) # Probability for "transgender woman" class
48
  }
49
 
50
  # Create the Gradio interface
51
  iface = gr.Interface(
52
+ fn=classify_image, # The function to be called when the interface is used
53
+ inputs=gr.Image(), # Input component: an image upload widget
54
+ outputs=gr.Label(num_top_classes=2), # Output component: label with top 2 classes
55
+ title="Transfem Clocker AI", # Title of the web interface
56
+ description="Upload an image of a woman and this will guess if she is trans.", # Description of the interface
 
57
  )
58
+ """
59
+ gr.Interface creates a web interface for the model:
60
+ - fn: The function to be called when an image is uploaded
61
+ - inputs: Specifies that the input should be an image
62
+ - outputs: Displays the top 2 class probabilities as labels
63
+ - title and description: Provides context for users
64
+ """
65
 
66
+ # Launches the interface
67
+ iface.launch()
68
+ """
69
+ This starts the Gradio interface, making it accessible via a web browser.
70
+ it is my first ever AI web app!
71
+ """