AlyxTeam commited on
Commit
21edaab
·
1 Parent(s): 43938c1

feat: 添加对比模型

Browse files
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -1,38 +1,65 @@
1
  import spaces
2
  import gradio as gr
3
- 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
  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):
13
  if len(input1) < 1:
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,
@@ -41,8 +68,10 @@ gr.Interface(
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()
 
1
  import spaces
2
  import gradio as gr
 
3
  from numpy.linalg import norm
4
+ from transformers import AutoModel
5
+ from sentence_transformers import SentenceTransformer
6
 
7
  cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
8
+
9
  model1 = AutoModel.from_pretrained("jinaai/jina-embeddings-v2-base-code", trust_remote_code=True)
10
  model2 = AutoModel.from_pretrained("jinaai/jina-embeddings-v2-base-en", trust_remote_code=True)
11
  model3 = AutoModel.from_pretrained("jinaai/jina-embeddings-v2-base-zh", trust_remote_code=True)
12
+ model4 = SentenceTransformer("aspire/acge_text_embedding")
13
+ model5 = SentenceTransformer("intfloat/multilingual-e5-large")
14
 
15
  @spaces.GPU
16
  def generate(input1, input2):
17
  if len(input1) < 1:
18
  input1 = "How do I access the index while iterating over a sequence with a for loop?"
19
  if len(input2) < 1:
20
+ input2 = "# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)"
21
+
22
  embeddings1 = model1.encode(
23
  [
24
  input1,
25
  input2,
26
  ]
27
  )
28
+ score1 = cos_sim(embeddings1[0], embeddings1[1])
29
+
30
  embeddings2 = model2.encode(
31
  [
32
  input1,
33
  input2,
34
  ]
35
  )
36
+ score2 = cos_sim(embeddings2[0], embeddings2[1])
37
+
38
  embeddings3 = model3.encode(
39
  [
40
  input1,
41
  input2,
42
  ]
43
  )
44
+ score3 = cos_sim(embeddings3[0], embeddings3[1])
45
+
46
+ embeddings4 = model4.encode(
47
+ [
48
+ input1,
49
+ input2,
50
+ ]
51
+ )
52
+ score4 = cos_sim(embeddings4[0], embeddings4[1])
53
+
54
+ embeddings5 = model5.encode(
55
+ [
56
+ input1,
57
+ input2,
58
+ ]
59
+ )
60
+ score5 = cos_sim(embeddings5[0], embeddings5[1])
61
+
62
+ return score1, score2, score3, score4, score5
63
 
64
  gr.Interface(
65
  fn=generate,
 
68
  gr.Text(label="input2", placeholder="# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)"),
69
  ],
70
  outputs=[
71
+ gr.Text(label="jinaai/jina-embeddings-v2-base-code"),
72
+ gr.Text(label="jinaai/jina-embeddings-v2-base-en"),
73
+ gr.Text(label="jinaai/jina-embeddings-v2-base-zh"),
74
+ gr.Text(label="aspire/acge_text_embedding"),
75
+ gr.Text(label="intfloat/multilingual-e5-large"),
76
  ],
77
  ).launch()