DexterSptizu commited on
Commit
83b72c1
1 Parent(s): 9732442

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -18
app.py CHANGED
@@ -1,37 +1,67 @@
1
  import gradio as gr
2
  from wordllama import WordLlama
 
 
 
3
 
4
  # Load the default WordLlama model
5
  wl = WordLlama.load()
6
 
7
- # Define the function that calculates similarity between two sentences
8
- def calculate_similarity(sentence1, sentence2):
9
- similarity_score = wl.similarity(sentence1, sentence2)
10
- return similarity_score
11
 
12
- # Define five example inputs
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  examples = [
14
  ["I went to the car", "I went to the pawn shop"],
15
  ["The cat is on the roof", "A dog is in the yard"],
16
  ["She loves playing tennis", "She enjoys sports"],
17
  ["This is a bright day", "It's a sunny morning"],
18
- ["I bought a new phone", "I got a new mobile"]
 
 
 
 
 
19
  ]
20
 
21
- # Define Gradio interface
22
  with gr.Blocks() as iface:
23
- gr.Markdown("# Sentence Similarity with WordLlama")
24
- gr.Markdown("Calculate the similarity between two sentences using the WordLlama model from Hugging Face.")
 
 
 
 
 
 
25
 
26
- sentence1 = gr.Textbox(lines=2, placeholder="Enter first sentence...")
27
- sentence2 = gr.Textbox(lines=2, placeholder="Enter second sentence...")
28
- output = gr.Number()
29
 
30
- # Button to trigger similarity calculation
31
- button = gr.Button("Calculate Similarity")
32
- button.click(calculate_similarity, inputs=[sentence1, sentence2], outputs=output)
 
 
33
 
34
- # Examples section
35
- gr.Examples(examples=examples, inputs=[sentence1, sentence2])
 
 
 
36
 
37
- iface.launch(share=True)
 
1
  import gradio as gr
2
  from wordllama import WordLlama
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ import numpy as np
6
 
7
  # Load the default WordLlama model
8
  wl = WordLlama.load()
9
 
10
+ # Initialize TF-IDF vectorizer
11
+ tfidf_vectorizer = TfidfVectorizer()
 
 
12
 
13
+ def calculate_similarities(sentence1, sentence2):
14
+ # WordLlama similarity
15
+ wordllama_score = wl.similarity(sentence1, sentence2)
16
+
17
+ # TF-IDF similarity
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 {
22
+ "WordLlama Similarity": float(wordllama_score),
23
+ "TF-IDF Similarity": float(tfidf_score)
24
+ }
25
+
26
+ # Extended examples with more diverse sentences
27
  examples = [
28
  ["I went to the car", "I went to the pawn shop"],
29
  ["The cat is on the roof", "A dog is in the yard"],
30
  ["She loves playing tennis", "She enjoys sports"],
31
  ["This is a bright day", "It's a sunny morning"],
32
+ ["I bought a new phone", "I got a new mobile"],
33
+ ["The restaurant serves delicious food", "This place has great cuisine"],
34
+ ["Python is a programming language", "Java is used for coding"],
35
+ ["The movie was entertaining", "I enjoyed watching the film"],
36
+ ["Climate change affects our planet", "Global warming is a serious issue"],
37
+ ["Students study in the library", "People read books in the library"]
38
  ]
39
 
40
+ # Define Gradio interface with updated layout
41
  with gr.Blocks() as iface:
42
+ gr.Markdown("# Text Similarity Comparison")
43
+ gr.Markdown("Compare sentences using both WordLlama and TF-IDF similarity metrics")
44
+
45
+ with gr.Row():
46
+ sentence1 = gr.Textbox(lines=2, placeholder="Enter first sentence...", label="First Sentence")
47
+ sentence2 = gr.Textbox(lines=2, placeholder="Enter second sentence...", label="Second Sentence")
48
+
49
+ button = gr.Button("Calculate Similarities")
50
 
51
+ with gr.Row():
52
+ wordllama_output = gr.Number(label="WordLlama Similarity")
53
+ tfidf_output = gr.Number(label="TF-IDF Similarity")
54
 
55
+ button.click(
56
+ calculate_similarities,
57
+ inputs=[sentence1, sentence2],
58
+ outputs=[wordllama_output, tfidf_output]
59
+ )
60
 
61
+ gr.Markdown("### Example Sentence Pairs")
62
+ gr.Examples(
63
+ examples=examples,
64
+ inputs=[sentence1, sentence2]
65
+ )
66
 
67
+ iface.launch(share=True)