Text classification

Introduction to Generative AI in Snowflake

James Cha-Earley

Senior Developer Advocate, Snowflake

Challenges of classifying text

The problem

$$

  • Reviews can contain any information

$$

  • Manual categorizing is time-consuming and not scalable

The solution

$$

  • Automate using Snowflake Cortex!

$$

  • classify_text()
Introduction to Generative AI in Snowflake

Defining categories

# Define categories
categories = ["overall_experience", "location", "staff", "food_beverages",
              "facilities"]
Introduction to Generative AI in Snowflake

Classifying text

from snowflake.cortex import classify_text

category = classify_text( str_input="The check-in was smooth and the staff were very friendly.", categories=categories )
print(category)
{  
  "label": "staff"  
}
Introduction to Generative AI in Snowflake

Converting outputs to the dictionary

print(type(category))
<class 'str'>
import json

category_dict = json.loads(category)
print(type(category_dict))
<class 'dict'>
Introduction to Generative AI in Snowflake

Scaling the workflow

  • Classification pipeline categorizing reviews for a given month
# Python code
month = 5
-- SQL query
SELECT *
FROM HOTELS.REVIEWS
WHERE EXTRACT(month FROM date) = '{{month}}'
Introduction to Generative AI in Snowflake

Applying classify_text()

df = cell2.to_pandas()

def classification(text): result = classify_text( str_input=text, categories=labels ) result_dict = json.loads(result) return result_dict["label"]
Introduction to Generative AI in Snowflake

Applying classify_text()

# Apply classification to the DataFrame
df["category"] = reviews["DESCRIPTION"].apply(classification)

# Print the first row's predicted category print(df["category"].head(1))
0    overall_experience  
Name: category, dtype: object
Introduction to Generative AI in Snowflake

Sentiment analysis

  • Only interested about reviews sentiment

AI_SENTIMENT in a SQL cell

Introduction to Generative AI in Snowflake

Sentiment analysis

AI_SENTIMENT - extracting a sentiment

$$

  • Available sentiments: positive, negative, neutral, mixed, and unknown
Introduction to Generative AI in Snowflake

Let's practice!

Introduction to Generative AI in Snowflake

Preparing Video For Download...