Spaces:
Sleeping
Sleeping
feat: 相似度计算demo
Browse files
app.py
CHANGED
@@ -1,15 +1,23 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
|
|
4 |
|
5 |
-
|
|
|
6 |
|
7 |
@spaces.GPU
|
8 |
-
def generate(
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
gr.Interface(
|
12 |
fn=generate,
|
13 |
-
inputs=gr.Text(),
|
14 |
-
outputs=gr.
|
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()
|