Amirizaniani commited on
Commit
3a5974a
1 Parent(s): e2f6264

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -141,6 +141,31 @@ highlighted_clustered_sentences = []
141
  for cluster in clustered_sentences:
142
  highlighted_clustered_sentences.extend(highlight_words_within_cluster(cluster, model, exclude_words))
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  text_list = []
146
 
@@ -178,7 +203,12 @@ def setTextVisibility(cbg, model_name_input):
178
  for idx, sentence in enumerate(highlighted_clustered_sentences):
179
  result.append("<p><strong>"+ cbg[idx] +"</strong></p><p>"+ sentence +"</p><br/>")
180
 
181
- return result
 
 
 
 
 
182
 
183
 
184
  # update_show = [gr.Textbox(visible=True, label=text, value=answer_question(text, model_name_input)) for text in cbg]
@@ -224,7 +254,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
224
  #btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=text_list)
225
  btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=html_result)
226
  gr.HTML("""
227
- <div style="text-align: center; font-size: 24px; font-weight: bold;">Similarity Score: 76%</div>
228
  """)
229
 
230
  clear = gr.ClearButton(link = "http://127.0.0.1:7865")
 
141
  for cluster in clustered_sentences:
142
  highlighted_clustered_sentences.extend(highlight_words_within_cluster(cluster, model, exclude_words))
143
 
144
+
145
+ def calculate_similarity_score(sentences):
146
+ # Encode all sentences to get their embeddings
147
+ model = SentenceTransformer('all-MiniLM-L6-v2')
148
+ embeddings = model.encode(sentences)
149
+
150
+ # Calculate average cosine similarity
151
+ total_similarity = 0
152
+ comparisons = 0
153
+ for i in range(len(embeddings)):
154
+ for j in range(i+1, len(embeddings)):
155
+ # Cosine similarity between embeddings
156
+ similarity = 1 - cosine(embeddings[i], embeddings[j])
157
+ total_similarity += similarity
158
+ comparisons += 1
159
+
160
+ # Average similarity
161
+ average_similarity = total_similarity / comparisons if comparisons > 0 else 0
162
+
163
+ # Scale from [-1, 1] to [0, 100]
164
+ score_out_of_100 = (average_similarity + 1) / 2 * 100
165
+
166
+
167
+ return score_out_of_100
168
+
169
 
170
  text_list = []
171
 
 
203
  for idx, sentence in enumerate(highlighted_clustered_sentences):
204
  result.append("<p><strong>"+ cbg[idx] +"</strong></p><p>"+ sentence +"</p><br/>")
205
 
206
+ score = round(calculate_similarity_score(sentences))
207
+
208
+ final_html = f"""<div>{result}<div style="text-align: center; font-size: 24px; font-weight: bold;">Similarity Score: {score}</div></div>"""
209
+
210
+
211
+ return final_html
212
 
213
 
214
  # update_show = [gr.Textbox(visible=True, label=text, value=answer_question(text, model_name_input)) for text in cbg]
 
254
  #btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=text_list)
255
  btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=html_result)
256
  gr.HTML("""
257
+ <div style="text-align: center; font-size: 24px; font-weight: bold;">Similarity Score: </div>
258
  """)
259
 
260
  clear = gr.ClearButton(link = "http://127.0.0.1:7865")