|
|
|
import gradio as gr |
|
from fastai.vision.all import * |
|
|
|
|
|
class GrayscaleTransform(Transform): |
|
""" |
|
Custom transformation class to convert images to grayscale. |
|
This is used to ensure that the input images match the format |
|
used during model training. |
|
""" |
|
def encodes(self, img: PILImage): |
|
""" |
|
Convert the input image to grayscale. |
|
|
|
Args: |
|
img (PILImage): The input image in PIL format. |
|
|
|
Returns: |
|
PIL.Image: The grayscale version of the input image. |
|
""" |
|
return img.convert("L") |
|
|
|
|
|
learn = load_learner('clocker.pkl') |
|
""" |
|
load_learner function loads a saved FastAI learner object. |
|
The 'clocker.pkl' file contains the trained model, including |
|
its architecture, weights, and any necessary preprocessing steps. |
|
""" |
|
|
|
def classify_image(img): |
|
""" |
|
Classify the input image using the loaded model. |
|
|
|
Args: |
|
img: The input image to be classified. |
|
|
|
Returns: |
|
dict: A dictionary containing the prediction probabilities for each class. |
|
""" |
|
|
|
pred, _, probs = learn.predict(img) |
|
|
|
return { |
|
"average woman": float(probs[0]), |
|
"transgender woman": float(probs[1]) |
|
} |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_image, |
|
inputs=gr.Image(), |
|
outputs=gr.Label(num_top_classes=2), |
|
title="Transfem Clocker AI", |
|
description="Upload an image of a woman and this will guess if she is trans.", |
|
) |
|
""" |
|
gr.Interface creates a web interface for the model: |
|
- fn: The function to be called when an image is uploaded |
|
- inputs: Specifies that the input should be an image |
|
- outputs: Displays the top 2 class probabilities as labels |
|
- title and description: Provides context for users |
|
""" |
|
|
|
|
|
iface.launch() |
|
""" |
|
This starts the Gradio interface, making it accessible via a web browser. |
|
it is my first ever AI web app! |
|
""" |