File size: 1,674 Bytes
cbcd78b
 
 
ee68faf
cbcd78b
a32f02f
4530b74
52d8051
 
cbcd78b
a32f02f
4530b74
 
 
 
 
 
 
3fac692
3ae1eb6
cbcd78b
 
0180738
 
cbcd78b
3fac692
 
cbcd78b
 
3fac692
0a5100e
3fac692
 
 
 
 
 
 
cbcd78b
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
from transformers import pipeline
import pandas as pd
import spaces

# Load dataset
from datasets import load_dataset
ds = load_dataset('ZennyKenny/demo_customer_nps')
df = pd.DataFrame(ds['train'])

# Initialize model pipeline
from huggingface_hub import login
import os

# Login using the API key stored as an environment variable
hf_api_key = os.getenv("API_KEY")
login(token=hf_api_key)

classifier = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
generator = pipeline("text2text-generation", model="google/flan-t5-base")

# Function to classify customer comments
# https://huggingface.co/docs/hub/spaces-zerogpu
@spaces.GPU
def classify_comments():
    sentiments = []
    categories = []
    results = []
    for comment in df['customer_comment']:
        # Classify the sentiment first
        sentiment = classifier(comment)[0]['label']
        prompt = f"What category best describes this comment? '{comment}' Please answer using only the name of the category: Product Experience, Customer Support, Price of Service, Other."
        category = generator(prompt, max_length=30)[0]['generated_text']
        categories.append(category)
        sentiments.append(sentiment)
    df['comment_sentiment'] = sentiments
    df['comment_category'] = categories
    return df[['customer_comment', 'comment_sentiment', 'comment_category']].to_html(index=False)

# Gradio Interface
with gr.Blocks() as nps:
    gr.Markdown("# NPS Comment Categorization")
    classify_btn = gr.Button("Classify Comments")
    output = gr.HTML()

    classify_btn.click(fn=classify_comments, outputs=output)

nps.launch()