File size: 4,846 Bytes
c49d8fe
 
 
e8f583a
3b8f14e
c49d8fe
 
 
 
2ed47d4
 
 
 
 
 
 
e8f583a
 
 
 
3b8f14e
 
e8f583a
c49d8fe
 
 
 
 
 
 
 
 
e8f583a
c49d8fe
2ed47d4
3b8f14e
90b6eb1
3b8f14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90b6eb1
3b8f14e
 
2ed47d4
3b8f14e
 
2ed47d4
3b8f14e
 
 
 
c49d8fe
3b8f14e
 
e8f583a
3b8f14e
b9d17c2
e8f583a
 
3b8f14e
e8f583a
3b8f14e
 
 
 
 
3db6b64
e8f583a
b9d17c2
 
 
3b8f14e
e8f583a
3b8f14e
 
e8f583a
3b8f14e
 
c49d8fe
b9d17c2
3b8f14e
 
b9d17c2
 
 
 
 
 
3b8f14e
b9d17c2
3b8f14e
 
 
 
e8f583a
c49d8fe
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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()