import gradio as gr import tensorflow as tf import numpy as np import os import pandas as pd model_gender = tf.keras.models.load_model('model_gender.h5') model_age = tf.keras.models.load_model('model_age.h5') actual_data = { "000000.png": {"Image": 1,"age": 61.0, "gender": "Female"}, "000001.png": {"Image": 2,"age": 63.0, "gender": "Male"}, "000002.png": {"Image": 3,"age": 45.0, "gender": "Male"}, "000003.png": {"Image": 4,"age": 59.0, "gender": "Female"}, "000004.png": {"Image": 5,"age": 37.0, "gender": "Female"} } df = pd.DataFrame(actual_data).T data = {'Name': ['Accuracy', 'Precision', 'Recall', 'F1-score'], 'Value': [96.11 , 0.9368, 0.9731, 0.9546]} df1 = pd.DataFrame(data) def preprocess_image(image): # Assuming image is a PIL Image object from Gradio img = image.convert('L') # Convert to grayscale img = img.resize((128, 128)) img = np.array(img) / 255.0 # Normalize pixel values img = img.reshape((1, 128, 128, 1)) # Add channel dimension return img def predict(image): preprocessed_image = preprocess_image(image) gender_pred = model_gender.predict(preprocessed_image)[0][0] age_pred = model_age.predict(preprocessed_image)[0][0] gender = "Male" if gender_pred > 0.68 else "Female" list = "{:.2f}".format(age_pred),gender,df return list # Gradio Interface with separate outputs text_age = gr.components.Textbox(label="Predicted Age") text_gender = gr.components.Textbox(label="Predicted Gender") def predictor_tab(): interface = gr.Interface(predict, gr.components.Image(height=440,width=1000,label="Upload Image", type="pil"), outputs=[text_age, text_gender, gr.DataFrame(value=df)], examples=[ os.path.join(os.path.dirname(__file__),"00000.png"), os.path.join(os.path.dirname(__file__),"00001.png"), os.path.join(os.path.dirname(__file__),"00002.png"), os.path.join(os.path.dirname(__file__),"00003.png"), os.path.join(os.path.dirname(__file__),"00004.png")], allow_flagging='never') return interface def about_tab(): with gr.Blocks() as about: # Title and Introduction gr.Markdown("# Age and Gender Prediction with Deep Learning!") gr.Markdown("This awesome app uses deep learning magic ✨ to predict someone's age and gender based on a x-ray image! Just upload a photo, and our clever models will do their best detective work to unveil the mystery.") # Dataset Section with gr.Row(): with gr.Column(): gr.Markdown("**DATASET 🗃**") gr.Markdown( """ The lung scans used in this project come from a publicly available dataset. It contains approximately 10,700 scans for training and 11,700 scans for testing. This dataset was part of a competition held by The Radiology and Diagnostic Imaging Society of São Paulo (SPR) with Amazon Web Services. """ ) gr.Text("https://www.kaggle.com/datasets/felipekitamura/spr-x-ray-age-and-gender-dataset",label="link") # Model Performance Section gr.Markdown("**Model Performance 💡**") table = gr.DataFrame(value=df1) gr.Markdown("⚜ **Model Accuracy for Genders**") gender_img = 'gender.png' gr.Image(value=gender_img, width=500, height=450) gr.Markdown("⚜ **Model Accuracy for Age**") age_img = 'age.png' gr.Image(value=age_img, width=500, height=450) gr.Markdown("⚜ **Confusion matrix**") matrix_img = 'matrix.png' gr.Image(value=matrix_img, width=500, height=450) # Wrap the table in a Block for better formatting # Creator Information Section with gr.Row(): with gr.Column(): gr.Markdown("**Created by 🤓**") gr.Markdown("Uday Jawheri") with gr.Row(): gr.Text("https://www.linkedin.com/in/uday-jawheri/", label="LinkedIn") gr.Text("https://xudayx.github.io/Portfolio/", label="Website") return about with gr.Blocks(theme=gr.themes.Soft(), title="Age and Gender Prediction") as app: # Consistent variable name 'app' with gr.Tab("Predictor"): predictor_tab() with gr.Tab("About"): about_tab() # Launch the Gradio app app.launch()