AlyxTeam commited on
Commit
026c201
1 Parent(s): 6450f47

feat: 部署对比模型

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -4,7 +4,9 @@ from transformers import AutoModel
4
  from numpy.linalg import norm
5
 
6
  cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
7
- model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-code', trust_remote_code=True)
 
 
8
 
9
  @spaces.GPU
10
  def generate(input1, input2):
@@ -12,13 +14,25 @@ def generate(input1, input2):
12
  input1 = "How do I access the index while iterating over a sequence with a for loop?"
13
  if len(input2) < 1:
14
  input2 = "# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)"
15
- embeddings = model.encode(
16
  [
17
  input1,
18
  input2,
19
  ]
20
  )
21
- return str(cos_sim(embeddings[0], embeddings[1]))
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  gr.Interface(
24
  fn=generate,
@@ -26,5 +40,9 @@ gr.Interface(
26
  gr.Text(label="input1", placeholder="How do I access the index while iterating over a sequence with a for loop?"),
27
  gr.Text(label="input2", placeholder="# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)"),
28
  ],
29
- outputs=gr.Text(),
 
 
 
 
30
  ).launch()
 
4
  from numpy.linalg import norm
5
 
6
  cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
7
+ model1 = AutoModel.from_pretrained("jinaai/jina-embeddings-v2-base-code", trust_remote_code=True)
8
+ model2 = AutoModel.from_pretrained("jinaai/jina-embeddings-v2-base-en", trust_remote_code=True)
9
+ model3 = AutoModel.from_pretrained("jinaai/jina-embeddings-v2-base-zh", trust_remote_code=True)
10
 
11
  @spaces.GPU
12
  def generate(input1, input2):
 
14
  input1 = "How do I access the index while iterating over a sequence with a for loop?"
15
  if len(input2) < 1:
16
  input2 = "# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)"
17
+ embeddings1 = model1.encode(
18
  [
19
  input1,
20
  input2,
21
  ]
22
  )
23
+ embeddings2 = model2.encode(
24
+ [
25
+ input1,
26
+ input2,
27
+ ]
28
+ )
29
+ embeddings3 = model3.encode(
30
+ [
31
+ input1,
32
+ input2,
33
+ ]
34
+ )
35
+ return cos_sim(embeddings1[0], embeddings1[1]), cos_sim(embeddings2[0], embeddings2[1]), cos_sim(embeddings3[0], embeddings3[1])
36
 
37
  gr.Interface(
38
  fn=generate,
 
40
  gr.Text(label="input1", placeholder="How do I access the index while iterating over a sequence with a for loop?"),
41
  gr.Text(label="input2", placeholder="# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)"),
42
  ],
43
+ outputs=[
44
+ gr.Text(label="jina-embeddings-v2-base-code"),
45
+ gr.Text(label="jina-embeddings-v2-base-en"),
46
+ gr.Text(label="jina-embeddings-v2-base-zh"),
47
+ ],
48
  ).launch()