My First AI App: Weather and Outfit Suggester
Inspiration and Journey
My journey into AI development began after being inspired by Dr. Andrew Ng. Taking his course on Python for AI helped me realize that AI isn’t magic—it’s logic and coding. This realization motivated me to create my first AI app, a Weather and Outfit Suggester.
The Concept
The app is designed to perform two main functions:
- Determine the weather for today using an API call to a weather service.
- Generate a suggestion for what to wear based on the weather.
Development Process
Initial Steps
I started by creating a Jupyter notebook and began with a simple “Hello World” program. From there, I moved on to fetching the weather forecast. Using Dr. Ng’s code as a foundation, I made an API call to OpenAI to generate outfit suggestions.
Rapid Development with Claude AI
One evening, I used Claude AI to generate the entire app, which took about 15 minutes. Below is a summary of the development process.
Weather App Development Summary
Initially, I aimed to create a Python app that would take a zip code as input, fetch the weather forecast for the day, and then use OpenAI’s ChatGPT to suggest what to wear based on the weather. We started with a basic command-line script that outlined the structure for these operations, including placeholder functions for API calls to a weather service and OpenAI.
Challenges and Solutions
- ModuleNotFoundError: We resolved a
ModuleNotFoundError
related to thepython-dotenv
library by providing installation instructions and offering a simplified version of the script that didn’t rely on environment variables. - User Interface: You requested a simple interface for the app, which led us to implement a graphical user interface (GUI) using tkinter. This transformation made the app more user-friendly, allowing for input fields and displaying results in a window rather than the command line.
- API Key Management: We refined the app to use API keys stored in a .env file instead of asking the user to input them each time, enhancing both security and user experience.
- Personalization: Finally, we added a gender selection feature to the GUI, allowing for more personalized clothing recommendations. This involved modifying both the interface and the API call to OpenAI to include gender information.
Final Product
The final product is a weather app that takes a user’s zip code and gender, fetches the weather forecast, and provides tailored outfit suggestions based on the weather and the user’s gender.
Conclusion
Creating this app was a fantastic learning experience. It started as a simple script and evolved into a sophisticated application with a GUI and personalized recommendations. Below is the code for the final product.
import tkinter as tk
from tkinter import messagebox, ttk
import requests
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def get_weather_forecast(zip_code):
api_key = os.getenv("WEATHER_API_KEY")
url = f"https://api.weatherapi.com/v1/forecast.json?key={api_key}&q={zip_code}&days=1"
response = requests.get(url)
data = response.json()
return data['forecast']['forecastday'][0]['day']
def get_outfit_suggestion(weather, gender):
api_key = os.getenv("OPENAI_API_KEY")
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant that suggests outfits based on weather and gender."},
{"role": "user", "content": f"The weather today is {weather}. What should a {gender} wear?"}
]
}
response = requests.post(url, headers=headers, json=data)
return response.json()['choices'][0]['message']['content']
def get_weather_and_outfit():
zip_code = zip_entry.get()
gender = gender_var.get()
if not zip_code:
messagebox.showerror("Error", "Please enter a zip code.")
return
if not gender:
messagebox.showerror("Error", "Please select a gender.")
return
try:
weather = get_weather_forecast(zip_code)
weather_description = f"temperature between {weather['mintemp_c']}°C and {weather['maxtemp_c']}°C, {weather['condition']['text']}"
weather_result.config(text=f"Today's weather: {weather['condition']['text']}\nHigh: {weather['maxtemp_c']}°C, Low: {weather['mintemp_c']}°C")
outfit_suggestion = get_outfit_suggestion(weather_description, gender)
outfit_result.config(text=f"Outfit suggestion: {outfit_suggestion}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
# Create the main window
root = tk.Tk()
root.title("Weather and Outfit Suggester")
root.geometry("400x450")
# Create and place widgets
tk.Label(root, text="Enter Zip Code:").pack(pady=5)
zip_entry = tk.Entry(root)
zip_entry.pack(pady=5)
# Gender selection
tk.Label(root, text="Select Gender:").pack(pady=5)
gender_var = tk.StringVar()
gender_frame = ttk.Frame(root)
gender_frame.pack(pady=5)
ttk.Radiobutton(gender_frame, text="Male", variable=gender_var, value="male").pack(side=tk.LEFT, padx=5)
ttk.Radiobutton(gender_frame, text="Female", variable=gender_var, value="female").pack(side=tk.LEFT, padx=5)
ttk.Radiobutton(gender_frame, text="Non-binary", variable=gender_var, value="non-binary").pack(side=tk.LEFT, padx=5)
submit_button = tk.Button(root, text="Get Weather and Outfit", command=get_weather_and_outfit)
submit_button.pack(pady=20)
weather_result = tk.Label(root, text="", wraplength=350)
weather_result.pack(pady=10)
outfit_result = tk.Label(root, text="", wraplength=350)
outfit_result.pack(pady=10)
# Start the GUI event loop
root.mainloop()
I hope this blog post provides a clear and engaging overview of my first AI app development experience!