VimalJohnMV's picture
Update app.py
b0c5d50 verified
# --- File: app.py ---
import gradio as gr
import tensorflow as tf
import numpy as np
from huggingface_hub import hf_hub_download
# --- Configuration ---
# The Gradio app will download the model from this same repository
REPO_ID = "VimalJohnMV/Wrinklum-Revealus"
MODEL_FILENAME = "wrinkle_evaluator_model.h5"
IMG_HEIGHT = 256
IMG_WIDTH = 256
# --- 1. Load the model from Hugging Face Hub ---
try:
print("Downloading model from Hugging Face Hub...")
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
model = tf.keras.models.load_model(model_path)
print("Model loaded successfully.")
except Exception as e:
print(f"Error loading model: {e}")
model = None # Handle case where model fails to load
# --- 2. Define the prediction function ---
def evaluate_wrinkles_and_comment(input_image):
if model is None:
return "Error: Model could not be loaded."
if input_image is None:
return "Please upload an image."
# Preprocess the image using TensorFlow's native functions for better compatibility
img_tensor = tf.convert_to_tensor(input_image, dtype=tf.float32)
img_resized = tf.image.resize(img_tensor, (IMG_HEIGHT, IMG_WIDTH))
img_normalized = img_resized / 255.0
img_array = np.expand_dims(img_normalized, axis=0)
# Make a prediction
prediction = model.predict(img_array)[0][0]
# Generate comment based on the prediction
if prediction < 0.2:
comment = "WOW, A perfectly ironed shirt! How did you get it to look so......BORING!!!!"
elif prediction < 0.5:
comment = "It's nice to see an outfit that hasn't completely given up on life yet"
elif prediction < 0.8:
comment = "I see you've gone for the 'woke up in a duffel bag' look. A classic."
else:
comment = "I didn't know your shirt was a participant in a very aggressive game of origami. 😅"
return f"Wrinkle Score (0.0-1.0): {prediction:.2f}\n\nComment: {comment}"
# --- 3. Create the Gradio Interface ---
iface = gr.Interface(
fn=evaluate_wrinkles_and_comment,
inputs=gr.Image(type="numpy", label="Upload a Dress Photo"),
outputs=gr.Textbox(label="Evaluation Result", lines=4),
title="👗Wrinklum-Revealus",
description="Upload a photo of a dress, and this AI will give it a wrinkle score and a comment."
)
if __name__ == "__main__":
iface.launch()