File size: 1,435 Bytes
cbcd78b
 
 
 
4530b74
 
 
52d8051
 
cbcd78b
 
4530b74
 
 
 
 
 
 
cbcd78b
 
 
4530b74
cbcd78b
 
 
4530b74
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
import gradio as gr
from transformers import pipeline
import pandas as pd

# Load the dataset
DATASET_URL = 'https://huggingface.co/datasets/ZennyKenny/demo_customer_nps/resolve/main/customer_feedback_dataset.csv'
from datasets import load_dataset
ds = load_dataset('ZennyKenny/demo_customer_nps')
df = pd.DataFrame(ds['train'])

# Initialize the 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)

pipe = pipeline("text-generation", model="mistralai/Mistral-Small-24B-Base-2501")

# Function to classify customer comments
@spaces.GPU
def classify_comments():
    results = []
    for comment in df['customer_comment']:
        prompt = f"Classify this customer feedback: '{comment}' into one of the following categories: Price of Service, Quality of Customer Support, Product Experience. Please only respond with the category name and nothing else."
        category = pipe(prompt, max_length=30)[0]['generated_text']
        results.append(category)
    df['comment_category'] = results
    return df[['customer_comment', '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()