Spaces:
Running
Running
import gradio as gr | |
import os | |
from svnm.models import GenderDetection | |
from datetime import datetime | |
# Create a directory for saving uploaded images | |
UPLOAD_DIR = "uploadimages" | |
os.makedirs(UPLOAD_DIR, exist_ok=True) | |
# Load the model once | |
model = GenderDetection() | |
# Function to save image and make predictions | |
def classify_image(img): | |
# Generate a unique filename | |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") | |
img_path = os.path.join(UPLOAD_DIR, f"{timestamp}.jpg") | |
# Save the uploaded image | |
img.save(img_path) | |
# Make predictions using the model | |
label, conf = model.predict(img_path) | |
os.unlink(img_path) | |
return f"Label: {label}, Confidence: {conf*100:.2f}%" | |
# Define the Gradio interface | |
interface = gr.Interface( | |
fn=classify_image, | |
inputs=gr.Image(type="pil"), # Updated Gradio input | |
outputs="text", # Output as text | |
title="Image Classification", | |
description="Upload an image to classify it into categories." | |
) | |
# Launch the interface | |
if __name__ == "__main__": | |
interface.launch(share=True) | |