Spaces:
Build error
Build error
| import gradio as gr | |
| import pandas as pd | |
| from pycaret.classification import load_model | |
| # Load the saved model | |
| model = load_model('best_automl_model') | |
| # Prediction function | |
| def predict_anemia(sex, red_pixel, green_pixel, blue_pixel): | |
| # Map input values to a DataFrame | |
| input_data = pd.DataFrame({ | |
| 'Red Pixel': [red_pixel], | |
| 'Green Pixel': [green_pixel], | |
| 'Blue Pixel': [blue_pixel], | |
| 'Sex': [1 if sex == "Male" else 0] # Encode 'Male' as 1, 'Female' as 0 | |
| }) | |
| # Make predictions | |
| prediction = model.predict(input_data)[0] | |
| probability = model.predict_proba(input_data)[0][1] | |
| # Convert results | |
| prediction_label = "Yes" if prediction == 1 else "No" | |
| return prediction_label, f"{probability:.2%}" # Probability as a percentage | |
| # Define input components with better labels and layout | |
| inputs = [ | |
| gr.Radio(["Male", "Female"], label="Sex", interactive=True), | |
| gr.Slider(0, 100, step=0.1, label="Red Pixel Percentage (%)"), | |
| gr.Slider(0, 100, step=0.1, label="Green Pixel Percentage (%)"), | |
| gr.Slider(0, 100, step=0.1, label="Blue Pixel Percentage (%)"), | |
| ] | |
| # Define output components | |
| outputs = [ | |
| gr.Textbox(label="Prediction", interactive=False), | |
| gr.Textbox(label="Probability of Anemia (%)", interactive=False), | |
| ] | |
| # Create the Gradio Interface | |
| interface = gr.Interface( | |
| fn=predict_anemia, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="Anemia Prediction App", | |
| description=( | |
| "This app predicts whether a person is anemic based on the percentages of red, " | |
| "green, and blue pixels in their image and their sex. " | |
| "Simply adjust the sliders and select the person's sex to get a prediction." | |
| ), | |
| theme="huggingface", # Optional theme | |
| live=False, # Turn off live predictions for better performance | |
| examples=[ | |
| ["Male", 45.0, 30.0, 25.0], | |
| ["Female", 50.0, 28.0, 22.0], | |
| ], | |
| ) | |
| # Launch the app | |
| interface.launch() | |