akashVP commited on
Commit
4bce1ba
·
1 Parent(s): 202b970

feat: add app.py abd .gitignore

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +210 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # general
2
+ import public_ip as ip
3
+ import requests
4
+ from datetime import datetime
5
+
6
+ # for setting env
7
+ import os
8
+ from dotenv import load_dotenv
9
+
10
+ load_dotenv()
11
+
12
+ # for gemini
13
+ from google import genai
14
+ from google.genai import types
15
+
16
+ # for ui
17
+ import gradio as gr
18
+
19
+
20
+ def get_ip_address():
21
+ """Find public IP address of user
22
+
23
+ Args:
24
+ No arguments
25
+
26
+ Returns:
27
+ A string contains public IP address of user
28
+ """
29
+ return ip.get()
30
+
31
+
32
+ def get_current_location() -> tuple:
33
+ """Find user current location as from public IP address
34
+
35
+ Args:
36
+ No arguments
37
+
38
+ Returns:
39
+ A dictionary contains users country,city,longitude,latitude
40
+ """
41
+ ip_address = get_ip_address()
42
+ response = requests.get("http://ip-api.com/json/" + ip_address).json()
43
+ return {
44
+ "country": response.get("country"),
45
+ "city": response.get("city"),
46
+ "zip": response.get("zip"),
47
+ "lat": response.get("lat"),
48
+ "lon": response.get("lon"),
49
+ }
50
+
51
+
52
+ def get_current_weather(location: str = get_current_location().get("city")) -> dict:
53
+ """Find current weather status from given location,
54
+ If no location given: find the currunt location from IP and give weather.
55
+
56
+ Args:
57
+ location: If location is avaliable, else it find from IP
58
+
59
+ Returns:
60
+ A dictionary contains current weather condeiton
61
+
62
+ """
63
+ response = requests.get(
64
+ f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={os.getenv('WEATHER_API')}"
65
+ ).json()
66
+
67
+ return {
68
+ "weather": response.get("weather"),
69
+ "main": response.get("main"),
70
+ "wind": response.get("wind"),
71
+ "location": response.get("name"),
72
+ "sunrise": response.get("sys").get("sunrise"),
73
+ "sunset": response.get("sys").get("sunset"),
74
+ }
75
+
76
+ # return response
77
+
78
+
79
+ def get_time_and_date() -> datetime:
80
+ """Find current date and time
81
+ Args:
82
+ No arguments
83
+
84
+ Returns:
85
+ Current date and time in datetime.datetime format
86
+
87
+ """
88
+ return datetime.now()
89
+
90
+
91
+ def get_sunrise_sunset(location: str = get_current_location().get("city")) -> dict:
92
+ """Find sunrise and sunset timings
93
+
94
+ Args:
95
+ location: location name in string format, if not given by default find current location of user
96
+
97
+ Returns:
98
+ sunrise and sunset timeings in datetime format as dictionary
99
+
100
+ """
101
+ weather = get_current_weather(location)
102
+
103
+ return {
104
+ "sunrise": datetime.fromtimestamp(weather.get("sunrise")),
105
+ "sunset": datetime.fromtimestamp(weather.get("sunset")),
106
+ }
107
+
108
+
109
+ def get_news_headlines(topic: str = "google") -> dict:
110
+ """Find news article, if topic provided find related news
111
+
112
+ Args:
113
+ topic: A string contains topic to get related news, default set to empty
114
+
115
+ Returns:
116
+ a dictionary of news titles and discription
117
+
118
+ """
119
+
120
+ response = requests.get(
121
+ f"https://gnews.io/api/v4/search?q={topic}&lang=en&max=5&apikey={os.getenv('GNEWS_API')}"
122
+ ).json()
123
+
124
+ # taking only news title and description, adding to new dict
125
+ news_articles = {}
126
+ index = 1
127
+ for news in response.get("articles"):
128
+ news_articles[index] = {
129
+ "title": news.get("title"),
130
+ "description": news.get("description"),
131
+ }
132
+ index += 1
133
+
134
+ return news_articles
135
+
136
+
137
+ def get_coordinates(location: str) -> dict:
138
+
139
+ response = requests.get(
140
+ f"http://api.openweathermap.org/geo/1.0/direct?q={location}&limit={1}&appid={os.getenv('WEATHER_API')}"
141
+ ).json()
142
+
143
+ return {"latitude": response[0].get("lat"), "longitude": response[0].get("lon")}
144
+
145
+ # return response
146
+
147
+
148
+ def get_air_quality(latitude: float, longitude: float) -> dict:
149
+ """Find air quality in a location from coordinates
150
+
151
+ Args:
152
+ latitude: lattitude value of loaction
153
+ longitude: longitude valur of location
154
+
155
+ Returns:
156
+ A dictionary contains air quality details
157
+
158
+ """
159
+
160
+ response = requests.get(
161
+ f"http://api.openweathermap.org/data/2.5/air_pollution?lat={latitude}&lon={longitude}&appid={os.getenv('WEATHER_API')}"
162
+ ).json()
163
+
164
+ return response
165
+
166
+
167
+ client = genai.Client(api_key=os.getenv("GEMINI_API"))
168
+ config = types.GenerateContentConfig(
169
+ tools=[
170
+ get_current_location,
171
+ get_current_weather,
172
+ get_time_and_date,
173
+ get_sunrise_sunset,
174
+ get_news_headlines,
175
+ get_coordinates,
176
+ get_air_quality,
177
+ ]
178
+ )
179
+
180
+ chat = client.chats.create(model="gemini-2.5-flash", config=config)
181
+
182
+ def chat_bot(text):
183
+ response = chat.send_message(text)
184
+
185
+ return response.text
186
+
187
+
188
+ # import time
189
+
190
+ with gr.Blocks() as demo:
191
+ chatbot = gr.Chatbot()
192
+ msg = gr.Textbox()
193
+ clear = gr.Button("Clear")
194
+
195
+ def user(user_message, history):
196
+ return "", [[user_message, None]]
197
+
198
+ def bot(history):
199
+ user_message = history[0][0]
200
+ # print(history)
201
+ bot_message = chat_bot(user_message)
202
+ history[-1][1] = bot_message
203
+ return history
204
+
205
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
206
+ bot, chatbot, chatbot
207
+ )
208
+ clear.click(lambda: None, None, chatbot, queue=False)
209
+
210
+ demo.launch()