Spaces:
Running
Running
Commit
•
d7acc86
1
Parent(s):
29f6c7f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
@author:XuMing(xuming624@qq.com)
|
4 |
+
@description: text similarity example, fine-tuned by CoSENT model
|
5 |
+
"""
|
6 |
+
import gradio as gr
|
7 |
+
from similarities import Similarity
|
8 |
+
|
9 |
+
# 中文句向量模型
|
10 |
+
sim_model = Similarity(model_name_or_path='shibing624/text2vec-base-chinese')
|
11 |
+
|
12 |
+
|
13 |
+
def load_file(path):
|
14 |
+
with open(path, 'r', encoding='utf-8') as f:
|
15 |
+
return f.read().split('\n')
|
16 |
+
|
17 |
+
|
18 |
+
sim_model.add_corpus(load_file('corpus.txt'))
|
19 |
+
|
20 |
+
|
21 |
+
def ai_text(query):
|
22 |
+
res = sim_model.most_similar(queries=query, topn=5)
|
23 |
+
print(res)
|
24 |
+
for q_id, c in res.items():
|
25 |
+
print('query:', query)
|
26 |
+
print("search top 5:")
|
27 |
+
for corpus_id, s in c.items():
|
28 |
+
print(f'\t{sim_model.corpus[corpus_id]}: {s:.4f}')
|
29 |
+
res_show = '\n'.join(['search top5:'] + [f'text: {sim_model.corpus[corpus_id]} score: {s:.4f}' for corpus_id, s in
|
30 |
+
list(res.values())[0].items()])
|
31 |
+
return res_show
|
32 |
+
|
33 |
+
|
34 |
+
if __name__ == '__main__':
|
35 |
+
examples = [
|
36 |
+
['星巴克被嘲笑了'],
|
37 |
+
['西班牙失业率超过50%'],
|
38 |
+
['她在看书'],
|
39 |
+
['一个人弹琴'],
|
40 |
+
]
|
41 |
+
input = gr.inputs.Textbox(lines=2, placeholder="Enter Query")
|
42 |
+
output_text = gr.outputs.Textbox()
|
43 |
+
gr.Interface(ai_text,
|
44 |
+
inputs=[input],
|
45 |
+
outputs=[output_text],
|
46 |
+
theme="grass",
|
47 |
+
title="Chinese Text Semantic Search Model",
|
48 |
+
description="Copy or input Chinese text here. Submit and the machine will find the most similarity texts.",
|
49 |
+
article="Link to <a href='https://github.com/shibing624/similarities' style='color:blue;' target='_blank\'>Github REPO</a>",
|
50 |
+
examples=examples
|
51 |
+
).launch()
|