A Streamlit-based weather forecast application that provides detailed weather information using the National Weather Service (NWS) API, enhanced with AI-powered summaries and interactive Q&A capabilities powered by Google Gemini.
This application allows users to:
- Enter a US ZIP code to find the nearest town and coordinates
- View detailed 7-day weather forecasts from the National Weather Service
- Get AI-generated natural language summaries of weather forecasts
- Ask questions about the weather forecast in an interactive chat interface
- Enter any 5-digit US ZIP code
- Automatic geocoding using multiple fallback services (Zippopotam.us and Nominatim)
- Displays town name and coordinates
- Validates ZIP code format and location
- 7-day weather forecast from the National Weather Service
- Temperature, wind speed and direction, precipitation probability
- Dewpoint and relative humidity information
- Detailed forecast descriptions
- AI-generated natural language summaries (requires Google API key)
- Ask questions about the weather forecast
- AI-powered answers using Google Gemini 2.5 Flash-Lite
- Conversation history maintained across questions
- Context-aware responses based on the current forecast
NWS LangChain app/
├── app.py # Main splash page and session state initialization
├── utils.py # Core utility functions and API interactions
├── requirements.txt # Python dependencies
├── pages/
│ ├── 1_📍_Enter_ZIP_Code.py # ZIP code entry and geocoding page
│ ├── 2_🌤️_Forecast.py # Weather forecast display page
│ └── 3_💬_Chat.py # Interactive Q&A chat page
└── venv/ # Virtual environment (not in git)
- Main entry point for the Streamlit application
- Initializes session state variables:
gemini_model: Google Gemini model instancecurrent_town_name: Currently selected town/citycurrent_coords: Latitude and longitude coordinatescurrent_forecast_data: Cached forecast data from NWS APIforecast_summary: AI-generated forecast summaryconversation_history: Chat conversation historyconversation_id: Unique identifier for chat sessions
- Displays welcome page with feature overview
Core utility functions:
Geocoding:
geocode_zip_code(): Converts ZIP codes to coordinates using multiple fallback services
NWS API Integration:
get_grid_point(): Retrieves NWS grid point information for coordinatesget_forecast(): Fetches forecast data from NWS APIdisplay_forecast(): Formats and displays forecast data in Streamlit UI
AI Integration:
init_gemini_model(): Initializes Google Gemini model via LangChainformat_forecast_data(): Formats forecast data for LLM processinggenerate_forecast_summary(): Creates natural language forecast summariesanswer_weather_question(): Answers user questions with forecast context
Chat History:
InMemoryChatMessageHistory: In-memory implementation of chat message historyget_or_create_chat_history(): Manages conversation history per session
- ZIP code input and validation
- Geocoding using
geocode_zip_code()utility - Displays location information and coordinates
- Stores location data in session state
- Displays weather forecast for selected location
- Fetches forecast data from NWS API if not cached
- Shows detailed forecast periods with expandable sections
- Generates AI summary if Gemini model is available
- Resets conversation ID when new forecast is loaded
- Interactive Q&A interface for weather questions
- Uses conversation history for context-aware responses
- Displays conversation history with expandable previous questions
- Validates that forecast data and Gemini model are available
streamlit>=1.28.0: Web application frameworkrequests>=2.31.0: HTTP library for API callslangchain>=0.1.0: LLM framework for AI integrationlangchain-google-genai>=1.0.0: Google Gemini integration for LangChainpython-dotenv>=1.0.0: Environment variable management
-
National Weather Service API: Free, public weather data API
- Base URL:
https://api.weather.gov - Requires User-Agent header
- No API key required
- Base URL:
-
Google Gemini API: AI model for summaries and Q&A
- Model:
gemini-2.5-flash-lite(free tier) - Requires API key in
.envfile - Alternative:
gemini-2.5-profor more capable responses
- Model:
-
Geocoding Services (fallback chain):
- Zippopotam.us: ZIP code-specific API
- Nominatim (OpenStreetMap): General geocoding service
cd "NWS LangChain app"python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the project root:
GOOGLE_API_KEY=your_google_api_key_hereNote: The application will work without the Google API key, but AI features (forecast summaries and Q&A) will be disabled.
streamlit run app.pyThe application will open in your default web browser at http://localhost:8501.
- Navigate to the "📍 Enter ZIP Code" page
- Enter a valid 5-digit US ZIP code (e.g.,
10001) - The app will find the location and display town name and coordinates
- Go to the "🌤️ Forecast" page
- The app automatically fetches the 7-day forecast from NWS
- View detailed forecast information in expandable sections
- If Gemini is configured, view the AI-generated summary
- Visit the "💬 Chat" page
- Ask questions about the weather forecast
- View conversation history and ask follow-up questions
The application uses Streamlit's session state to maintain:
- Location information across pages
- Cached forecast data to avoid redundant API calls
- Conversation history for context-aware Q&A
- Unique conversation IDs for session management
- Comprehensive error handling for API failures
- Graceful fallbacks for geocoding services
- User-friendly error messages
- Exception traceback logging with line numbers
- NWS API: No official rate limits, but be respectful
- Nominatim: Requires User-Agent header, rate-limited
- Google Gemini: Subject to API quota limits
The app uses a multi-service fallback approach:
- Primary: Zippopotam.us (ZIP-specific, fast)
- Fallback: Nominatim with multiple query formats
- Validation: Coordinate bounds checking for US locations
- Model:
gemini-2.5-flash-lite(free tier, lightweight and fast responses) - Temperature: 0.7 (balanced creativity/accuracy)
- Context: Full forecast data included in system message
- History: Maintained per conversation ID
The codebase follows:
- PEP 8 style guidelines
- Type hints for function parameters and return values
- Docstrings for all functions and classes
- f-strings for string formatting
- Keyword arguments where appropriate
- Meaningful variable names
- Error handling with line number reporting
- US ZIP Codes Only: The application currently supports only US ZIP codes
- NWS Coverage: Weather data is only available for locations within NWS coverage areas
- API Dependencies: Requires internet connection and API availability
- Gemini API Key: AI features require a valid Google API key
Potential improvements:
- Support for international locations
- Historical weather data
- Weather alerts and warnings
- Multiple location comparison
- Export forecast data
- Customizable forecast periods
- Weather maps integration
This project uses:
- NWS API: Public domain data from NOAA
- Streamlit: Apache License 2.0
- LangChain: MIT License
- Google Gemini: Subject to Google's Terms of Service
When contributing:
- Follow PEP 8 style guidelines
- Add type hints to all functions
- Include docstrings
- Handle errors gracefully with line number reporting
- Test with various ZIP codes and edge cases
For issues or questions:
- Check that your ZIP code is valid and in the US
- Verify your Google API key is correctly configured
- Ensure you have an active internet connection
- Check that the NWS API is accessible
- National Weather Service for providing free, public weather data
- Google for Gemini AI capabilities
- Streamlit for the web framework
- LangChain for LLM integration tools