Subham9126 commited on
Commit
5640aad
·
verified ·
1 Parent(s): 7fddbcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -6,24 +6,22 @@ import re
6
  model = SentenceTransformer('all-MiniLM-L6-v2')
7
 
8
  def extract_lists(text):
9
- # Use regex to find all lists in the format ['item1', 'item2', ...]
10
- pattern = r'\[([^\]]+)\]'
11
- matches = re.findall(pattern, text)
12
-
13
- # Process each match into a list of keywords
14
  lists = []
15
- for match in matches:
16
- # Split by comma, strip whitespace and quotes, and filter out empty strings
17
- keywords = [item.strip().strip("'\"") for item in match.split(',') if item.strip()]
18
- if keywords: # Only add non-empty lists
19
- lists.append(keywords)
20
-
 
 
21
  return lists
22
 
23
  def compare_embeddings(query, lists_text):
24
  # Extract lists from the input text
25
  keyword_lists = extract_lists(lists_text)
26
-
27
  if not keyword_lists:
28
  return "No valid lists found in the input. Please check the format."
29
 
@@ -40,7 +38,6 @@ def compare_embeddings(query, lists_text):
40
 
41
  # Calculate average similarity for the list
42
  avg_similarity = similarities.mean().item()
43
-
44
  results.append((i, avg_similarity, keywords))
45
 
46
  # Sort results by similarity score (descending)
@@ -50,7 +47,7 @@ def compare_embeddings(query, lists_text):
50
  output = ""
51
  for i, score, keywords in results:
52
  output += f"List {i}: Similarity score {score:.4f}\n"
53
- output += f" Keywords: {', '.join(keywords)}\n\n"
54
 
55
  return output
56
 
@@ -61,13 +58,13 @@ iface = gr.Interface(
61
  gr.Textbox(label="Query"),
62
  gr.Textbox(
63
  label="Lists of keywords",
64
- placeholder="Enter lists in the format: ['keyword1', 'keyword2', ...] ['keyword3', 'keyword4', ...]",
65
  lines=5
66
  ),
67
  ],
68
  outputs=gr.Textbox(label="Results"),
69
  title="Keyword Lists Comparison App",
70
- description="Compare a query with multiple lists of keywords and find the most relevant lists. Enter each list in square brackets, separated by commas."
71
  )
72
 
73
  # Launch the app
 
6
  model = SentenceTransformer('all-MiniLM-L6-v2')
7
 
8
  def extract_lists(text):
9
+ # Split the input text by newlines and process each line
10
+ lines = text.split('\n')
 
 
 
11
  lists = []
12
+ for line in lines:
13
+ # Strip whitespace and check if the line is not empty
14
+ line = line.strip()
15
+ if line:
16
+ # Split the line by spaces and strip any remaining whitespace
17
+ keywords = [item.strip() for item in line.split() if item.strip()]
18
+ if keywords:
19
+ lists.append(keywords)
20
  return lists
21
 
22
  def compare_embeddings(query, lists_text):
23
  # Extract lists from the input text
24
  keyword_lists = extract_lists(lists_text)
 
25
  if not keyword_lists:
26
  return "No valid lists found in the input. Please check the format."
27
 
 
38
 
39
  # Calculate average similarity for the list
40
  avg_similarity = similarities.mean().item()
 
41
  results.append((i, avg_similarity, keywords))
42
 
43
  # Sort results by similarity score (descending)
 
47
  output = ""
48
  for i, score, keywords in results:
49
  output += f"List {i}: Similarity score {score:.4f}\n"
50
+ output += f" Keywords: {' '.join(keywords)}\n\n"
51
 
52
  return output
53
 
 
58
  gr.Textbox(label="Query"),
59
  gr.Textbox(
60
  label="Lists of keywords",
61
+ placeholder="Enter each list of keywords on a new line, with keywords separated by spaces.",
62
  lines=5
63
  ),
64
  ],
65
  outputs=gr.Textbox(label="Results"),
66
  title="Keyword Lists Comparison App",
67
+ description="Compare a query with multiple lists of keywords and find the most relevant lists. Enter each list on a new line, with keywords separated by spaces."
68
  )
69
 
70
  # Launch the app