tanish78 commited on
Commit
6aae5f7
·
verified ·
1 Parent(s): d4dc4a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -9,6 +9,7 @@ import re
9
  from io import BytesIO
10
  import tempfile
11
  import numpy as np
 
12
 
13
  def preprocess_data(df):
14
  df.rename(columns={'Question Asked': 'texts'}, inplace=True)
@@ -134,7 +135,8 @@ def visualize_clusters(df):
134
  buf = BytesIO()
135
  plt.savefig(buf, format='png')
136
  buf.seek(0)
137
- return buf
 
138
 
139
  def silhouette_analysis(X, labels, num_clusters):
140
  fig, ax1 = plt.subplots(1, 1)
@@ -166,7 +168,8 @@ def silhouette_analysis(X, labels, num_clusters):
166
  buf = BytesIO()
167
  plt.savefig(buf, format='png')
168
  buf.seek(0)
169
- return buf
 
170
 
171
  def main(file, num_clusters_to_display):
172
  try:
@@ -198,10 +201,10 @@ def main(file, num_clusters_to_display):
198
 
199
  with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmpfile:
200
  df.to_csv(tmpfile.name, index=False)
201
- return tmpfile.name, silhouette_avg, silhouette_plot
202
  except Exception as e:
203
  print(f"Error: {e}")
204
- return str(e), None, None
205
 
206
  interface = gr.Interface(
207
  fn=main,
@@ -212,6 +215,7 @@ interface = gr.Interface(
212
  outputs=[
213
  gr.File(label="Clustered Data CSV"),
214
  gr.Number(label="Silhouette Score"),
 
215
  gr.Image(label="Silhouette Plot")
216
  ],
217
  title="Unanswered User Queries Clustering",
 
9
  from io import BytesIO
10
  import tempfile
11
  import numpy as np
12
+ from PIL import Image
13
 
14
  def preprocess_data(df):
15
  df.rename(columns={'Question Asked': 'texts'}, inplace=True)
 
135
  buf = BytesIO()
136
  plt.savefig(buf, format='png')
137
  buf.seek(0)
138
+ img = Image.open(buf)
139
+ return img
140
 
141
  def silhouette_analysis(X, labels, num_clusters):
142
  fig, ax1 = plt.subplots(1, 1)
 
168
  buf = BytesIO()
169
  plt.savefig(buf, format='png')
170
  buf.seek(0)
171
+ img = Image.open(buf)
172
+ return img
173
 
174
  def main(file, num_clusters_to_display):
175
  try:
 
201
 
202
  with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmpfile:
203
  df.to_csv(tmpfile.name, index=False)
204
+ return tmpfile.name, silhouette_avg, cluster_plot, silhouette_plot
205
  except Exception as e:
206
  print(f"Error: {e}")
207
+ return str(e), None, None, None
208
 
209
  interface = gr.Interface(
210
  fn=main,
 
215
  outputs=[
216
  gr.File(label="Clustered Data CSV"),
217
  gr.Number(label="Silhouette Score"),
218
+ gr.Image(label="Cluster Plot"),
219
  gr.Image(label="Silhouette Plot")
220
  ],
221
  title="Unanswered User Queries Clustering",