Ifeanyi commited on
Commit
662968b
·
verified ·
1 Parent(s): c3d65e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -119
app.py CHANGED
@@ -1,126 +1,23 @@
1
- import google.generativeai as genai
2
  import gradio as gr
3
- import os
4
 
5
- def carbonFootPrintAI(monthly_oil_bill: int,
6
- monthly_gas_bill: int,
7
- monthly_electricity_bill: int,
8
- total_yearly_mileage_on_car: int,
9
- number_of_flights_in_past_year_less_or_equal_4hours: int,
10
- number_of_flights_in_past_year_more_or_equal_4hours: int,
11
- recycle_newspaper=False,
12
- recycle_aluminium_and_tin=False):
13
-
14
- """
15
- This function calculates the carbon footprint based on the following parameters:
16
- - Monthly oil bill in dollars
17
- - Monthly gas bill in dollars
18
- - Monthly electricity bill in dollars
19
- - Total yearly mileage on car in miles
20
- - Number of flights in the past year that are less than or equal to 4 hours
21
- - Number of flights in the past year that are more than or equal to 4 hours
22
- - recycle_newspaper: Indicating whether or not the user recycles newspaper
23
- - recycle_aluminium_and_tin: Indicating whether or not the user recycles aluminium and tin.
24
-
25
- Args:
26
- monthly_oil_bill (int): Monthly oil bill in dollars
27
- monthly_gas_bill (int): Monthly gas bill in dollars
28
- monthly_electricity_bill (int): Monthly electricity bill in dollars
29
- total_yearly_mileage_on_car (int): Total yearly mileage on car in miles
30
- number_of_flights_in_past_year_less_or_equal_4hours (int): Number of flights in the past year that are less than or equal to 4 hours
31
- number_of_flights_in_past_year_more_or_equal_4hours (int): Number of flights in the past year that are more than or equal to 4 hours
32
- recycle_newspaper (bool): Boolean indicating if the user recycles newspaper
33
- recycle_aluminium_and_tin (bool): Boolean indicating if the user recycles aluminium and tin.
34
 
 
 
 
 
35
  Returns:
36
- - total_footprint (int): The total carbon footprint calculated based on the inputs
37
- - response (str): A recommendation message based on the total carbon footprint.
38
  """
39
-
40
- api_key = os.getenv("GEMINI_API_KEY")
41
-
42
- genai.configure(api_key=api_key)
43
-
44
- model = genai.GenerativeModel("gemini-1.5-flash")
45
-
46
- # Calculate base values
47
- e_bill = monthly_electricity_bill * 105
48
- o_bill = monthly_oil_bill * 113
49
- g_bill = monthly_gas_bill * 105
50
- y_mileage = total_yearly_mileage_on_car * 0.79
51
- f_less_than_4hours = number_of_flights_in_past_year_less_or_equal_4hours * 1100
52
- f_more_than_4hours = number_of_flights_in_past_year_more_or_equal_4hours * 4400
53
-
54
- # Total footprint without recycling adjustments
55
- total_footprint = e_bill + o_bill + g_bill + y_mileage + f_less_than_4hours + f_more_than_4hours
56
-
57
- # Add penalties if not recycling
58
- if not recycle_newspaper:
59
- total_footprint += 184
60
- if not recycle_aluminium_and_tin:
61
- total_footprint += 166
62
-
63
- if total_footprint < 6000:
64
- prompt1 = """
65
- commend for excellent work keeping total carbon footprint down at {} and recommend suggestions to keep it that way.
66
- Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
67
- less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
68
- """.format(total_footprint)
69
- response1 = model.generate_content(prompt1)
70
- return [total_footprint, response1.text]
71
-
72
- elif total_footprint > 6000 and total_footprint < 15999:
73
- prompt2 = """
74
- commend for keeping total carbon footprint down at {} and recommend practical suggestions to bring it down further.
75
- Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
76
- less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
77
- """.format(total_footprint)
78
- response2 = model.generate_content(prompt2)
79
- return [total_footprint, response2.text]
80
-
81
- elif total_footprint > 16000 and total_footprint < 22000:
82
- prompt3 = """
83
- commend for keeping total carbon footprint at an average of {} and recommend useful suggestions to make sure it doesn't get higher than that.
84
- Give suggestions regarding monthly electricity bill, monthly gas bill, monthly oil bill, total yearly mileage on car, number of flights
85
- less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
86
- """.format(total_footprint)
87
- response3 = model.generate_content(prompt3)
88
- return [total_footprint, response3.text]
89
-
90
- elif total_footprint > 22000:
91
- prompt4 = """
92
- 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
93
- less than or equal to 4 hours, and number of flights more than or equal to 4 hours.
94
- """.format(total_footprint)
95
- response4 = model.generate_content(prompt4)
96
- return [total_footprint, response4.text]
97
-
98
- css = """
99
- .app {
100
- border-width: 3px;
101
- border-color: forestgreen;
102
- }
103
- """
104
- # Gradio Interface
105
- app = gr.Interface(
106
- fn=carbonFootPrintAI,
107
- inputs=[
108
- gr.Number(label="Monthly Gas Bill ($)", elem_classes="app"),
109
- gr.Number(label="Monthly Oil Bill ($)", elem_classes="app"),
110
- gr.Number(label="Monthly Electricity Bill ($)", elem_classes="app"),
111
- gr.Slider(label="Total Yearly Mileage on Car", value = 0, minimum = 0, maximum = 50000, step = 1, elem_classes="app"),
112
- gr.Slider(label="Number of Flights Less Than or Equal to 4 Hours", value = 0, minimum = 0, maximum = 100, step = 1, elem_classes="app"),
113
- gr.Slider(label="Number of Flights More Than or Equal to 4 Hours", value = 0, minimum = 0, maximum = 100, step = 1, elem_classes="app"),
114
- gr.Checkbox(label="Do You Recycle Newspaper?", elem_classes="app"),
115
- gr.Checkbox(label="Do You Recycle Aluminium and Tin?", elem_classes="app")
116
- ],
117
-
118
- outputs=[
119
- gr.Number(label="Total Carbon Footprint", elem_classes="app"),
120
- gr.Markdown("<center>Recommendation To Reduce Your Carbon Footprint</center>")
121
- ],
122
- theme = "upsatwal/mlsc_tiet",
123
- title = "&#127807; &#127757; Carbon Footprint Calculator &#127757; &#127807;"
124
  )
125
 
126
- app.launch(mcp_server = True)
 
 
1
  import gradio as gr
 
2
 
3
+ def letter_counter(word, letter):
4
+ """Count the occurrences of a specific letter in a word.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ Args:
7
+ word: The word or phrase to analyze
8
+ letter: The letter to count occurrences of
9
+
10
  Returns:
11
+ The number of times the letter appears in the word
 
12
  """
13
+ return word.lower().count(letter.lower())
14
+
15
+ demo = gr.Interface(
16
+ fn=letter_counter,
17
+ inputs=["text", "text"],
18
+ outputs="number",
19
+ title="Letter Counter",
20
+ description="Count how many times a letter appears in a word"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  )
22
 
23
+ demo.launch(mcp_server=True)