Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ import re
|
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
def preprocess_data(df):
|
| 11 |
-
|
| 12 |
# Renaming the 'Queries' column to 'texts'
|
| 13 |
df.rename(columns={'Queries': 'texts'}, inplace=True)
|
| 14 |
|
|
@@ -21,7 +20,6 @@ def preprocess_data(df):
|
|
| 21 |
# Remove URL from text
|
| 22 |
df['texts'] = df['texts'].apply(lambda text: re.sub(r'https?://\S+|www\.\S+', '', text))
|
| 23 |
|
| 24 |
-
|
| 25 |
# Remove emojis from text
|
| 26 |
def remove_emoji(string):
|
| 27 |
emoji_pattern = re.compile("["
|
|
@@ -98,7 +96,6 @@ def preprocess_data(df):
|
|
| 98 |
for phrase in remove_phrases:
|
| 99 |
df['texts'] = df['texts'].str.replace(phrase, '')
|
| 100 |
|
| 101 |
-
|
| 102 |
# Drop rows containing any general words from response and its variations
|
| 103 |
general_variations = ["good morning", "good evening", "good afternoon", "good night", "done", "sorry", "top", "query",
|
| 104 |
"stop", "sir", "sure", "oh", "wow", "aaa", "maam", "mam", "ma'am","i'm all set","ask a question","apply the survey",
|
|
@@ -128,12 +125,9 @@ def preprocess_data(df):
|
|
| 128 |
df['texts'] = df['texts'].apply(lambda x: x.strip()) # Remove leading and trailing whitespaces
|
| 129 |
df = df[df['texts'] != '']
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
return df
|
| 134 |
|
| 135 |
def cluster_data(df, num_clusters=5):
|
| 136 |
-
|
| 137 |
# Vectorize the text data
|
| 138 |
vectorizer = TfidfVectorizer(stop_words='english')
|
| 139 |
X = vectorizer.fit_transform(df['texts'])
|
|
@@ -149,10 +143,8 @@ def cluster_data(df, num_clusters=5):
|
|
| 149 |
df['PCA1'] = principal_components[:, 0]
|
| 150 |
df['PCA2'] = principal_components[:, 1]
|
| 151 |
|
| 152 |
-
|
| 153 |
return df
|
| 154 |
|
| 155 |
-
|
| 156 |
def visualize_clusters(df):
|
| 157 |
plt.figure(figsize=(10, 6))
|
| 158 |
scatter = plt.scatter(df['PCA1'], df['PCA2'], c=df['Cluster'], cmap='viridis')
|
|
@@ -162,21 +154,20 @@ def visualize_clusters(df):
|
|
| 162 |
plt.ylabel('PCA Component 2')
|
| 163 |
plt.show()
|
| 164 |
|
| 165 |
-
|
| 166 |
def main(file, num_clusters):
|
| 167 |
try:
|
| 168 |
-
|
| 169 |
df = pd.read_excel(file)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
df = preprocess_data(df)
|
| 173 |
df = cluster_data(df, num_clusters)
|
| 174 |
visualize_clusters(df)
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
return
|
| 178 |
except Exception as e:
|
| 179 |
-
|
| 180 |
return str(e)
|
| 181 |
|
| 182 |
interface = gr.Interface(
|
|
@@ -185,9 +176,9 @@ interface = gr.Interface(
|
|
| 185 |
gr.File(label="Upload Excel File (.xlsx)"),
|
| 186 |
gr.Number(value=5, label="Number of Clusters")
|
| 187 |
],
|
| 188 |
-
outputs=gr.
|
| 189 |
title="Unanswered User Queries Clustering",
|
| 190 |
description="Upload an Excel file (.xlsx)"
|
| 191 |
)
|
| 192 |
|
| 193 |
-
interface.launch()
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
def preprocess_data(df):
|
|
|
|
| 11 |
# Renaming the 'Queries' column to 'texts'
|
| 12 |
df.rename(columns={'Queries': 'texts'}, inplace=True)
|
| 13 |
|
|
|
|
| 20 |
# Remove URL from text
|
| 21 |
df['texts'] = df['texts'].apply(lambda text: re.sub(r'https?://\S+|www\.\S+', '', text))
|
| 22 |
|
|
|
|
| 23 |
# Remove emojis from text
|
| 24 |
def remove_emoji(string):
|
| 25 |
emoji_pattern = re.compile("["
|
|
|
|
| 96 |
for phrase in remove_phrases:
|
| 97 |
df['texts'] = df['texts'].str.replace(phrase, '')
|
| 98 |
|
|
|
|
| 99 |
# Drop rows containing any general words from response and its variations
|
| 100 |
general_variations = ["good morning", "good evening", "good afternoon", "good night", "done", "sorry", "top", "query",
|
| 101 |
"stop", "sir", "sure", "oh", "wow", "aaa", "maam", "mam", "ma'am","i'm all set","ask a question","apply the survey",
|
|
|
|
| 125 |
df['texts'] = df['texts'].apply(lambda x: x.strip()) # Remove leading and trailing whitespaces
|
| 126 |
df = df[df['texts'] != '']
|
| 127 |
|
|
|
|
|
|
|
| 128 |
return df
|
| 129 |
|
| 130 |
def cluster_data(df, num_clusters=5):
|
|
|
|
| 131 |
# Vectorize the text data
|
| 132 |
vectorizer = TfidfVectorizer(stop_words='english')
|
| 133 |
X = vectorizer.fit_transform(df['texts'])
|
|
|
|
| 143 |
df['PCA1'] = principal_components[:, 0]
|
| 144 |
df['PCA2'] = principal_components[:, 1]
|
| 145 |
|
|
|
|
| 146 |
return df
|
| 147 |
|
|
|
|
| 148 |
def visualize_clusters(df):
|
| 149 |
plt.figure(figsize=(10, 6))
|
| 150 |
scatter = plt.scatter(df['PCA1'], df['PCA2'], c=df['Cluster'], cmap='viridis')
|
|
|
|
| 154 |
plt.ylabel('PCA Component 2')
|
| 155 |
plt.show()
|
| 156 |
|
|
|
|
| 157 |
def main(file, num_clusters):
|
| 158 |
try:
|
|
|
|
| 159 |
df = pd.read_excel(file)
|
|
|
|
|
|
|
| 160 |
df = preprocess_data(df)
|
| 161 |
df = cluster_data(df, num_clusters)
|
| 162 |
visualize_clusters(df)
|
| 163 |
|
| 164 |
+
# Save the DataFrame to a CSV file
|
| 165 |
+
output = BytesIO()
|
| 166 |
+
df.to_csv(output, index=False)
|
| 167 |
+
output.seek(0)
|
| 168 |
|
| 169 |
+
return output
|
| 170 |
except Exception as e:
|
|
|
|
| 171 |
return str(e)
|
| 172 |
|
| 173 |
interface = gr.Interface(
|
|
|
|
| 176 |
gr.File(label="Upload Excel File (.xlsx)"),
|
| 177 |
gr.Number(value=5, label="Number of Clusters")
|
| 178 |
],
|
| 179 |
+
outputs=gr.File(label="Clustered Data CSV"),
|
| 180 |
title="Unanswered User Queries Clustering",
|
| 181 |
description="Upload an Excel file (.xlsx)"
|
| 182 |
)
|
| 183 |
|
| 184 |
+
interface.launch()
|