Ifeanyi commited on
Commit
6649241
·
verified ·
1 Parent(s): e18f7fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -18
app.py CHANGED
@@ -1,31 +1,97 @@
1
  import google.generativeai as genai
2
  import gradio as gr
3
- import numpy as np
4
- import PIL.Image
5
 
6
- genai.configure(api_key="AIzaSyA7tPavobVN5_3-BJ0qhFT5HVjO4V19QWk")
 
 
 
 
 
 
 
7
 
8
- def ImageChat(image, prompt):
9
 
10
- # load model
11
- model = genai.GenerativeModel("gemini-1.5-flash")
12
 
13
- # check image file and convert to a Numpy array
14
- if isinstance(image, np.ndarray):
 
 
 
 
 
15
 
16
- img = PIL.Image.fromarray(image)
17
- else:
18
- img = PIL.Image.open(image)
19
 
20
- response = model.generate_content([prompt, img])
 
 
 
 
21
 
22
- return response.text
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
 
 
24
 
25
- app = gr.Interface(ImageChat,
26
- inputs = [gr.Image(label = "Image"), gr.Text(label = "Prompt")],
27
- outputs = gr.Text(label = "Response"),
28
- title = "Image Chat",
29
- theme = "Taithrah/Minimal")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  app.launch()
 
1
  import google.generativeai as genai
2
  import gradio as gr
 
 
3
 
4
+ def carbonFootPrintAI(monthly_oil_bill: int,
5
+ monthly_gas_bill: int,
6
+ monthly_electricity_bill: int,
7
+ total_yearly_mileage_on_car: int,
8
+ number_of_flights_in_past_year_less_or_equal_4hours: int,
9
+ number_of_flights_in_past_year_more_or_equal_4hours: int,
10
+ recycle_newspaper=False,
11
+ recycle_aluminium_and_tin=False):
12
 
13
+ genai.configure(api_key="AIzaSyAs726sBS7iWEO7ql2LFbvCxJ_bzbpDbiI")
14
 
15
+ model = genai.GenerativeModel("gemini-pro")
 
16
 
17
+ # Calculate base values
18
+ e_bill = monthly_electricity_bill * 105
19
+ o_bill = monthly_oil_bill * 113
20
+ g_bill = monthly_gas_bill * 105
21
+ y_mileage = total_yearly_mileage_on_car * 0.79
22
+ f_less_than_4hours = number_of_flights_in_past_year_less_or_equal_4hours * 1100
23
+ f_more_than_4hours = number_of_flights_in_past_year_more_or_equal_4hours * 4400
24
 
25
+ # Total footprint without recycling adjustments
26
+ total_footprint = e_bill + o_bill + g_bill + y_mileage + f_less_than_4hours + f_more_than_4hours
 
27
 
28
+ # Add penalties if not recycling
29
+ if not recycle_newspaper:
30
+ total_footprint += 184
31
+ if not recycle_aluminium_and_tin:
32
+ total_footprint += 166
33
 
34
+ if total_footprint < 6000:
35
+ prompt1 = """
36
+ commend for excellent work keeping total carbon footprint down at {} and recommend suggestions to keep it that way.
37
+ Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
38
+ less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
39
+ """.format(total_footprint)
40
+ response1 = model.generate_content(prompt1)
41
+ return [total_footprint, response1.text]
42
 
43
+ elif total_footprint > 6000 and total_footprint < 15999:
44
+ prompt2 = """
45
+ commend for keeping total carbon footprint down at {} and recommend practical suggestions to bring it down further.
46
+ Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
47
+ less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
48
+ """.format(total_footprint)
49
+ response2 = model.generate_content(prompt2)
50
+ return [total_footprint, response2.text]
51
 
52
+ elif total_footprint > 16000 and total_footprint < 22000:
53
+ prompt3 = """
54
+ commend for keeping total carbon footprint at an average of {} and recommend useful suggestions to make sure it doesn't get higher than that.
55
+ Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
56
+ less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
57
+ """.format(total_footprint)
58
+ response3 = model.generate_content(prompt3)
59
+ return [total_footprint, response3.text]
60
+
61
+ elif total_footprint > 22000:
62
+ prompt4 = """
63
+ urgently recommend drastic and practical measures to bring carbon footprint down from {}. Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
64
+ less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
65
+ """.format(total_footprint)
66
+ response4 = model.generate_content(prompt4)
67
+ return [total_footprint, response4.text]
68
+
69
+
70
+ css = """
71
+ .app {
72
+ border-width: 3px;
73
+ border-color: forestgreen;
74
+ }
75
+ """
76
+ # Gradio Interface
77
+ app = gr.Interface(
78
+ fn=carbonFootPrintAI,
79
+ inputs=[
80
+ gr.Number(label="Monthly Gas Bill", elem_classes="app"),
81
+ gr.Number(label="Monthly Oil Bill", elem_classes="app"),
82
+ gr.Number(label="Monthly Electricity Bill", elem_classes="app"),
83
+ gr.Slider(label="Total Yearly Mileage on Car", value = 0, minimum = 0, maximum = 50000, step = 1, elem_classes="app"),
84
+ gr.Slider(label="Number of Flights Less Than or Equal to 4 Hours", value = 0, minimum = 0, maximum = 100, step = 1, elem_classes="app"),
85
+ gr.Slider(label="Number of Flights More Than or Equal to 4 Hours", value = 0, minimum = 0, maximum = 100, step = 1, elem_classes="app"),
86
+ gr.Checkbox(label="Do You Recycle Newspaper?", elem_classes="app"),
87
+ gr.Checkbox(label="Do You Recycle Aluminium and Tin?", elem_classes="app")
88
+ ],
89
+ outputs=[
90
+ gr.Number(label="Total Carbon Footprint", elem_classes="app"),
91
+ gr.Markdown(label="Recommendation", elem_classes="app")
92
+ ],
93
+ theme = gr.themes.Soft(),
94
+ title = "&#127757; &#127807; Carbon Footprint Calculator &#127757; &#127807;"
95
+ )
96
 
97
  app.launch()