Skip to content

Commit

Permalink
Merge pull request #6 from SaM-92/dev_b
Browse files Browse the repository at this point in the history
fixed issue with negative values for net import
  • Loading branch information
SaM-92 authored Feb 27, 2024
2 parents c540ecb + 752a13b commit a091c01
Showing 4 changed files with 91 additions and 35 deletions.
Binary file modified .DS_Store
Binary file not shown.
23 changes: 19 additions & 4 deletions subs/energy_api.py
Original file line number Diff line number Diff line change
@@ -183,12 +183,27 @@ def fuel_mix():
fuel_mix_eirgrid["FieldName"] = fuel_mix_eirgrid["FieldName"].map(
descriptive_names
)
total = sum(fuel_mix_eirgrid["Value"])
percentages = [(value / total) * 100 for value in fuel_mix_eirgrid["Value"]]
fuel_mix_eirgrid["ValueForPercentage"] = fuel_mix_eirgrid["Value"].apply(
lambda x: max(x, 0)
)
total_for_percentage = sum(fuel_mix_eirgrid["ValueForPercentage"])
percentages = [
(value / total_for_percentage) * 100 if value > 0 else 0
for value in fuel_mix_eirgrid["ValueForPercentage"]
]
fuel_mix_eirgrid["Percentage"] = percentages
return fuel_mix_eirgrid
if (
fuel_mix_eirgrid.loc[
fuel_mix_eirgrid["FieldName"] == "Net Import", "Value"
].values[0]
< 0
):
net_import = "exporting"
else:
net_import = "importing"
return fuel_mix_eirgrid, net_import
except:
return None
return None, None


def classify_status(value, min_val, max_val):
75 changes: 53 additions & 22 deletions subs/openai_script.py
Original file line number Diff line number Diff line change
@@ -200,32 +200,63 @@ def get_energy_actions(text):
return energy_saving_actions


def create_fuel_mix_prompt(date, fuel_mix_data):
def create_fuel_mix_prompt(date, fuel_mix_data, net_import_status):
# Preparing fuel mix data string
# Correcting the list comprehension to match the data structure
fuel_mix_details = "\n".join(
[
f"- {fuel_mix_data['FieldName'][i]}: {fuel_mix_data['Value'][i]} MWh ({fuel_mix_data['Percentage'][i]:.1f}%)"
for i in range(len(fuel_mix_data["FieldName"]))

if net_import_status == "importing":
fuel_mix_details = "\n".join(
[
f"- {fuel_mix_data['FieldName'][i]}: {fuel_mix_data['Value'][i]} MWh ({fuel_mix_data['Percentage'][i]:.1f}%)"
for i in range(len(fuel_mix_data["FieldName"]))
]
)
prompt_text = (
f"📅 Date: {date}\n"
f"🔋 Fuel Mix Data (MWh & Percentage):\n\n"
f"{fuel_mix_details}\n\n"
"Based on the above data, write a short report about the status of the energy system over the last 24 hours. "
"Please summarize the contribution of each fuel source to the overall mix and any notable trends. "
"Use the following structure for your response, incorporating the specified emojis to highlight each fuel source:\n\n"
"📋 Fuel Mix Status:\n"
"- 🪨 Coal: [percentage]%\n"
"- 🌬️ Gas: [percentage]%\n"
"- ⚡ Net Import: [percentage]%\n"
"- 🛢️ Other Fossil: [percentage]%\n"
"- 🌿 Renewables: [percentage]%\n\n"
"Note: Replace [percentage] with the actual percentages from the data. "
"Avoid using asterisks (*) in your response and stick to the names and format provided."
)
elif net_import_status == "exporting":
export_value = fuel_mix_data.loc[
fuel_mix_data["FieldName"] == "Net Import", "Value"
].values[0]
filtered_fuel_mix_data = fuel_mix_data[
fuel_mix_data["FieldName"] != "Net Import"
]
)
fuel_mix_details = "\n".join(
[
f"- {filtered_fuel_mix_data['FieldName'][idx]}: {filtered_fuel_mix_data['Value'][idx]} MWh ({filtered_fuel_mix_data['Percentage'][idx]:.1f}%)"
for idx in filtered_fuel_mix_data.index # Use the actual indices
]
)

prompt_text = (
f"📅 Date: {date}\n"
f"🔋 Fuel Mix Data (MWh & Percentage):\n\n"
f"{fuel_mix_details}\n\n"
"Based on the above data, write a short report about the current status of the energy system. "
"Please summarize the contribution of each fuel source to the overall mix and any notable trends. "
"Use the following structure for your response, incorporating the specified emojis to highlight each fuel source:\n\n"
"📋 Fuel Mix Status:\n"
"- 🪨 Coal: [percentage]%\n"
"- 🌬️ Gas: [percentage]%\n"
"- ⚡ Net Import: [percentage]%\n"
"- 🛢️ Other Fossil: [percentage]%\n"
"- 🌿 Renewables: [percentage]%\n\n"
"Note: Replace [percentage] with the actual percentages from the data. "
"Avoid using asterisks (*) in your response and stick to the names and format provided."
)
prompt_text = (
f"📅 Date: {date}\n"
f"🔋 Fuel Mix Data (MWh & Percentage):\n\n"
f"{fuel_mix_details}\n\n"
"Based on the above data, write a short report about the status of the energy system over the last 24 hours. "
"Please summarize the contribution of each fuel source to the overall mix and any notable trends. "
"Use the following structure for your response, incorporating the specified emojis to highlight each fuel source:\n\n"
"📋 Fuel Mix Status:\n"
"- 🪨 Coal: [percentage]%\n"
"- 🌬️ Gas: [percentage]%\n"
"- 🛢️ Other Fossil: [percentage]%\n"
"- 🌿 Renewables: [percentage]%\n\n"
f"- ⚡ Ireland is currently exporting electricity to the UK, with the average export being {export_value} MWh over the last 24 hours. \n"
"Note: Replace [percentage] with the actual percentages from the data. "
"Avoid using asterisks (*) in your response and stick to the names and format provided."
)

return prompt_text

28 changes: 19 additions & 9 deletions subs/telegram_func.py
Original file line number Diff line number Diff line change
@@ -93,7 +93,8 @@ async def telegram_carbon_intensity(update, context, user_first_name):
del audio_msg


async def pie_chart_fuel_mix(update, context, fuel_mix_eirgrid, current_time):
async def pie_chart_fuel_mix(update, context, df, net_import_status, current_time):

# Adjusting colors to be less vibrant (more pastel-like)
pastel_colors = {
"Coal": "#3B3434", # Coal - less vibrant gray
@@ -104,16 +105,23 @@ async def pie_chart_fuel_mix(update, context, fuel_mix_eirgrid, current_time):
}
# print(fuel_mix_eirgrid)
# Mapping the pastel colors to the dataframe's FieldName
pastel_pie_colors = [
pastel_colors[field] for field in fuel_mix_eirgrid["FieldName"]
]
pastel_pie_colors = [pastel_colors[field] for field in df["FieldName"]]
# Filter df based on net_import_status
if net_import_status == "importing":
pie_data = df
elif net_import_status == "exporting":
pie_data = df[df["FieldName"] != "Net Import"]
# Update pastel_pie_colors to match the filtered data
pastel_pie_colors = [pastel_colors[field] for field in pie_data["FieldName"]]

# Generate custom_labels from the (potentially filtered) pie_data
custom_labels = [
f'{row["FieldName"]}\n({row["Percentage"]:.1f}%)'
for index, row in fuel_mix_eirgrid.iterrows()
for index, row in pie_data.iterrows()
]
plt.figure(figsize=(7, 7))
plt.pie(
fuel_mix_eirgrid["Value"],
pie_data["Percentage"],
labels=custom_labels,
startangle=140,
colors=pastel_pie_colors,
@@ -135,14 +143,16 @@ async def pie_chart_fuel_mix(update, context, fuel_mix_eirgrid, current_time):


async def telegram_fuel_mix(update, context, user_first_name):
fuel_mix_eirgrid = fuel_mix()
fuel_mix_eirgrid, net_import_status = fuel_mix()
now = round_time(datetime.datetime.now())

promopt_for_fuel_mix = create_fuel_mix_prompt(now, fuel_mix_eirgrid)
promopt_for_fuel_mix = create_fuel_mix_prompt(
now, fuel_mix_eirgrid, net_import_status
)
fuel_mix_response_from_gpt = opt_gpt_summarise(promopt_for_fuel_mix)

await update.message.reply_text(fuel_mix_response_from_gpt)
await pie_chart_fuel_mix(update, context, fuel_mix_eirgrid, now)
await pie_chart_fuel_mix(update, context, fuel_mix_eirgrid, net_import_status, now)
audio_msg = generate_voice(fuel_mix_response_from_gpt)

await context.bot.send_voice(

0 comments on commit a091c01

Please sign in to comment.