as-cle-bert's picture
Create app.py
5f73f88 verified
import gradio as gr
import json
import joblib
import pandas as pd
import random
model = joblib.load('model.joblib')
def to_dataframe(body_type, gender, diet, shower, heating, transport, vehicle, social, bill, airtravel, monthlyvehicle, wastesize, wastecount, pchours, clothes, internet, efficiency, recycling, cooking):
wedontbelieveinbinary = ["male", "female"]
if gender == "other":
gender = random.choice(wedontbelieveinbinary)
data = {
"Body Type": [body_type],
"Sex": [gender],
"Diet": [diet],
"How Often Shower": [shower],
"Heating Energy Source": [heating],
"Transport": [transport],
"Vehicle Type": [vehicle],
"Social Activity": [social],
"Monthly Grocery Bill": [bill],
"Frequency of Traveling by Air": [airtravel],
"Vehicle Monthly Distance Km": [monthlyvehicle],
"Waste Bag Size": [wastesize],
"Waste Bag Weekly Count": [wastecount],
"How Long TV PC Daily Hour": [pchours],
"How Many New Clothes Monthly": [clothes],
"How Long Internet Daily Hour": [internet],
"Energy efficiency": [efficiency],
"Recycling": ["\""+(str(recycling))+"\""],
"Cooking_With": ["\""+(str(cooking))+"\""]
}
df = pd.DataFrame(data)
return df
def footprint_predictor(body_type, gender, diet, shower, heating, transport, vehicle, social, bill, airtravel, monthlyvehicle, wastesize, wastecount, pchours, clothes, internet, efficiency, recycling, cooking):
pred = model.predict(to_dataframe(body_type, gender, diet, shower, heating, transport, vehicle, social, bill, airtravel, monthlyvehicle, wastesize, wastecount, pchours, clothes, internet, efficiency, recycling, cooking))
return f'{str(pred).replace("[","").replace("]","")} kg/month of CO2, which means {round(pred[0]*12/1000, 2)} ton/year'
demo = gr.Interface(
footprint_predictor,
[
gr.Radio(["overweight", "underweight", "normal", "obese"], label="What's your current body type?", info="Choose one of the following"),
gr.Radio(["male", "female", "other"], label="What's your gender?", info="Choose one of the following"),
gr.Radio(["pescatarian", "vegan", "omnivore", "vegetarian"], label="What's your diet type?", info="Choose one of the following"),
gr.Radio(["less frequently", "daily", "twice a day", "more frequently"], label="How often do you shower?", info="Choose one of the following"),
gr.Radio(["wood", "natural gas", "coal", "electricity"], label="What's the main heating source in your house?", info="Choose one of the following"),
gr.Radio(["public", "walk/bicycle", "private"], label="What transport do you use?", info="Choose one of the following"),
gr.Radio(["lpg", "diesel", "electric", "petrol", "hybrid"], label="What type is your vehicle?", info="Choose one of the following"),
gr.Radio(["often", "sometimes", "never"], label="How often do you engage in social activities?", info="Choose one of the following"),
gr.Slider(0, 520, value=173.0, label="How much do you spend on grocery, monthly?", info="Choose between 0 and 520"),
gr.Radio(["frequently", "very frequently", "never", "rarely"], label="How often do you travel by airplane?", info="Choose one of the following"),
gr.Slider(0, 20000, value=823.0, label="How many kms do you travel by car, monthly?", info="Choose between 0 and 20,000"),
gr.Radio(["extra large", "small", "medium", "large"], label="What's the size of your wastebag?", info="Choose one of the following"),
gr.Slider(0, 15, value=4.0, label="How many wastebags do you trash, weekly?", info="Choose between 0 and 15"),
gr.Slider(0, 24, value=12.0, label="How many hours do you spend on screens (PC, TV...) daily?", info="Choose between 0 and 24"),
gr.Slider(0, 100, value=25.0, label="How many new clothes do you buy, monthly?", info="Choose between 0 and 100"),
gr.Slider(0, 24, value=12.0, label="How many hours do you spend on the internet, daily?", info="Choose between 0 and 24"),
gr.Radio(["Yes", "Sometimes", "No"], label="Do you use energy-saving modes for your electronic devices?", info="Choose one of the following"),
gr.CheckboxGroup(["Metal", "Glass", "Plastic", "Paper"], label="Do you recycle any of these materials?", info="Choose one or more of the following"),
gr.CheckboxGroup(["Grill", "Oven", "Microwave", "Airfryer", "Stove"], label="What do you cook with?", info="Choose one or more of the following"),
],
"text",
examples = [
["overweight", "male", "pescatarian", "daily", "coal", "public", "lpg", "often", 150, "rarely", 100, "medium", 3, 8, 10, 10, "Sometimes", ['Metal'], ['Stove', 'Oven']],
["normal", "female", "vegan", "less frequently", "electricity", "private", "petrol", "often", 200, "frequently", 80, "large", 2, 9, 15, 9, "Yes", ['Metal', 'Plastic'], ['Oven']],
],
)
if __name__ == "__main__":
demo.launch()