Spaces:
Sleeping
Sleeping
Download app.py from devxatif/Battery_Drain_Predictor: direct link, hf CLI and curl.
- Browser
- Download file 4.85 kB
-
https://huggingface.co/spaces/devxatif/Battery_Drain_Predictor/resolve/main/app.py
- Command line
-
hf download hf://spaces/devxatif/Battery_Drain_Predictor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/devxatif/Battery_Drain_Predictor/resolve/main/app.py
4.85 kB
| import gradio as gr | |
| import joblib | |
| import pandas as pd | |
| import random | |
| import plotly.graph_objects as go | |
| # 1. Load the trained pipeline | |
| model = joblib.load('model_pipeline.joblib') | |
| # Master list of heavy battery-draining apps | |
| APP_LIST = [ | |
| "WhatsApp", "Instagram", "PUBG Mobile", "TikTok", | |
| "Snapchat", "YouTube", "Facebook", "Genshin Impact", | |
| "Spotify", "Google Maps", "Netflix", "Twitter/X" | |
| ] | |
| device_model_list = [ | |
| "Google Pixel 5", "iPhone 12", "OnePlus 9", "Samsung Galaxy S21", "Xiaomi Mi 11" | |
| ] | |
| # 2. Define the prediction and visualization function | |
| def predict_and_plot(screen_time, app_time, apps_installed, data_usage, age, device_model): | |
| # Prepare data | |
| 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 | |
| prediction = model.predict(input_data)[0] | |
| # --- Create Interactive Gauge Chart --- | |
| bar_color = "red" if prediction > 2006 else "#00cc66" | |
| fig = go.Figure(go.Indicator( | |
| mode = "gauge+number", | |
| value = prediction, | |
| number = {"suffix": " mAh"}, | |
| domain = {'x': [0, 1], 'y': [0, 1]}, | |
| title = {'text': "Predicted Daily Drain", 'font': {'size': 20}}, | |
| gauge = { | |
| 'axis': {'range': [0, 4000], 'tickwidth': 1, 'tickcolor': "darkblue"}, | |
| 'bar': {'color': bar_color}, | |
| 'bgcolor': "white", | |
| 'borderwidth': 2, | |
| 'bordercolor': "#e5e7eb", | |
| 'steps': [ | |
| {'range': [0, 2006], 'color': '#e6ffe6'}, # Safe zone | |
| {'range': [2006, 4000], 'color': '#ffe6e6'} # Danger zone | |
| ], | |
| 'threshold': { | |
| 'line': {'color': "red", 'width': 4}, | |
| 'thickness': 0.75, | |
| 'value': 2006 | |
| } | |
| } | |
| )) | |
| fig.update_layout(height=350, margin=dict(l=20, r=20, t=50, b=20)) | |
| # --- Generate Warnings --- | |
| warning_text = "β System Optimized.\nBattery usage is in a healthy range. No action required." | |
| if prediction > 2006: | |
| # Pick random apps to stop | |
| apps_string = ", ".join(random.sample(APP_LIST, k=random.randint(3, 4))) | |
| # 1. Trigger Native Flash Warning | |
| gr.Warning("CRITICAL: Battery drain limit exceeded!") | |
| # 2. Update the text box with the specific apps | |
| warning_text = f"β οΈ CRITICAL DRAIN ALERT!\nYour usage is above the 2006 mAh safety limit.\n\nRECOMMENDATION: Force stop these background apps to save battery:\nπ {apps_string}" | |
| return fig, warning_text | |
| # 3. Build a Clean, Modern UI | |
| with gr.Blocks(theme=gr.themes.Ocean()) as app: | |
| gr.Markdown("# π Mobile Battery Drain Predictor Dashboard") | |
| gr.Markdown("Adjust the user metrics below, then click **Predict** to run the simulation.") | |
| with gr.Row(): | |
| # LEFT COLUMN: Inputs | |
| with gr.Column(scale=1): | |
| device_input = gr.Dropdown(choices=device_model_list, label="π± Device Model", value="Google Pixel 5") | |
| screen_time_input = gr.Slider(minimum=0, maximum=24, value=4.5, step=0.5, label="πΊ Screen On Time (hours/day)") | |
| app_time_input = gr.Slider(minimum=0, maximum=1440, value=200, step=10, label="β³ App Usage Time (min/day)") | |
| apps_installed_input = gr.Slider(minimum=0, maximum=150, value=50, step=1, label="π¦ Number of Apps Installed") | |
| data_usage_input = gr.Slider(minimum=0, maximum=5000, value=1000, step=100, label="π Data Usage (MB/day)") | |
| age_input = gr.Number(label="π€ Phone Age", value=30) | |
| # --- NEW: Submit Button Added Here --- | |
| submit_btn = gr.Button("π Predict Battery Drain", variant="primary") | |
| # RIGHT COLUMN: Gauge Chart & Warnings | |
| with gr.Column(scale=1): | |
| # Interactive Gauge Chart | |
| gauge_output = gr.Plot() | |
| # Text box for the warnings | |
| warning_output = gr.Textbox(label="System Status", lines=6) | |
| # --- Button Interactivity --- | |
| inputs_list = [screen_time_input, app_time_input, apps_installed_input, data_usage_input, age_input, device_input] | |
| # This connects the button click to the prediction function | |
| submit_btn.click( | |
| fn=predict_and_plot, | |
| inputs=inputs_list, | |
| outputs=[gauge_output, warning_output] | |
| ) | |
| # Run once on startup so the UI isn't totally blank at the beginning | |
| app.load( | |
| fn=predict_and_plot, | |
| inputs=inputs_list, | |
| outputs=[gauge_output, warning_output] | |
| ) | |
| if __name__ == "__main__": | |
| app.launch() |