Share to gain more social capita

Written by — Mika Heino, Data Architect
Discover how to build a "Not Hotdog" image recognition app entirely inside Snowflake using Streamlit, Cortex Complete Multimodal, and native camera input.
Written by — Mika Heino, Data Architect
Share to gain more social capita
I love HBO's Silicon Valley TV series (though maybe not the final seasons).
Anyone who's seen the show knows Jian-Yang’s iconic Not Hot Dog app. It's been recreated countless times in different ways, largely because it's a great hands-on learning experience for image classification and model training.
When Snowflake introduced Cortex Complete Multimodal and st.camera_input
support in Streamlit within Snowflake (SiS), I realized I could build the app entirely using native Snowflake components.
So, without further ado, here’s the code. You can also find the same code on my GitHub account for cloning.
Do this first in Snowsight (with a role that has privileges to create databases, warehouses, stages and Streamlit -application).
-- create database and schema to host streamlit app CREATE OR REPLACE DATABASE DB_HD ; CREATE OR REPLACE SCHEMA SCHEMA_HD ; CREATE OR REPLACE STAGE DB_HD.SCHEMA_HD.IMAGES DIRECTORY = (ENABLE = TRUE) ENCRYPTION = (TYPE = 'SNOWFLAKE_SSE'); GRANT READ, WRITE ON STAGE DB_HD.SCHEMA_HD.IMAGES TO ROLE PUBLIC; --upload app.py to images stage USE DATABASE DB_HD ; USE SCHEMA SCHEMA_HD ; --create wh to use and grant access to it CREATE OR REPLACE WAREHOUSE HD_WH ; GRANT USAGE ON WAREHOUSE HD_WH TO PUBLIC ; |
Then create a Streamlit app and copy paste this code
import os, json, tempfile, streamlit as st from snowflake.snowpark import Session st.set_page_config(page_title="Not Hotdog", layout="wide", initial_sidebar_state="collapsed") st.markdown("""<style>body,.stApp{background:#000;color:#fff;text-align:center} .result-hotdog{color:#0F0;font-size:4em;font-weight:700} .result-nothotdog{color:red;font-size:4em;font-weight:700}</style>""", unsafe_allow_html=True) if not (pic := st.camera_input("")): st.stop() with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f: f.write(pic.getvalue()); path = f.name s = Session.builder.getOrCreate() s.file.put(path, "@IMAGES", overwrite=False, auto_compress=False) f = next((x.name for x in s.sql("list @IMAGES").collect() if x.name.endswith(os.path.basename(path))), None) if not f: st.stop() p = """1. Does this image contain a hot dog? Answer: { "hotdog": true|false } 2. What is in the image? Use key "description". Return one JSON like: { "hotdog": true, "description": "a hot dog with mustard on a plate" }""" q = f"select snowflake.cortex.complete('claude-3-5-sonnet', $$ {p} $$, to_file('@IMAGES','{f.split('/',1)[-1]}'))" try: r = json.loads(s.sql(q).collect()[0][0]) c, t = ("result-hotdog", "🌭 HOT DOG") if r.get("hotdog") else ("result-nothotdog", "🚫 NOT HOT DOG") st.markdown(f"<div class='{c}'>{t}</div>", unsafe_allow_html=True) if d := r.get("description"): st.markdown(f"<div style='margin-top:1em;font-size:1.3em;color:#ccc'>Detected: {d.capitalize()}</div>", unsafe_allow_html=True) except Exception as e: st.error(f"❌ Error: {e}") |
You're done.
This app leverages Snowflake's integrated features to perform image classification efficiently:
st.camera_input
(which just went live) widget captures an image directly from your device's camera within the Streamlit app.@IMAGES
) using the Snowpark API, enabling secure and efficient storage within Snowflake's environment.Cortex COMPLETE
function is invoked with the claude-3-5-sonnet
model, processing the image to determine if it contains a hot dog and providing a description. This function allows for advanced image analysis directly within Snowflake using simple SQL commandsThe end result runs directly in the Snowflake UI, which can be accessed on any modern phone with a browser, as shown here. Unfortunately, I didn’t have any hot dogs available for this test run.