asahi417 commited on
Commit
7c630e0
1 Parent(s): ac00d10
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -24,6 +24,15 @@ def greet(
24
  candidate_8,
25
  candidate_9,
26
  candidate_10):
 
 
 
 
 
 
 
 
 
27
  pairs = []
28
  pairs_id = []
29
  for n, i in enumerate([
@@ -38,32 +47,25 @@ def greet(
38
  candidate_9,
39
  candidate_10
40
  ]):
41
- query = query.split(',')
42
- # validate query
43
- if len(query) == 0:
44
- raise ValueError(f'ERROR: query is empty {query}')
45
- if len(query) == 1:
46
- raise ValueError(f'ERROR: query contains single word {query}')
47
- if len(query) > 2:
48
- raise ValueError(f'ERROR: query contains more than two word {query}')
49
-
50
- if i != '':
51
- if len(i.split(',')) == 1:
52
- raise ValueError(f'ERROR: candidate {n + 1} contains single word {i.split(",")}')
53
- if len(i.split(',')) > 2:
54
- raise ValueError(f'ERROR: candidate {n + 1} contains more than two word {i.split(",")}')
55
- pairs.append(i.split(','))
56
- pairs_id.append(n+1)
57
- if len(pairs_id) < 2:
58
- raise ValueError(f'ERROR: please specify at least two candidates: {pairs}')
59
- vectors = model.get_embedding(pairs+[query])
60
- vector_q = vectors.pop(-1)
61
- sims = []
62
- for v in vectors:
63
- sims.append(cosine_similarity(v, vector_q))
64
- output = sorted(list(zip(pairs_id, sims, pairs)), key=lambda _x: _x[1], reverse=True)
65
- output = {f'candidate {n}: [{p[0]}, {p[1]}]': s for n, (i, s, p) in enumerate(output)}
66
- return output
67
 
68
 
69
  demo = gr.Interface(
 
24
  candidate_8,
25
  candidate_9,
26
  candidate_10):
27
+ query = query.split(',')
28
+ # validate query
29
+ if len(query) == 0:
30
+ raise ValueError(f'ERROR: query is empty {query}')
31
+ if len(query) == 1:
32
+ raise ValueError(f'ERROR: query contains single word {query}')
33
+ if len(query) > 2:
34
+ raise ValueError(f'ERROR: query contains more than two word {query}')
35
+
36
  pairs = []
37
  pairs_id = []
38
  for n, i in enumerate([
 
47
  candidate_9,
48
  candidate_10
49
  ]):
50
+ if i == '':
51
+ continue
52
+ candidate = i.split(',')
53
+ if len(candidate) == 1:
54
+ raise ValueError(f'ERROR: candidate {n + 1} contains single word {candidate}')
55
+ if len(candidate) > 2:
56
+ raise ValueError(f'ERROR: candidate {n + 1} contains more than two word {candidate}')
57
+ pairs.append(candidate)
58
+ pairs_id.append(n+1)
59
+ if len(pairs_id) < 2:
60
+ raise ValueError(f'ERROR: please specify at least two candidates: {pairs}')
61
+ vectors = model.get_embedding(pairs+[query])
62
+ vector_q = vectors.pop(-1)
63
+ sims = []
64
+ for v in vectors:
65
+ sims.append(cosine_similarity(v, vector_q))
66
+ output = sorted(list(zip(pairs_id, sims, pairs)), key=lambda _x: _x[1], reverse=True)
67
+ output = {f'candidate {n}: [{p[0]}, {p[1]}]': s for n, (i, s, p) in enumerate(output)}
68
+ return output
 
 
 
 
 
 
 
69
 
70
 
71
  demo = gr.Interface(