Spaces:
Sleeping
Sleeping
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import gradio as grimport joblibimport pandas as pd
# 1. Load the trained pipeline# This tells the app to look for your joblib file in the same foldermodel = joblib.load('model_pipeline.joblib')
# 2. Define the prediction functiondef predict_battery_drain(screen_time, app_time, apps_installed, data_usage, age, device_model): # Create a dataframe from the inputs. # The column names perfectly match your original dataset. input_data = pd.DataFrame({ 'Screen On Time (hours/day)': [screen_time], 'App Usage Time (min/day)': [app_time], 'Number of Apps Installed': [apps_installed], 'Data Usage (MB/day)': [data_usage], 'Age': [age], 'Device Model': [device_model] }) # Make prediction using the pipeline prediction = model.predict(input_data)[0] return f"{prediction:.2f} mAh/day"
# 3. Define the Gradio Interface# These are the exact device models extracted from your CSV filedevice_model_list = [ "Google Pixel 5", "iPhone 12", "OnePlus 9", "Samsung Galaxy S21", "Xiaomi Mi 11"]
# Set up the input boxes with some average default valuesinputs = [ gr.Number(label="Screen On Time (hours/day)", value=4.5), gr.Number(label="App Usage Time (min/day)", value=200), gr.Number(label="Number of Apps Installed", value=50), gr.Number(label="Data Usage (MB/day)", value=1000), gr.Number(label="Age", value=30), gr.Dropdown(choices=device_model_list, label="Device Model", value="Google Pixel 5")]
# Set up the output boxoutputs = gr.Textbox(label="Predicted Battery Drain")
# 4. Launch the appapp = gr.Interface( fn=predict_battery_drain, inputs=inputs, outputs=outputs, title="🔋 Mobile Battery Drain Predictor", description="Enter device usage metrics to predict daily battery drain (in mAh/day). Built using scikit-learn and Gradio.")
if __name__ == "__main__": app.launch()app.pyAdded+59