tanish78 commited on
Commit
c246bae
·
verified ·
1 Parent(s): db51a4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -5
app.py CHANGED
@@ -10,6 +10,31 @@ import matplotlib.pyplot as plt
10
  import plotly.express as px
11
  from PIL import Image
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def preprocess_data(df):
14
  df.rename(columns={'Question Asked': 'texts'}, inplace=True)
15
  df['texts'] = df['texts'].astype(str).str.lower()
@@ -61,8 +86,12 @@ def preprocess_data(df):
61
  df['texts'] = df['texts'].str.strip()
62
  df = df[df['texts'] != '']
63
 
 
 
 
64
  return df
65
 
 
66
  def cluster_data(df, num_clusters):
67
  vectorizer = TfidfVectorizer(stop_words='english')
68
  X = vectorizer.fit_transform(df['texts'])
@@ -142,11 +171,14 @@ def main(file, num_clusters_to_display):
142
  df['Cluster'] = pd.Categorical(df['Cluster'], categories=filtered_clusters, ordered=True)
143
  df = df.sort_values('Cluster')
144
 
 
 
 
145
  wordcloud_img = generate_wordcloud(df)
146
  bar_chart_img = generate_bar_chart(df, num_clusters_to_display)
147
 
148
  with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmpfile:
149
- df.to_csv(tmpfile.name, index=False)
150
  csv_file_path = tmpfile.name
151
 
152
  return csv_file_path, wordcloud_img, bar_chart_img
@@ -154,6 +186,7 @@ def main(file, num_clusters_to_display):
154
  print(f"Error: {e}")
155
  return str(e), None, None
156
 
 
157
  interface = gr.Interface(
158
  fn=main,
159
  inputs=[
@@ -161,12 +194,12 @@ interface = gr.Interface(
161
  gr.Slider(label="Number of Categories to Display", minimum=1, maximum=10, step=1, value=5)
162
  ],
163
  outputs=[
164
- gr.File(label="Clustered Data CSV"),
165
  gr.Image(label="Word Cloud"),
166
  gr.Image(label="Bar Chart")
167
  ],
168
- title="Unanswered User Queries Clustering",
169
- description="Unanswered User Query Categorization"
170
  )
171
 
172
- interface.launch(share=True)
 
10
  import plotly.express as px
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'],
15
+ 'Volunteering': ['volunteering', 'volunteer', 'volunteering certificate', 'resume my volunteering', 'volunteering journey', 'volunteering with TFI'],
16
+ 'Certificates': ['certificate', 'certificates'],
17
+ 'Job Opportunities': ['job', 'vacancy', 'Talent Acquisition Executive job', 'opportunity'],
18
+ 'Surveys and Forms': ['survey', 'form', 'fill out the survey', 'application form'],
19
+ 'General Queries': ['query', 'queries', 'questions', 'thank', 'thanks', 'ok', 'ok thank you', 'thankyou', 'no thank you', 'feedback', 'loved', 'overwhelming'],
20
+ 'Spam': ['free recharge', 'offer', 'click the link', 'https', 'sorry', 'yes', 'no', 'ok', 'K', 'Sorry', 'yes.', 'noo', 'thnku', 'thx', 'thank'],
21
+ 'Rescheduling and Postponing': ['reschedule', 'postpone', 'cancellation', 'date', 'time slot'],
22
+ 'Contact and Communication Issues': ['call', 'phone', 'contact', 'not received'],
23
+ 'Email and Credentials Issues': ['email', 'credentials', 'received'],
24
+ 'Timing and Scheduling': ['session', 'time', 'interview', '6 baje', '23 feb', '12 april'],
25
+ 'Salary and Benefits': ['salary', 'increment', 'accommodation', 'training period', 'reside'],
26
+ 'Technical Issues': ['network issues', 'zoom meeting', 'passcode', 'technical', 'issue'],
27
+ 'Miscellaneous': []
28
+ }
29
+
30
+ def categorize_question(question):
31
+ for category, keywords in categories_keywords.items():
32
+ for keyword in keywords:
33
+ if keyword.lower() in question.lower():
34
+ return category
35
+ return 'Miscellaneous'
36
+
37
+
38
  def preprocess_data(df):
39
  df.rename(columns={'Question Asked': 'texts'}, inplace=True)
40
  df['texts'] = df['texts'].astype(str).str.lower()
 
86
  df['texts'] = df['texts'].str.strip()
87
  df = df[df['texts'] != '']
88
 
89
+ # Categorize the texts
90
+ df['Category'] = df['texts'].apply(categorize_question)
91
+
92
  return df
93
 
94
+
95
  def cluster_data(df, num_clusters):
96
  vectorizer = TfidfVectorizer(stop_words='english')
97
  X = vectorizer.fit_transform(df['texts'])
 
171
  df['Cluster'] = pd.Categorical(df['Cluster'], categories=filtered_clusters, ordered=True)
172
  df = df.sort_values('Cluster')
173
 
174
+ # Generate categorized output
175
+ categorized_df = df[['texts', 'Cluster', 'Category']].copy()
176
+
177
  wordcloud_img = generate_wordcloud(df)
178
  bar_chart_img = generate_bar_chart(df, num_clusters_to_display)
179
 
180
  with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmpfile:
181
+ categorized_df.to_csv(tmpfile.name, index=False)
182
  csv_file_path = tmpfile.name
183
 
184
  return csv_file_path, wordcloud_img, bar_chart_img
 
186
  print(f"Error: {e}")
187
  return str(e), None, None
188
 
189
+
190
  interface = gr.Interface(
191
  fn=main,
192
  inputs=[
 
194
  gr.Slider(label="Number of Categories to Display", minimum=1, maximum=10, step=1, value=5)
195
  ],
196
  outputs=[
197
+ gr.File(label="Categorized Data CSV"),
198
  gr.Image(label="Word Cloud"),
199
  gr.Image(label="Bar Chart")
200
  ],
201
+ title="Unanswered User Queries Categorization",
202
+ description="Categorize unanswered user queries into predefined categories"
203
  )
204
 
205
+ interface.launch(share=True)