Subham9126 commited on
Commit
6753bb8
·
verified ·
1 Parent(s): a6d4f8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
+ import torch
4
+
5
+ # Load the SentenceTransformer model
6
+ model = SentenceTransformer('all-MiniLM-L6-v2')
7
+
8
+ def find_relevant_words(query, data, top_k):
9
+ # Convert data string to list
10
+ word_list = [word.strip() for word in data.split(',')]
11
+
12
+ # Create embeddings
13
+ query_embedding = model.encode(query, convert_to_tensor=True)
14
+ word_embeddings = model.encode(word_list, convert_to_tensor=True)
15
+
16
+ # Compute cosine similarities
17
+ cos_scores = util.cos_sim(query_embedding, word_embeddings)[0]
18
+
19
+ # Get top-k results
20
+ top_results = torch.topk(cos_scores, k=min(top_k, len(word_list)))
21
+
22
+ results = []
23
+ for score, idx in zip(top_results.values, top_results.indices):
24
+ results.append(f"{word_list[idx]} (Score: {score:.4f})")
25
+
26
+ return "\n".join(results)
27
+
28
+ # Create Gradio interface
29
+ iface = gr.Interface(
30
+ fn=find_relevant_words,
31
+ inputs=[
32
+ gr.Textbox(label="Query"),
33
+ gr.Textbox(label="Data (comma-separated words)"),
34
+ gr.Slider(minimum=1, maximum=20, step=1, label="Top K", value=5)
35
+ ],
36
+ outputs=gr.Textbox(label="Results"),
37
+ title="Semantic Word Relevance Finder",
38
+ description="Enter a query and a list of words to find the most semantically relevant words."
39
+ )
40
+
41
+ # Launch the app
42
+ iface.launch()