Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,16 @@ import random
|
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import plotly.graph_objects as go
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
def analyze_energy_data(energy_data, location, language):
|
13 |
appliances = {}
|
14 |
total_kwh = 0
|
@@ -66,62 +71,8 @@ def analyze_energy_data(energy_data, location, language):
|
|
66 |
savings # Return savings for ROI calculation
|
67 |
)
|
68 |
|
69 |
-
#
|
70 |
-
leaderboard = {"User1": 30, "User2": 25, "User3": 20}
|
71 |
-
|
72 |
-
def update_leaderboard(user_name, reduction_percentage):
|
73 |
-
leaderboard[user_name] = leaderboard.get(user_name, 0) + reduction_percentage
|
74 |
-
sorted_leaderboard = sorted(leaderboard.items(), key=lambda x: x[1], reverse=True)
|
75 |
-
leaderboard_text = "\n".join([f"{i+1}. {user}: {points} points" for i, (user, points) in enumerate(sorted_leaderboard)])
|
76 |
-
badge = "Gold" if reduction_percentage > 30 else "Silver" if reduction_percentage > 20 else "Bronze"
|
77 |
-
return leaderboard_text, f"Badge earned: {badge}"
|
78 |
-
|
79 |
-
# IoT-based Smart Device Integration (Simulated)
|
80 |
-
def fetch_smart_device_data():
|
81 |
-
data = {
|
82 |
-
"AC": random.randint(350, 500),
|
83 |
-
"Lighting": random.randint(100, 200),
|
84 |
-
"Refrigerator": random.randint(180, 250),
|
85 |
-
}
|
86 |
-
return "\n".join([f"{k}: {v} kWh" for k, v in data.items()])
|
87 |
-
|
88 |
-
# Customized Energy Visualization: Interactive Bar Chart
|
89 |
-
def visualize_energy_data(appliances):
|
90 |
-
df = pd.DataFrame(appliances.items(), columns=["Appliance", "Energy (kWh)"])
|
91 |
-
fig = go.Figure(data=[go.Bar(
|
92 |
-
x=df["Appliance"],
|
93 |
-
y=df["Energy (kWh)"],
|
94 |
-
marker=dict(color=['#FF6347' if x > 300 else '#32CD32' for x in df["Energy (kWh)"]]) # High usage appliances in red, low in green
|
95 |
-
)])
|
96 |
-
fig.update_layout(
|
97 |
-
title="Appliance-wise Energy Usage",
|
98 |
-
xaxis_title="Appliance",
|
99 |
-
yaxis_title="Energy (kWh)",
|
100 |
-
hovermode='x unified',
|
101 |
-
)
|
102 |
-
return fig
|
103 |
|
104 |
-
# ROI Calculator
|
105 |
-
def calculate_roi(initial_investment, savings):
|
106 |
-
try:
|
107 |
-
roi = (savings / initial_investment) * 100
|
108 |
-
except ZeroDivisionError:
|
109 |
-
roi = 0.0
|
110 |
-
return f"Your ROI is {roi:.2f}% based on an initial investment of {initial_investment} PKR and savings of {savings:.2f} PKR."
|
111 |
-
|
112 |
-
# Chatbot Interface
|
113 |
-
def chatbot_interface(home_size, location, energy_data, language, user_name, reduction_percentage, initial_investment):
|
114 |
-
energy_analysis, tips, weather_tips, alerts, carbon_output, ai_recommendation, savings = analyze_energy_data(energy_data, location, language)
|
115 |
-
appliances = {
|
116 |
-
appliance: float(kwh.strip(" kWh"))
|
117 |
-
for appliance, kwh in [line.split(":") for line in energy_data.strip().split("\n")]
|
118 |
-
}
|
119 |
-
energy_chart = visualize_energy_data(appliances)
|
120 |
-
leaderboard_text, badge = update_leaderboard(user_name, reduction_percentage)
|
121 |
-
roi_output = calculate_roi(initial_investment, savings)
|
122 |
-
return energy_analysis, tips, weather_tips, alerts, carbon_output, ai_recommendation, energy_chart, leaderboard_text, badge, roi_output
|
123 |
-
|
124 |
-
# Build UI with Gradio
|
125 |
def build_ui():
|
126 |
with gr.Blocks() as demo:
|
127 |
with gr.Row():
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import plotly.graph_objects as go
|
6 |
|
7 |
+
# Attempt to load the GPT-2 model
|
8 |
+
try:
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("gpt2")
|
11 |
+
except ImportError as e:
|
12 |
+
tokenizer = None
|
13 |
+
model = None
|
14 |
+
print(f"Warning: {e}. GPT-2 model won't be used in this session.")
|
15 |
+
|
16 |
+
# Functionality remains the same; no GPT-2 model inference is used
|
17 |
def analyze_energy_data(energy_data, location, language):
|
18 |
appliances = {}
|
19 |
total_kwh = 0
|
|
|
71 |
savings # Return savings for ROI calculation
|
72 |
)
|
73 |
|
74 |
+
# Other functions remain unchanged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
def build_ui():
|
77 |
with gr.Blocks() as demo:
|
78 |
with gr.Row():
|