Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,46 +11,38 @@ import plotly.express as px
|
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
categories_keywords = {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"Online Meetings": ["meeting", "meeting code", "passcode", "join meeting", "zoom", "zoom link"],
|
| 31 |
-
"Event Inquiries": ["event", "webinar", "meeting", "conference", "session", "seminar", "workshop", "invitation"],
|
| 32 |
-
"Payment Issues": ["payment", "billing", "transaction", "charge", "fee", "invoice", "refund", "receipt"],
|
| 33 |
-
"Registration Issues": ["registration", "register", "sign up", "enroll", "join", "signup", "enrollment"],
|
| 34 |
-
"Service Requests": ["service", "support", "request", "assistance", "help", "aid", "maintenance"],
|
| 35 |
-
"Account Issues": ["account", "profile", "update", "activation", "deactivation", "credentials", "reset"],
|
| 36 |
-
"Product Information": ["product", "service", "details", "info", "information", "specifications", "features"],
|
| 37 |
-
"Order Status": ["order", "status", "tracking", "shipment", "delivery", "purchase", "dispatch"],
|
| 38 |
-
"Miscellaneous": []
|
| 39 |
}
|
| 40 |
|
|
|
|
| 41 |
def categorize_question(question):
|
| 42 |
-
words = question.split()
|
| 43 |
-
if len(words) == 1:
|
| 44 |
-
single_word = words[0].lower()
|
| 45 |
-
if any(single_word in keyword for keyword in categories_keywords["Start of Conversation"]):
|
| 46 |
-
return "Start of Conversation"
|
| 47 |
-
else:
|
| 48 |
-
return "End of Conversation"
|
| 49 |
-
|
| 50 |
for category, keywords in categories_keywords.items():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def preprocess_data(df):
|
| 56 |
df.rename(columns={'Question Asked': 'texts'}, inplace=True)
|
|
@@ -103,6 +95,7 @@ def preprocess_data(df):
|
|
| 103 |
df['texts'] = df['texts'].str.strip()
|
| 104 |
df = df[df['texts'] != '']
|
| 105 |
|
|
|
|
| 106 |
df['Category'] = df['texts'].apply(categorize_question)
|
| 107 |
|
| 108 |
return df
|
|
@@ -145,6 +138,7 @@ def generate_wordcloud(df):
|
|
| 145 |
return img
|
| 146 |
|
| 147 |
def generate_bar_chart(df, num_clusters_to_display):
|
|
|
|
| 148 |
common_words = {'i', 'you', 'thanks', 'thank', 'ok', 'okay', 'sure', 'done'}
|
| 149 |
|
| 150 |
top_categories = df['Category'].value_counts().index[:num_clusters_to_display]
|
|
@@ -152,7 +146,6 @@ def generate_bar_chart(df, num_clusters_to_display):
|
|
| 152 |
|
| 153 |
category_top_words = df_top_categories.groupby('Category', observed=False)['texts'].apply(lambda x: ' '.join(x)).reset_index()
|
| 154 |
category_top_words['top_word'] = category_top_words['texts'].apply(lambda x: ' '.join([word for word in pd.Series(x.split()).value_counts().index if word not in common_words][:3]))
|
| 155 |
-
|
| 156 |
category_sizes = df_top_categories['Category'].value_counts().reset_index()
|
| 157 |
category_sizes.columns = ['Category', 'Count']
|
| 158 |
category_sizes = category_sizes.merge(category_top_words[['Category', 'top_word']], on='Category')
|
|
@@ -171,15 +164,30 @@ def main(file, num_clusters_to_display):
|
|
| 171 |
try:
|
| 172 |
df = pd.read_csv(file)
|
| 173 |
|
|
|
|
| 174 |
df = df[df['Answer'] == 'Fallback Message shown']
|
| 175 |
|
| 176 |
df = preprocess_data(df)
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmpfile:
|
| 182 |
-
|
| 183 |
csv_file_path = tmpfile.name
|
| 184 |
|
| 185 |
return csv_file_path, wordcloud_img, bar_chart_img
|
|
@@ -191,7 +199,7 @@ interface = gr.Interface(
|
|
| 191 |
fn=main,
|
| 192 |
inputs=[
|
| 193 |
gr.File(label="Upload CSV File (.csv)"),
|
| 194 |
-
gr.Slider(label="Number of Categories to Display", minimum=1, maximum=
|
| 195 |
],
|
| 196 |
outputs=[
|
| 197 |
gr.File(label="Categorized Data CSV"),
|
|
@@ -199,7 +207,7 @@ interface = gr.Interface(
|
|
| 199 |
gr.Image(label="Bar Chart")
|
| 200 |
],
|
| 201 |
title="Unanswered User Queries Categorization",
|
| 202 |
-
description="Categorize
|
| 203 |
)
|
| 204 |
|
| 205 |
interface.launch(share=True)
|
|
|
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
categories_keywords = {
|
| 14 |
+
'Application Status': ['application', 'applied', 'update on my application', 'result of my application', 'selected', 'selection process', 'apply', 'fellow', 'lesson plan', 'status of my application', 'application update', 'application status', 'applied for'],
|
| 15 |
+
'Volunteering': ['volunteering', 'volunteer', 'volunteering certificate', 'resume my volunteering', 'volunteering journey', 'volunteering with TFI', 'volunteering opportunities', 'volunteer work', 'volunteer program'],
|
| 16 |
+
'Certificates': ['certificate', 'certificates', 'certificate of completion', 'volunteer certificate', 'issue certificate'],
|
| 17 |
+
'Job Opportunities': ['job', 'vacancy', 'Talent Acquisition Executive job', 'opportunity', 'job opening', 'job position', 'career opportunities'],
|
| 18 |
+
'Surveys and Forms': ['survey', 'form', 'fill out the survey', 'application form', 'survey link', 'survey form', 'form submission'],
|
| 19 |
+
'General Queries': ['query', 'queries', 'questions', 'feedback', 'loved', 'overwhelming', 'general question', 'inquiry', 'query about'],
|
| 20 |
+
'Spam': ['free recharge', 'offer', 'click the link', 'https'],
|
| 21 |
+
'Rescheduling and Postponing': ['reschedule', 'postpone', 'cancellation', 'date', 'time slot', 'change date', 'change time', 'reschedule appointment'],
|
| 22 |
+
'Contact and Communication Issues': ['call', 'phone', 'contact', 'not received', 'contact support', 'phone call', 'call back', 'internet'],
|
| 23 |
+
'Email and Credentials Issues': ['email', 'credentials', 'received', 'email issue', 'email problem', 'credential issue', 'login problem'],
|
| 24 |
+
'Timing and Scheduling': ['session', 'time', 'interview', 'baje', 'schedule time', 'meeting time', 'appointment time'],
|
| 25 |
+
'Salary and Benefits': ['salary', 'increment', 'accommodation', 'training period', 'reside', 'stipend', 'pay', 'wage', 'salary details', 'benefits information'],
|
| 26 |
+
'Technical Issues': ['network issues', 'zoom meeting', 'passcode', 'technical', 'issue','technical problem', 'system issue', 'technical support'],
|
| 27 |
+
'Complaint Handling': ['help', 'i need help', 'Help me', 'complaint', 'issue is unresolved', 'unsatisfied', 'bad experience'],
|
| 28 |
+
'End of Conversation': ['thanks', 'thankss', 'thank u', 'thank you', 'ok', 'okay', 'done', 'joining', 'sounds good', 'goodbye', 'end chat', 'end'],
|
| 29 |
+
'Miscellaneous': []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
+
|
| 33 |
def categorize_question(question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
for category, keywords in categories_keywords.items():
|
| 35 |
+
for keyword in keywords:
|
| 36 |
+
if keyword.lower() in question.lower():
|
| 37 |
+
# Check if the question is one word and belongs to 'End of Conversation'
|
| 38 |
+
if category == 'End of Conversation':
|
| 39 |
+
return category
|
| 40 |
+
# If not 'End of Conversation', return the matched category
|
| 41 |
+
if category != 'End of Conversation':
|
| 42 |
+
return category
|
| 43 |
+
return 'Miscellaneous'
|
| 44 |
+
|
| 45 |
+
|
| 46 |
|
| 47 |
def preprocess_data(df):
|
| 48 |
df.rename(columns={'Question Asked': 'texts'}, inplace=True)
|
|
|
|
| 95 |
df['texts'] = df['texts'].str.strip()
|
| 96 |
df = df[df['texts'] != '']
|
| 97 |
|
| 98 |
+
# Categorize the texts
|
| 99 |
df['Category'] = df['texts'].apply(categorize_question)
|
| 100 |
|
| 101 |
return df
|
|
|
|
| 138 |
return img
|
| 139 |
|
| 140 |
def generate_bar_chart(df, num_clusters_to_display):
|
| 141 |
+
# Exclude common words from the top words
|
| 142 |
common_words = {'i', 'you', 'thanks', 'thank', 'ok', 'okay', 'sure', 'done'}
|
| 143 |
|
| 144 |
top_categories = df['Category'].value_counts().index[:num_clusters_to_display]
|
|
|
|
| 146 |
|
| 147 |
category_top_words = df_top_categories.groupby('Category', observed=False)['texts'].apply(lambda x: ' '.join(x)).reset_index()
|
| 148 |
category_top_words['top_word'] = category_top_words['texts'].apply(lambda x: ' '.join([word for word in pd.Series(x.split()).value_counts().index if word not in common_words][:3]))
|
|
|
|
| 149 |
category_sizes = df_top_categories['Category'].value_counts().reset_index()
|
| 150 |
category_sizes.columns = ['Category', 'Count']
|
| 151 |
category_sizes = category_sizes.merge(category_top_words[['Category', 'top_word']], on='Category')
|
|
|
|
| 164 |
try:
|
| 165 |
df = pd.read_csv(file)
|
| 166 |
|
| 167 |
+
# Filter by 'Fallback Message shown'
|
| 168 |
df = df[df['Answer'] == 'Fallback Message shown']
|
| 169 |
|
| 170 |
df = preprocess_data(df)
|
| 171 |
|
| 172 |
+
# Get category sizes and sort by size in ascending order
|
| 173 |
+
category_sizes = df['Category'].value_counts().reset_index()
|
| 174 |
+
category_sizes.columns = ['Category', 'Count']
|
| 175 |
+
sorted_categories = category_sizes.sort_values(by='Count', ascending=True)['Category'].tolist()
|
| 176 |
+
|
| 177 |
+
# Get the largest x categories as specified by num_clusters_to_display
|
| 178 |
+
largest_categories = sorted_categories[:num_clusters_to_display]
|
| 179 |
+
|
| 180 |
+
# Filter the dataframe to include only the largest categories
|
| 181 |
+
filtered_df = df[df['Category'].isin(largest_categories)]
|
| 182 |
+
|
| 183 |
+
# Sort the dataframe by Category
|
| 184 |
+
filtered_df = filtered_df.sort_values(by='Category')
|
| 185 |
+
|
| 186 |
+
wordcloud_img = generate_wordcloud(filtered_df)
|
| 187 |
+
bar_chart_img = generate_bar_chart(filtered_df, num_clusters_to_display)
|
| 188 |
|
| 189 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmpfile:
|
| 190 |
+
filtered_df.to_csv(tmpfile.name, index=False)
|
| 191 |
csv_file_path = tmpfile.name
|
| 192 |
|
| 193 |
return csv_file_path, wordcloud_img, bar_chart_img
|
|
|
|
| 199 |
fn=main,
|
| 200 |
inputs=[
|
| 201 |
gr.File(label="Upload CSV File (.csv)"),
|
| 202 |
+
gr.Slider(label="Number of Categories to Display", minimum=1, maximum=10, step=1, value=5)
|
| 203 |
],
|
| 204 |
outputs=[
|
| 205 |
gr.File(label="Categorized Data CSV"),
|
|
|
|
| 207 |
gr.Image(label="Bar Chart")
|
| 208 |
],
|
| 209 |
title="Unanswered User Queries Categorization",
|
| 210 |
+
description="Categorize unanswered user queries into predefined categories"
|
| 211 |
)
|
| 212 |
|
| 213 |
interface.launch(share=True)
|