Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,27 +13,25 @@ def get_embedding(text):
|
|
| 13 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 14 |
with torch.no_grad():
|
| 15 |
outputs = model(**inputs)
|
| 16 |
-
# Use .squeeze() to remove single-dimensional entries from the shape of the array.
|
| 17 |
return outputs.pooler_output.squeeze().numpy()
|
| 18 |
|
| 19 |
def compare_texts(*inputs):
|
| 20 |
categories = ["Hobbies", "Favorite Foods", "Travel Preferences", "Music Taste", "Movie/TV Preferences"]
|
| 21 |
similarities = []
|
|
|
|
| 22 |
for i in range(len(categories)):
|
| 23 |
text1, text2 = inputs[i*2], inputs[i*2 + 1]
|
| 24 |
embedding1 = get_embedding(text1)
|
| 25 |
embedding2 = get_embedding(text2)
|
| 26 |
-
# Ensure both embeddings are 2D by using [embedding1], [embedding2].
|
| 27 |
similarity = cosine_similarity([embedding1], [embedding2])[0][0]
|
| 28 |
similarities.append(similarity)
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
average_similarity = np.mean(similarities)
|
| 32 |
percentage_similarity = round(average_similarity * 100, 2)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Remainder of your Gradio application setup remains the same
|
| 36 |
-
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("# Couple Compatibility Checker")
|
|
@@ -45,7 +43,7 @@ with gr.Blocks() as demo:
|
|
| 45 |
inputs.append(gr.Textbox(label=f"Person 1's {category}"))
|
| 46 |
inputs.append(gr.Textbox(label=f"Person 2's {category}"))
|
| 47 |
btn_compare = gr.Button("Compare")
|
| 48 |
-
result = gr.Textbox(label="Compatibility
|
| 49 |
|
| 50 |
btn_compare.click(compare_texts, inputs=inputs, outputs=result)
|
| 51 |
|
|
|
|
| 13 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 14 |
with torch.no_grad():
|
| 15 |
outputs = model(**inputs)
|
|
|
|
| 16 |
return outputs.pooler_output.squeeze().numpy()
|
| 17 |
|
| 18 |
def compare_texts(*inputs):
|
| 19 |
categories = ["Hobbies", "Favorite Foods", "Travel Preferences", "Music Taste", "Movie/TV Preferences"]
|
| 20 |
similarities = []
|
| 21 |
+
results = []
|
| 22 |
for i in range(len(categories)):
|
| 23 |
text1, text2 = inputs[i*2], inputs[i*2 + 1]
|
| 24 |
embedding1 = get_embedding(text1)
|
| 25 |
embedding2 = get_embedding(text2)
|
|
|
|
| 26 |
similarity = cosine_similarity([embedding1], [embedding2])[0][0]
|
| 27 |
similarities.append(similarity)
|
| 28 |
+
results.append(f"{categories[i]} similarity: {similarity:.2f}%")
|
| 29 |
+
print(f"{categories[i]} similarity: {similarity:.2f}%")
|
| 30 |
|
| 31 |
average_similarity = np.mean(similarities)
|
| 32 |
percentage_similarity = round(average_similarity * 100, 2)
|
| 33 |
+
results.append(f"Overall Compatibility: {percentage_similarity}%")
|
| 34 |
+
return "\n".join(results)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown("# Couple Compatibility Checker")
|
|
|
|
| 43 |
inputs.append(gr.Textbox(label=f"Person 1's {category}"))
|
| 44 |
inputs.append(gr.Textbox(label=f"Person 2's {category}"))
|
| 45 |
btn_compare = gr.Button("Compare")
|
| 46 |
+
result = gr.Textbox(label="Compatibility Results", interactive=False, lines=10)
|
| 47 |
|
| 48 |
btn_compare.click(compare_texts, inputs=inputs, outputs=result)
|
| 49 |
|