Skip to content

A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.

audit: fix multimodal_weather_app - add env comment, rewrite em dashes, add module docstring, replace prints with logging, add docstrings

Tiioluwanib2b10daafdd5
diff --git a/multimodal/multimodal_weather_app/.env.example b/multimodal/multimodal_weather_app/.env.example
index b263636..9c47f26 100644
--- a/multimodal/multimodal_weather_app/.env.example
+++ b/multimodal/multimodal_weather_app/.env.example
@@ -1 +1,3 @@
+# Mistral AI API key (required)
+# Get your free key at https://platform.mistral.ai
MISTRAL_API_KEY=your_mistral_api_key_here
diff --git a/multimodal/multimodal_weather_app/README.md b/multimodal/multimodal_weather_app/README.md
index ee9c45d..ac1e239 100644
--- a/multimodal/multimodal_weather_app/README.md
+++ b/multimodal/multimodal_weather_app/README.md
@@ -1,6 +1,6 @@
# Multimodal Weather App
-> Upload a map image and get live weather — Mistral Small identifies the city via vision, then fetches real-time conditions through native tool calling.
+> Upload a map image and get live weather: Mistral Small identifies the city via vision, then fetches real-time conditions through native tool calling.
## Overview
@@ -12,11 +12,11 @@ This app combines two AI capabilities in a single pipeline: **vision** and **too
## Features
-- **Vision-powered city detection** — Mistral Small 4 reads map images and extracts the city name
-- **Native tool calling** — Mistral Small 4 decides when and how to invoke the weather tool
-- **Live weather data** — temperature, conditions, humidity, and wind speed fetched in real time
-- **No weather API key needed** — uses `python_weather`, a free weather client
-- **Simple one-click UI** — built with Gradio Blocks
+- **Vision-powered city detection:** Mistral Small 4 reads map images and extracts the city name
+- **Native tool calling:** Mistral Small 4 decides when and how to invoke the weather tool
+- **Live weather data:** temperature, conditions, humidity, and wind speed fetched in real time
+- **No weather API key needed:** uses `python_weather`, a free weather client
+- **Simple one-click UI:** built with Gradio Blocks
## Tech Stack
@@ -30,7 +30,7 @@ This app combines two AI capabilities in a single pipeline: **vision** and **too
## Prerequisites
- Python 3.10 or higher
-- A Mistral AI API key — get one at [platform.mistral.ai](https://platform.mistral.ai)
+- A Mistral AI API key; get one at [platform.mistral.ai](https://platform.mistral.ai)
## Installation
@@ -93,7 +93,7 @@ MISTRAL_API_KEY=your_mistral_api_key_here
| Variable | Required | Description |
|---|---|---|
-| `MISTRAL_API_KEY` | Yes | Your Mistral AI API key — get one at [platform.mistral.ai](https://platform.mistral.ai) |
+| `MISTRAL_API_KEY` | Yes | Your Mistral AI API key; get one at [platform.mistral.ai](https://platform.mistral.ai) |
## Project Structure
@@ -104,3 +104,7 @@ multimodal-weather-app/
├── .env.example # Environment variable template
└── .env # Your local secrets (not committed)
```
+
+---
+
+[Back to Top](#multimodal-weather-app)
diff --git a/multimodal/multimodal_weather_app/app.py b/multimodal/multimodal_weather_app/app.py
index c261230..3a7ff5c 100644
--- a/multimodal/multimodal_weather_app/app.py
+++ b/multimodal/multimodal_weather_app/app.py
@@ -1,6 +1,9 @@
+"""Multimodal weather app that uses Mistral Small vision to identify a city from a map image, then fetches live weather via native tool calling."""
+
import os
import base64
import asyncio
+import logging
import tempfile
import threading
import gradio as gr
@@ -13,11 +16,13 @@ from langchain_core.messages import HumanMessage
load_dotenv()
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
+logger = logging.getLogger(__name__)
# ── Image helpers ────────────────────────────────────────────────────────────
def image_to_base64(image: Image.Image) -> str:
+ """Convert a PIL image to a base64-encoded JPEG string for use in API requests."""
# Write to a real temp file first to ensure a complete, valid JPEG on disk
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
tmp_path = tmp.name
@@ -25,13 +30,14 @@ def image_to_base64(image: Image.Image) -> str:
with open(tmp_path, "rb") as f:
b64 = base64.b64encode(f.read()).decode("utf-8")
os.remove(tmp_path)
- print(f"[base64 preview]: {b64[:100]!r} (total length: {len(b64)})")
+ logger.debug("[base64 preview]: %r (total length: %d)", b64[:100], len(b64))
return b64
# ── City identification via Mistral Small (vision) ───────────────────────────
def identify_city(image: Image.Image) -> str:
+ """Use Mistral Small vision to read a map image and return the identified city name."""
b64 = image_to_base64(image)
llm = ChatMistralAI(model="mistral-small-latest", api_key=MISTRAL_API_KEY)
message = HumanMessage(content=[
@@ -40,7 +46,7 @@ def identify_city(image: Image.Image) -> str:
])
response = llm.invoke([message])
raw = response.content.strip()
- print(f"[mistral vision identified city]: {raw!r}")
+ logger.debug("[mistral vision identified city]: %r", raw)
return raw
@@ -122,6 +128,7 @@ _WEATHER_TOOL_SCHEMA = {
def call_weather_tool(city: str) -> str:
+ """Ask Mistral Small to invoke the weather tool for the given city and return the formatted result."""
llm = ChatMistralAI(model="mistral-small-latest", api_key=MISTRAL_API_KEY)
llm_with_tools = llm.bind_tools([_WEATHER_TOOL_SCHEMA])
response = llm_with_tools.invoke([HumanMessage(content=f"Get the current weather for {city}.")])
@@ -134,6 +141,7 @@ def call_weather_tool(city: str) -> str:
# ── Output parser ─────────────────────────────────────────────────────────────
def parse_weather(text: str) -> tuple:
+ """Extract temperature, conditions, humidity, and wind speed from a formatted weather string."""
temp = conditions = humidity = wind = ""
for line in text.splitlines():
if "Temperature:" in line:
@@ -150,6 +158,7 @@ def parse_weather(text: str) -> tuple:
# ── Main pipeline ─────────────────────────────────────────────────────────────
def analyze(image):
+ """Run the full pipeline: identify the city from the uploaded map image, then fetch and return live weather data."""
if image is None:
return "Please upload a map image.", "", "", "", ""