chat-system / app.py
Sayed Mubarish
Remove unwanted imports
56add8c
import public_ip as ip
import requests
from datetime import datetime
import os
from dotenv import load_dotenv
load_dotenv()
def get_ip_address()->str:
print("Printing ip address")
"""
Retrives the public ip address of user
Args:
No arguements
Returns:
returns a string containing public ip address
"""
return ip.get()
def get_user_location(ip : str) -> dict:
print("Printing user location")
"""
Retrives the country,city,latitude and longitude from public ip address of user
Args:
ip : public ip address of user
Returns:
returns a dictionary containing country,city,latitude and longitude of current location
"""
response = requests.get(f'http://ip-api.com/json/{ip}')
location_data = response.json()
location = {
"location_country" : location_data['country'],
"location_city" : location_data['city'],
"location_longitude" : location_data['lon'],
"location_latitude" : location_data['lat']
}
return location
def get_current_time()->str:
print("Printing current time")
"""
Retrives the current date and time
Args:
No arguements
Returns:
returns a string containing current date and time
"""
current_time = str(datetime.now())
return current_time
def get_current_weather(location:str = None)->dict:
print("Printing current weather")
"""
Retrives the weather of the location
Args:
location : Name of location eg.Berlin
Returns:
returns a dictionary containing weather details
"""
if location is None:
location = get_user_location(get_ip_address())['location_city']
url = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+os.getenv("OPEN_WEATHER_API")
response = requests.get(url)
result = response.json()
return result
def get_news_headlines()->dict:
print("Printing news")
"""
Retrives the latest news from bbc
Args:
no arguements
Returns:
returns a dictionary containing news details
"""
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
URL = "https://newsdata.io/api/1/latest?apikey="+NEWS_API_KEY+"&domain=bbc"
response = requests.get(URL)
return response.json()
def get_air_quality(longitude:str = None,latitude:str = None)->dict:
print("Printing air quality")
"""
Retrives the current air pollution data from coordinates
The range of air quality is 1 to 5, where is 1 good and 5 is very poor and rest of numbers appropriately
Args:
longitude : Longitude of location
latitude : Latitude of location
Returns:
returns a dictionary containing air quality details
"""
if longitude is None:
longitude:str = get_user_location(get_ip_address())['location_longitude']
if latitude is None:
latitude:str = get_user_location(get_ip_address())['location_latitude']
longitude = str(longitude)
latitude = str(latitude)
OPEN_WEATHER_API = os.getenv("OPEN_WEATHER_API")
URL = "http://api.openweathermap.org/data/2.5/air_pollution?lat="+latitude+"&lon="+longitude+"&appid="+OPEN_WEATHER_API
response = requests.get(URL)
return response.json()
def get_coordinates(location:str = None) -> dict:
print("Printing coordinates")
"""
Retrives the coordinates of the location
Args:
location : Name of location eg.Berlin
Returns:
returns a dictionary containing coordinate details
"""
if location is None:
location = get_user_location(get_ip_address())['location_city']
OPEN_WEATHER_API = os.getenv("OPEN_WEATHER_API")
URL = "https://api.openweathermap.org/geo/1.0/direct?q="+location+"&appid="+OPEN_WEATHER_API
response = requests.get(URL)
return response.json()
def get_sunrise_sunset(location:str = None) -> dict:
print("Printing sun")
"""
Retrives the sunset,sunrise of the location
Args:
location : Name of location eg.Berlin
Returns:
returns a dictionary containing sunset,sunrise details
"""
if location is None:
location = get_user_location(get_ip_address())['location_city']
url = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+os.getenv("OPEN_WEATHER_API")
response = requests.get(url)
result = response.json()
final_output = {
"sunrise" : datetime.fromtimestamp(result['sys']['sunrise']),
"sunset" : datetime.fromtimestamp(result['sys']['sunset'])
}
return final_output
from google import genai
from google.genai import types
client = genai.Client(api_key=os.getenv("GOOGLE_GEMINI_API_KEY"))
config = types.GenerateContentConfig(
tools=[
get_ip_address,
get_user_location,
get_current_time,
get_current_weather,
get_news_headlines,
get_air_quality,
get_coordinates,
get_sunrise_sunset
]
)
# response = client.models.generate_content(
# model="gemini-2.5-flash",
# contents="What is the coordinates here, my location?",
# config=config,
# )
# chat = client.chats.create(model="gemini-2.5-flash", config=config)
# response = chat.send_message("What is the weather")
chat = client.chats.create(model="gemini-2.5-flash", config=config)
def chat_bot(text):
response = chat.send_message(text)
return response.text
import gradio as gr
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, history):
return "", [[user_message, None]]
def bot(history):
user_message = history[0][0]
# print(history)
bot_message = chat_bot(user_message)
history[-1][1] = bot_message
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch()