as-cle-bert commited on
Commit
5f73f88
1 Parent(s): 122fa53

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import joblib
4
+ import pandas as pd
5
+ import random
6
+
7
+ model = joblib.load('model.joblib')
8
+
9
+ def to_dataframe(body_type, gender, diet, shower, heating, transport, vehicle, social, bill, airtravel, monthlyvehicle, wastesize, wastecount, pchours, clothes, internet, efficiency, recycling, cooking):
10
+ wedontbelieveinbinary = ["male", "female"]
11
+ if gender == "other":
12
+ gender = random.choice(wedontbelieveinbinary)
13
+ data = {
14
+ "Body Type": [body_type],
15
+ "Sex": [gender],
16
+ "Diet": [diet],
17
+ "How Often Shower": [shower],
18
+ "Heating Energy Source": [heating],
19
+ "Transport": [transport],
20
+ "Vehicle Type": [vehicle],
21
+ "Social Activity": [social],
22
+ "Monthly Grocery Bill": [bill],
23
+ "Frequency of Traveling by Air": [airtravel],
24
+ "Vehicle Monthly Distance Km": [monthlyvehicle],
25
+ "Waste Bag Size": [wastesize],
26
+ "Waste Bag Weekly Count": [wastecount],
27
+ "How Long TV PC Daily Hour": [pchours],
28
+ "How Many New Clothes Monthly": [clothes],
29
+ "How Long Internet Daily Hour": [internet],
30
+ "Energy efficiency": [efficiency],
31
+ "Recycling": ["\""+(str(recycling))+"\""],
32
+ "Cooking_With": ["\""+(str(cooking))+"\""]
33
+ }
34
+ df = pd.DataFrame(data)
35
+ return df
36
+
37
+ def footprint_predictor(body_type, gender, diet, shower, heating, transport, vehicle, social, bill, airtravel, monthlyvehicle, wastesize, wastecount, pchours, clothes, internet, efficiency, recycling, cooking):
38
+ 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))
39
+ return f'{str(pred).replace("[","").replace("]","")} kg/month of CO2, which means {round(pred[0]*12/1000, 2)} ton/year'
40
+
41
+
42
+ demo = gr.Interface(
43
+ footprint_predictor,
44
+ [
45
+ gr.Radio(["overweight", "underweight", "normal", "obese"], label="What's your current body type?", info="Choose one of the following"),
46
+ gr.Radio(["male", "female", "other"], label="What's your gender?", info="Choose one of the following"),
47
+ gr.Radio(["pescatarian", "vegan", "omnivore", "vegetarian"], label="What's your diet type?", info="Choose one of the following"),
48
+ gr.Radio(["less frequently", "daily", "twice a day", "more frequently"], label="How often do you shower?", info="Choose one of the following"),
49
+ gr.Radio(["wood", "natural gas", "coal", "electricity"], label="What's the main heating source in your house?", info="Choose one of the following"),
50
+ gr.Radio(["public", "walk/bicycle", "private"], label="What transport do you use?", info="Choose one of the following"),
51
+ gr.Radio(["lpg", "diesel", "electric", "petrol", "hybrid"], label="What type is your vehicle?", info="Choose one of the following"),
52
+ gr.Radio(["often", "sometimes", "never"], label="How often do you engage in social activities?", info="Choose one of the following"),
53
+ gr.Slider(0, 520, value=173.0, label="How much do you spend on grocery, monthly?", info="Choose between 0 and 520"),
54
+ gr.Radio(["frequently", "very frequently", "never", "rarely"], label="How often do you travel by airplane?", info="Choose one of the following"),
55
+ gr.Slider(0, 20000, value=823.0, label="How many kms do you travel by car, monthly?", info="Choose between 0 and 20,000"),
56
+ gr.Radio(["extra large", "small", "medium", "large"], label="What's the size of your wastebag?", info="Choose one of the following"),
57
+ gr.Slider(0, 15, value=4.0, label="How many wastebags do you trash, weekly?", info="Choose between 0 and 15"),
58
+ 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"),
59
+ gr.Slider(0, 100, value=25.0, label="How many new clothes do you buy, monthly?", info="Choose between 0 and 100"),
60
+ gr.Slider(0, 24, value=12.0, label="How many hours do you spend on the internet, daily?", info="Choose between 0 and 24"),
61
+ gr.Radio(["Yes", "Sometimes", "No"], label="Do you use energy-saving modes for your electronic devices?", info="Choose one of the following"),
62
+ gr.CheckboxGroup(["Metal", "Glass", "Plastic", "Paper"], label="Do you recycle any of these materials?", info="Choose one or more of the following"),
63
+ gr.CheckboxGroup(["Grill", "Oven", "Microwave", "Airfryer", "Stove"], label="What do you cook with?", info="Choose one or more of the following"),
64
+ ],
65
+ "text",
66
+ examples = [
67
+ ["overweight", "male", "pescatarian", "daily", "coal", "public", "lpg", "often", 150, "rarely", 100, "medium", 3, 8, 10, 10, "Sometimes", ['Metal'], ['Stove', 'Oven']],
68
+ ["normal", "female", "vegan", "less frequently", "electricity", "private", "petrol", "often", 200, "frequently", 80, "large", 2, 9, 15, 9, "Yes", ['Metal', 'Plastic'], ['Oven']],
69
+ ],
70
+ )
71
+
72
+ if __name__ == "__main__":
73
+ demo.launch()
74
+
75
+