|
import pandas as pd |
|
import gradio as gr |
|
|
|
|
|
|
|
data = pd.read_csv('flat-ui__data-Tue Oct 15 2024.csv') |
|
|
|
|
|
def emissions_data_function_call(query: str) -> str: |
|
query = query.lower() |
|
|
|
|
|
if "highest total emissions" in query: |
|
return get_highest_emissions() |
|
|
|
if "lowest total emissions" in query: |
|
return get_lowest_emissions() |
|
|
|
|
|
if "per capita emissions" in query: |
|
return get_highest_per_capita_emissions() |
|
|
|
|
|
if "solid fuel emissions" in query: |
|
return get_fuel_emissions('Solid Fuel') |
|
|
|
if "liquid fuel emissions" in query: |
|
return get_fuel_emissions('Liquid Fuel') |
|
|
|
if "gas fuel emissions" in query: |
|
return get_fuel_emissions('Gas Fuel') |
|
|
|
if "cement emissions" in query: |
|
return get_fuel_emissions('Cement') |
|
|
|
|
|
if "emissions for" in query: |
|
country = extract_country(query) |
|
if country: |
|
return get_country_emissions(country) |
|
else: |
|
return "Sorry, I couldn't find the country in your query." |
|
|
|
|
|
if "compare" in query: |
|
return handle_comparison(query) |
|
|
|
|
|
if "average emissions" in query: |
|
return get_average_emissions() |
|
|
|
|
|
if "emissions in" in query: |
|
year = extract_year(query) |
|
if year: |
|
return get_emissions_for_year(year) |
|
else: |
|
return "Please specify a valid year in your query." |
|
|
|
return "Sorry, I couldn't understand your query. Please ask about emissions, fuel types, or specific countries." |
|
|
|
|
|
def get_highest_emissions(): |
|
country = data.loc[data['Total'].idxmax(), 'Country'] |
|
total_emission = data['Total'].max() |
|
return f"The country with the highest total emissions is {country} with {total_emission} metric tons." |
|
|
|
|
|
def get_lowest_emissions(): |
|
country = data.loc[data['Total'].idxmin(), 'Country'] |
|
total_emission = data['Total'].min() |
|
return f"The country with the lowest total emissions is {country} with {total_emission} metric tons." |
|
|
|
|
|
def get_highest_per_capita_emissions(): |
|
country = data.loc[data['Per Capita'].idxmax(), 'Country'] |
|
per_capita = data['Per Capita'].max() |
|
return f"The country with the highest per capita emissions is {country} with {per_capita} metric tons per person." |
|
|
|
|
|
def get_fuel_emissions(fuel_type): |
|
total_fuel = data[fuel_type].sum() |
|
return f"The total emissions from {fuel_type.lower()} across all countries is {total_fuel} metric tons." |
|
|
|
|
|
def get_country_emissions(country): |
|
row = data[data['Country'].str.lower() == country.lower()] |
|
if row.empty: |
|
return f"No emissions data found for {country}." |
|
total_emission = row['Total'].values[0] |
|
return f"{country} has total emissions of {total_emission} metric tons." |
|
|
|
|
|
def extract_country(query): |
|
|
|
countries = data['Country'].str.lower().tolist() |
|
for country in countries: |
|
if country in query: |
|
return country |
|
return None |
|
|
|
|
|
def handle_comparison(query): |
|
try: |
|
|
|
countries = query.split("compare between")[1].split("and") |
|
country1 = countries[0].strip() |
|
country2 = countries[1].strip() |
|
|
|
row1 = data[data['Country'].str.lower() == country1.lower()] |
|
row2 = data[data['Country'].str.lower() == country2.lower()] |
|
|
|
if row1.empty or row2.empty: |
|
return "One or both of the countries were not found in the dataset." |
|
|
|
emission1 = row1['Total'].values[0] |
|
emission2 = row2['Total'].values[0] |
|
|
|
if emission1 > emission2: |
|
return f"{country1} has higher total emissions ({emission1} metric tons) than {country2} ({emission2} metric tons)." |
|
else: |
|
return f"{country2} has higher total emissions ({emission2} metric tons) than {country1} ({emission1} metric tons)." |
|
except: |
|
return "Sorry, I couldn't process the comparison query. Please specify valid countries." |
|
|
|
|
|
def get_average_emissions(): |
|
average_total = data['Total'].mean() |
|
return f"The average total emissions across all countries is {average_total} metric tons." |
|
|
|
|
|
def get_emissions_for_year(year): |
|
year_data = data[data['Year'] == year] |
|
if year_data.empty: |
|
return f"No emissions data found for the year {year}." |
|
total_emission = year_data['Total'].sum() |
|
return f"The total emissions for all countries in {year} is {total_emission} metric tons." |
|
|
|
|
|
def extract_year(query): |
|
try: |
|
words = query.split() |
|
for word in words: |
|
if word.isdigit(): |
|
return int(word) |
|
return None |
|
except: |
|
return None |
|
|
|
|