AlyxTeam commited on
Commit
be10d84
1 Parent(s): 8d6633c

feat: 相似度计算demo

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -1,15 +1,23 @@
1
  import spaces
2
  import gradio as gr
3
- from transformers import pipeline
 
4
 
5
- pipe = pipeline("feature-extraction", model="jinaai/jina-embeddings-v2-base-code", trust_remote_code=True)
 
6
 
7
  @spaces.GPU
8
- def generate(prompt):
9
- return pipe(prompt)[0].numpy().mean(axis=0)
 
 
 
 
 
 
10
 
11
  gr.Interface(
12
  fn=generate,
13
- inputs=gr.Text(),
14
- outputs=gr.Gallery(),
15
  ).launch()
 
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
+ model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-code', trust_remote_code=True)
8
 
9
  @spaces.GPU
10
+ def generate(input1, input2):
11
+ embeddings = model.encode(
12
+ [
13
+ input1,
14
+ input2,
15
+ ]
16
+ )
17
+ return str(cos_sim(embeddings[0], embeddings[1]))
18
 
19
  gr.Interface(
20
  fn=generate,
21
+ inputs=[gr.Text(), gr.Text()],
22
+ outputs=gr.Text(),
23
  ).launch()