DexterSptizu
commited on
Commit
•
6773f13
1
Parent(s):
cfe8e38
Update app.py
Browse files
app.py
CHANGED
@@ -18,11 +18,11 @@ def calculate_similarities(sentence1, sentence2):
|
|
18 |
tfidf_matrix = tfidf_vectorizer.fit_transform([sentence1, sentence2])
|
19 |
tfidf_score = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
|
20 |
|
21 |
-
# Return two separate values instead of a dictionary
|
22 |
return float(wordllama_score), float(tfidf_score)
|
23 |
|
24 |
-
#
|
25 |
examples = [
|
|
|
26 |
["I went to the car", "I went to the pawn shop"],
|
27 |
["The cat is on the roof", "A dog is in the yard"],
|
28 |
["She loves playing tennis", "She enjoys sports"],
|
@@ -32,23 +32,72 @@ examples = [
|
|
32 |
["Python is a programming language", "Java is used for coding"],
|
33 |
["The movie was entertaining", "I enjoyed watching the film"],
|
34 |
["Climate change affects our planet", "Global warming is a serious issue"],
|
35 |
-
["Students study in the library", "People read books in the library"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
]
|
37 |
|
38 |
# Define Gradio interface with updated layout
|
39 |
-
with gr.Blocks() as iface:
|
40 |
-
gr.Markdown("# Text Similarity Comparison")
|
41 |
-
gr.Markdown("
|
|
|
|
|
|
|
42 |
|
43 |
with gr.Row():
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
button = gr.Button("Calculate Similarities")
|
48 |
|
49 |
with gr.Row():
|
50 |
-
wordllama_output = gr.Number(
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
button.click(
|
54 |
calculate_similarities,
|
@@ -56,10 +105,11 @@ with gr.Blocks() as iface:
|
|
56 |
outputs=[wordllama_output, tfidf_output]
|
57 |
)
|
58 |
|
59 |
-
gr.Markdown("### Example Sentence Pairs")
|
60 |
gr.Examples(
|
61 |
examples=examples,
|
62 |
-
inputs=[sentence1, sentence2]
|
|
|
63 |
)
|
64 |
|
|
|
65 |
iface.launch(share=True)
|
|
|
18 |
tfidf_matrix = tfidf_vectorizer.fit_transform([sentence1, sentence2])
|
19 |
tfidf_score = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
|
20 |
|
|
|
21 |
return float(wordllama_score), float(tfidf_score)
|
22 |
|
23 |
+
# Examples combining original and new homophone-based examples
|
24 |
examples = [
|
25 |
+
# Original examples
|
26 |
["I went to the car", "I went to the pawn shop"],
|
27 |
["The cat is on the roof", "A dog is in the yard"],
|
28 |
["She loves playing tennis", "She enjoys sports"],
|
|
|
32 |
["Python is a programming language", "Java is used for coding"],
|
33 |
["The movie was entertaining", "I enjoyed watching the film"],
|
34 |
["Climate change affects our planet", "Global warming is a serious issue"],
|
35 |
+
["Students study in the library", "People read books in the library"],
|
36 |
+
|
37 |
+
# New examples with similar words but different meanings
|
38 |
+
["The executive board met this morning", "I was so bored during the meeting"],
|
39 |
+
["Don't waste your time on this", "The dress fits perfectly at the waist"],
|
40 |
+
["The principal called a meeting", "It's a matter of principle"],
|
41 |
+
["The weather is beautiful today", "I don't know whether to go or stay"],
|
42 |
+
["I need a piece of the cake", "The world needs peace"],
|
43 |
+
["The bass was swimming in the lake", "Turn up the bass in the speaker"],
|
44 |
+
["The fair is in town this weekend", "That decision wasn't fair at all"],
|
45 |
+
["I need to address this letter", "What's your new address?"],
|
46 |
+
["The bank of the river is muddy", "I need to go to the bank for money"],
|
47 |
+
["Can you bear this weight?", "I saw a bear in the woods"]
|
48 |
]
|
49 |
|
50 |
# Define Gradio interface with updated layout
|
51 |
+
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
52 |
+
gr.Markdown("# Advanced Text Similarity Comparison")
|
53 |
+
gr.Markdown("""
|
54 |
+
Compare sentences using both WordLlama and TF-IDF similarity metrics.
|
55 |
+
This tool includes examples of similar words with different meanings to demonstrate semantic understanding.
|
56 |
+
""")
|
57 |
|
58 |
with gr.Row():
|
59 |
+
with gr.Column():
|
60 |
+
sentence1 = gr.Textbox(
|
61 |
+
lines=2,
|
62 |
+
placeholder="Enter first sentence...",
|
63 |
+
label="First Sentence",
|
64 |
+
info="Type or select from examples below"
|
65 |
+
)
|
66 |
+
with gr.Column():
|
67 |
+
sentence2 = gr.Textbox(
|
68 |
+
lines=2,
|
69 |
+
placeholder="Enter second sentence...",
|
70 |
+
label="Second Sentence",
|
71 |
+
info="Type or select from examples below"
|
72 |
+
)
|
73 |
|
74 |
+
button = gr.Button("Calculate Similarities", variant="primary")
|
75 |
|
76 |
with gr.Row():
|
77 |
+
wordllama_output = gr.Number(
|
78 |
+
label="WordLlama Similarity",
|
79 |
+
info="Contextual similarity score (0-1)",
|
80 |
+
value=0.0
|
81 |
+
)
|
82 |
+
tfidf_output = gr.Number(
|
83 |
+
label="TF-IDF Similarity",
|
84 |
+
info="Term frequency-based similarity score (0-1)",
|
85 |
+
value=0.0
|
86 |
+
)
|
87 |
+
|
88 |
+
gr.Markdown("""
|
89 |
+
### Understanding the Scores
|
90 |
+
- **WordLlama Similarity**: Measures semantic similarity considering context and meaning
|
91 |
+
- **TF-IDF Similarity**: Measures similarity based on word frequency and importance
|
92 |
+
""")
|
93 |
+
|
94 |
+
gr.Markdown("### Example Sentence Pairs")
|
95 |
+
gr.Markdown("""
|
96 |
+
The examples include:
|
97 |
+
- Regular sentence pairs
|
98 |
+
- Sentences with similar words but different meanings (homophones)
|
99 |
+
- Contextually related sentences
|
100 |
+
""")
|
101 |
|
102 |
button.click(
|
103 |
calculate_similarities,
|
|
|
105 |
outputs=[wordllama_output, tfidf_output]
|
106 |
)
|
107 |
|
|
|
108 |
gr.Examples(
|
109 |
examples=examples,
|
110 |
+
inputs=[sentence1, sentence2],
|
111 |
+
label="Click on any example to load it"
|
112 |
)
|
113 |
|
114 |
+
# Launch the interface
|
115 |
iface.launch(share=True)
|