Classificação de texto

Introdução à IA Generativa no Snowflake

James Cha-Earley

Senior Developer Advocate, Snowflake

Desafios de classificar texto

O problema

$$

  • Avaliações podem conter qualquer informação

$$

  • Categorizar manualmente é demorado e não escala

A solução

$$

  • Automatize com Snowflake Cortex!

$$

  • classify_text()
Introdução à IA Generativa no Snowflake

Definindo categorias

# Definir categorias
categories = ["overall_experience", "location", "staff", "food_beverages",
              "facilities"]
Introdução à IA Generativa no Snowflake

Classificando texto

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"  
}
Introdução à IA Generativa no Snowflake

Convertendo saídas para dicionário

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

category_dict = json.loads(category)
print(type(category_dict))
<class 'dict'>
Introdução à IA Generativa no Snowflake

Escalando o fluxo de trabalho

  • Pipeline de classificação categorizando avaliações de um mês
# Código em Python
month = 5
-- Consulta SQL
SELECT *
FROM HOTELS.REVIEWS
WHERE EXTRACT(month FROM date) = '{{month}}'
Introdução à IA Generativa no Snowflake

Aplicando classify_text()

df = dataframe_1.to_pandas()

def classification(text): result = classify_text( str_input=text, categories=labels ) result_dict = json.loads(result) return result_dict["label"]
Introdução à IA Generativa no Snowflake

Aplicando classify_text()

# Aplicar a classificação ao DataFrame
df["category"] = reviews["DESCRIPTION"].apply(classification)

# Imprimir a categoria prevista da primeira linha print(df["category"].head(1))
0    overall_experience  
Name: category, dtype: object
Introdução à IA Generativa no Snowflake

Análise de sentimento

  • Interessado apenas no sentimento das avaliações

AI_SENTIMENT em uma célula SQL

Introdução à IA Generativa no Snowflake

Análise de sentimento

AI_SENTIMENT - extraindo um sentimento

$$

  • Sentimentos disponíveis: positive, negative, neutral, mixed e unknown
Introdução à IA Generativa no Snowflake

Vamos praticar!

Introdução à IA Generativa no Snowflake

Preparing Video For Download...