0% found this document useful (0 votes)
4 views3 pages

AI Weather Assistant in Python

The document outlines a Python-based AI weather assistant that predicts activities based on current weather conditions. It includes a sample training dataset with temperature, humidity, wind speed, and corresponding activity suggestions. The assistant fetches real-time weather data using an API and provides tailored activity recommendations based on the fetched conditions.

Uploaded by

223317
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

AI Weather Assistant in Python

The document outlines a Python-based AI weather assistant that predicts activities based on current weather conditions. It includes a sample training dataset with temperature, humidity, wind speed, and corresponding activity suggestions. The assistant fetches real-time weather data using an API and provides tailored activity recommendations based on the fetched conditions.

Uploaded by

223317
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# PURE PYTHON AI-BASED WEATHER ASSISTANT

import requests

# --- Sample Training Dataset ---

# [temperature, humidity, wind speed, condition, suggestion]

training_data = [

[90, 75, 10, "sunny", "Stay indoors and drink water"],

[70, 50, 5, "clear", "Go for a walk or picnic"],

[60, 90, 15, "rain", "Carry an umbrella"],

[85, 60, 5, "sunny", "Enjoy outdoor games"],

[55, 80, 20, "storm", "Avoid going outside"],

[45, 60, 30, "windy", "Stay safe from strong winds"],

[80, 85, 10, "cloudy", "Good for a jog"],

[72, 65, 12, "sunny", "Ideal for light exercise"],

# --- Simple AI Predictor Function ---

def predict_activity(temp, humidity, wind, condition):

best_match = None

smallest_diff = float('inf')

for row in training_data:

t, h, w, cond, label = row

# Check if condition keyword matches

if cond in [Link]():

diff = abs(temp - t) + abs(humidity - h) + abs(wind - w)

if diff < smallest_diff:

smallest_diff = diff

best_match = label
return best_match or "No suggestion found. Try again later."

# --- Weather Fetching Function ---

def get_weather(city, api_key):

url = f"[Link]
timeline/{city}?unitGroup=us&key={api_key}&contentType=json"

response = [Link](url)

if response.status_code == 200:

data = [Link]()

current = data['currentConditions']

temp = current['temp']

humidity = current['humidity']

wind = current['windspeed']

condition = current['conditions']

print(f"\nWeather for: {data['resolvedAddress']}")

print(f"Temperature: {temp} °F")

print(f"Condition: {condition}")

print(f"Humidity: {humidity}%")

print(f"Wind Speed: {wind} mph")

# AI Suggestion

suggestion = predict_activity(temp, humidity, wind, condition)

print(f"\nAI Suggestion: {suggestion}")

else:

print("Failed to retrieve weather data")

# --- Run the Assistant ---


city = input("Enter city name: ")

api_key = "XGFFA33ZH2MEWVASUQP8JMJRK"

get_weather(city, api_key)

Common questions

Powered by AI

The 'predict_activity' function balances precision by ensuring that the temperature, humidity, and wind speed differences are minimized between user inputs and dataset entries. It maintains flexibility by only requiring a keyword match for the weather condition, allowing a breadth of possible condition names. This dual approach ensures that suggestions are not too rigidly tied to exact matches, while still striving for the closest fit in key weather parameters .

The program extracts and prints key weather details—such as temperature, condition, humidity, and wind speed—from the API response. It enhances user interaction by providing detailed, current weather information that is relevant and directly connected to activity suggestions. This connection between real-time data and activity recommendations allows users to make informed decisions based on the latest weather conditions .

The scalability of the AI-based weather assistant faces challenges such as increased processing time as more cities and conditions require more data to be retrieved and compared against. This may lead to longer wait times for activity predictions. Additionally, maintaining an extensive and updated training dataset that accommodates more varied conditions would demand more storage and efficient data management techniques. The system might also require enhancements in algorithm efficiency to handle the wider input scope without degrading performance .

Relying solely on the smallest difference approach can lead to issues if multiple entries in the training dataset have similar differences, potentially leading to less relevant suggestions. Moreover, this approach does not account for nonlinear relationships between weather components and user preference variations, limiting the system's adaptability in unique or unexpected scenarios .

The AI-based weather assistant uses a training dataset that includes historical weather data and corresponding suggestions for activities. For prediction, it compares user input conditions like temperature, humidity, wind speed, and specific weather conditions against this dataset. It calculates the 'difference' by summing the absolute differences between the input and each dataset entry for these parameters. The entry with the smallest difference and a matched weather condition keyword is selected to provide the best activity suggestion .

The AI assistant retrieves weather data for a given city by making an HTTP GET request to a weather service API. It constructs a URL using the city name and an API key, specifies the unit group, and sets the content type to JSON. The response from the API contains data including current temperature, humidity, wind speed, and weather condition, which the assistant then processes and displays to the user .

The API key is used for authenticating requests made to the weather service. It ensures that access to the API is authorized and that the server can track usage associated with the key, which is necessary for billing or usage tracking .

The AI assistant might output 'No suggestion found. Try again later.' if none of the weather conditions in the training dataset exactly match the input condition keyword from the user. This indicates that the assistant could not find a suitable suggestion because either the keyword does not exist in the dataset or other parameters did not align closely with the dataset entries .

To handle a wider range of weather conditions, improvements could include expanding the training dataset with more diverse and comprehensive weather scenarios and keywords. Implementing machine learning algorithms that can predict user-friendly activities even for conditions not explicitly covered in the dataset would enhance functionality. Additionally, fuzzy matching techniques could be used to better handle condition variations and synonyms .

The AI assistant uses a method of matching the weather condition keyword and calculating the numerical differences in temperature, humidity, and wind speed between the user input and each entry in the training dataset. It selects the suggestion from the dataset entry that results in the smallest total difference, provided the condition keyword matches .

You might also like