Spaces:
Sleeping
Sleeping
| 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 | |
| def classify_comments(categories): | |
| global df # Ensure we're modifying the global DataFrame | |
| sentiments = [] | |
| assigned_categories = [] | |
| for comment in df['customer_comment']: | |
| # Classify sentiment | |
| sentiment = classifier(comment)[0]['label'] | |
| # Generate category | |
| category_str = ', '.join(categories) | |
| prompt = f"What category best describes this comment? '{comment}' Please answer using only the name of the category: {category_str}." | |
| category = generator(prompt, max_length=30)[0]['generated_text'] | |
| assigned_categories.append(category) | |
| sentiments.append(sentiment) | |
| df['comment_sentiment'] = sentiments | |
| df['comment_category'] = assigned_categories | |
| return df.to_html(index=False) # Return all fields with appended sentiment and category | |
| # Gradio Interface | |
| with gr.Blocks() as nps: | |
| # State to store categories | |
| categories = gr.State([]) | |
| # Function to add a category | |
| def add_category(categories, new_category): | |
| if new_category.strip() != "" and len(categories) < 5: # Limit to 5 categories | |
| categories.append(new_category.strip()) | |
| return categories, "", f"**Categories:**\n" + "\n".join([f"- {cat}" for cat in categories]) | |
| # Function to reset categories | |
| def reset_categories(): | |
| return [], "**Categories:**\n- None" | |
| # UI for adding categories | |
| with gr.Row(): | |
| category_input = gr.Textbox(label="New Category", placeholder="Enter category name") | |
| add_category_btn = gr.Button("Add Category") | |
| reset_btn = gr.Button("Reset Categories") | |
| category_status = gr.Markdown("**Categories:**\n- None") | |
| # File upload and template buttons | |
| uploaded_file = gr.File(label="Upload CSV", type="filepath") | |
| template_btn = gr.Button("Use Template") | |
| gr.Markdown("# NPS Comment Categorization") | |
| # Classify button | |
| classify_btn = gr.Button("Classify Comments") | |
| output = gr.HTML() | |
| # Function to load data from uploaded CSV | |
| def load_data(file): | |
| global df # Ensure we're modifying the global DataFrame | |
| if file is not None: | |
| file.seek(0) # Reset file pointer | |
| if file.name.endswith('.csv'): | |
| custom_df = pd.read_csv(file, encoding='utf-8') | |
| else: | |
| return "Error: Uploaded file is not a CSV." | |
| # Check for required columns | |
| required_columns = ['customer_comment'] | |
| if not all(col in custom_df.columns for col in required_columns): | |
| return f"Error: Uploaded CSV must contain the following column: {', '.join(required_columns)}" | |
| df = custom_df | |
| return "Custom CSV loaded successfully!" | |
| else: | |
| return "No file uploaded." | |
| # Function to use template categories | |
| def use_template(): | |
| template_categories = ["Product Experience", "Customer Support", "Price of Service", "Other"] | |
| return template_categories, f"**Categories:**\n" + "\n".join([f"- {cat}" for cat in template_categories]) | |
| # Event handlers | |
| add_category_btn.click( | |
| fn=add_category, | |
| inputs=[categories, category_input], | |
| outputs=[categories, category_input, category_status] | |
| ) | |
| reset_btn.click( | |
| fn=reset_categories, | |
| outputs=[categories, category_status] | |
| ) | |
| uploaded_file.change( | |
| fn=load_data, | |
| inputs=uploaded_file, | |
| outputs=output | |
| ) | |
| template_btn.click( | |
| fn=use_template, | |
| outputs=[categories, category_status] | |
| ) | |
| classify_btn.click( | |
| fn=classify_comments, | |
| inputs=categories, | |
| outputs=output | |
| ) | |
| nps.launch(share=True) |