Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| import json | |
| import requests | |
| import streamlit as st | |
| #from dotenv import load_dotenv | |
| # Set API keys | |
| openai.api_key = os.environ['OPENAI_API_KEY'] | |
| OPENWEATHER_API_KEY = os.environ['OPENWEATHER_API_KEY'] | |
| # Function to get the current weather | |
| def get_current_weather(location): | |
| """Get the current weather in a given location""" | |
| base_url = "http://api.openweathermap.org/data/2.5/weather?" | |
| complete_url = f"{base_url}appid={OPENWEATHER_API_KEY}&q={location}" | |
| response = requests.get(complete_url) | |
| if response.status_code == 200: | |
| data = response.json() | |
| weather = data['weather'][0]['main'] | |
| temperature = data['main']['temp'] - 273.15 # Convert Kelvin to Celsius | |
| return { | |
| "city": location, | |
| "weather": weather, | |
| "temperature": round(temperature, 2) | |
| } | |
| else: | |
| return {"city": location, "weather": "Data Fetch Error", "temperature": "N/A"} | |
| # Streamlit app | |
| st.title("Weather Info Assistant") | |
| #st.write("Get the current weather information for a specific location. Just replace Gurgaon with the location") | |
| user_input = st.text_input("Get the current weather information for a specific location. Just replace Gurgaon with the location", "What is the weather like in Gurgaon?") | |
| if st.button("Get Weather"): | |
| # Use GPT-3.5-turbo to parse the user query | |
| response = openai.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": user_input | |
| } | |
| ], | |
| temperature=0, | |
| max_tokens=300, | |
| functions=[ | |
| { | |
| "name": "get_current_weather", | |
| "description": "Get the current weather in a given location", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "location": { | |
| "type": "string", | |
| "description": "The city and state, e.g. San Francisco, CA", | |
| }, | |
| }, | |
| "required": ["location"], | |
| }, | |
| } | |
| ], | |
| function_call="auto" | |
| ) | |
| # Extract the location from the response | |
| try: | |
| # Extract the function call information | |
| function_call_info = response.choices[0].message.function_call | |
| function_name = function_call_info.name # This should be 'get_current_weather' | |
| function_arguments = function_call_info.arguments # This is a JSON string | |
| # Parse the JSON string to get the arguments dictionary | |
| args = json.loads(function_arguments) | |
| location = args['location'] | |
| # Now you can fetch the weather information using the extracted location | |
| weather_info = get_current_weather(location) | |
| # Define a dictionary to map weather conditions to emoticons | |
| weather_icons = { | |
| "Clear": "βοΈ", # Sun icon | |
| "Clouds": "βοΈ", # Cloud icon | |
| "Rain": "π§οΈ", # Rain icon | |
| "Snow": "βοΈ", # Snow icon | |
| "Thunderstorm": "βοΈ", # Thunderstorm icon | |
| "Mist": "π«οΈ", # Fog icon | |
| "Smoke": "π¨", # Smoke icon | |
| "Haze": "π«οΈ", # Haze icon | |
| "Dust": "πͺοΈ", # Dust icon | |
| "Fog": "π«οΈ", # Fog icon | |
| "Sand": "ποΈ", # Sand icon | |
| "Ash": "π", # Volcanic ash icon | |
| "Squall": "π¬οΈ", # Wind face icon | |
| "Tornado": "πͺοΈ" # Tornado icon | |
| } | |
| if weather_info["weather"] != "Data Fetch Error": | |
| icon = weather_icons.get(weather_info["weather"], "β") # Default to question mark if no match | |
| weather_description = f"The weather in **{weather_info['city']}** is currently `{weather_info['weather']}` with a temperature of `{weather_info['temperature']}Β°C`." | |
| chat_response = openai.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": "Generate a conversational response for weather information. Please include the weather and temperature details in the response and provide suggestion on things that can be done best in this weather" | |
| }, | |
| { | |
| "role": "user", | |
| "content": weather_description | |
| } | |
| ], | |
| temperature=0.5, | |
| max_tokens=300 | |
| ) | |
| # Use HTML to style the font size, color, and include the icon in the displayed message | |
| st.markdown(f"<div style='font-size: 24px; color: blue; font-weight: bold;'> Weather: {weather_info['weather']} {icon}          Temperature: {weather_info['temperature']}Β°C</div>", unsafe_allow_html=True) | |
| st.markdown(f"<div style='font-size: 20px;'> {chat_response.choices[0].message.content}", unsafe_allow_html=True) | |
| else: | |
| st.markdown(f"<div style='font-size: 20px;'>**Could not fetch weather data for {weather_info['city']}. Please try again.**</div>", unsafe_allow_html=True) | |
| except Exception as e: | |
| print(f"Error processing the response or fetching the weather: {e}") | |