LLaMa / app.py
Vetri04's picture
Update app.py
a0cd936 verified
import pandas as pd
import gradio as gr
# Load the dataset
data = pd.read_csv('flat-ui__data-Tue Oct 15 2024.csv')
# Function to handle query parsing and execute appropriate logic
def emissions_data_function_call(query: str) -> str:
query = query.lower()
# Handle highest/lowest total emissions queries
if "highest total emissions" in query:
return get_highest_emissions()
if "lowest total emissions" in query:
return get_lowest_emissions()
# Handle per capita emissions queries
if "per capita emissions" in query:
return get_highest_per_capita_emissions()
# Handle fuel type-specific queries (solid, liquid, gas, cement)
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')
# Handle country-specific emissions queries
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."
# Handle comparison queries (e.g., comparing emissions between two countries)
if "compare" in query:
return handle_comparison(query)
# Handle queries about averages
if "average emissions" in query:
return get_average_emissions()
# Handle year-specific queries (e.g., emissions for a specific year)
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."
# Helper function to get the highest total emissions
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."
# Helper function to get the lowest total emissions
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."
# Helper function to get highest per capita emissions
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."
# Helper function to get total emissions for a specific fuel type
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."
# Helper function to get country-specific emissions
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."
# Extract the country from the query
def extract_country(query):
# List of countries from the dataset for validation (you could load this dynamically)
countries = data['Country'].str.lower().tolist()
for country in countries:
if country in query:
return country
return None
# Helper function to compare emissions between two countries
def handle_comparison(query):
try:
# Extract the countries from the query
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."
# Helper function to get average emissions
def get_average_emissions():
average_total = data['Total'].mean()
return f"The average total emissions across all countries is {average_total} metric tons."
# Helper function to get emissions for a specific year
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."
# Helper function to extract the year from the query
def extract_year(query):
try:
words = query.split()
for word in words:
if word.isdigit():
return int(word)
return None
except:
return None