Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import re
|
4 |
+
|
5 |
+
from flair.models import SequenceTagger
|
6 |
+
from flair.data import Sentence
|
7 |
+
|
8 |
+
flair_ner_model_path = "flair_model"
|
9 |
+
assert os.path.exists(flair_ner_model_path)
|
10 |
+
loaded_model: SequenceTagger = SequenceTagger.load(os.path.join(flair_ner_model_path ,"best-model.pt"))
|
11 |
+
|
12 |
+
def one_item_process(r, loaded_model):
|
13 |
+
#assert type(r) == type(pd.Series())
|
14 |
+
zh = r["question"]
|
15 |
+
zh = zh.replace(" ", "").strip()
|
16 |
+
sentence = Sentence(" ".join(list(zh)))
|
17 |
+
loaded_model.predict(sentence)
|
18 |
+
sentence_str = str(sentence)
|
19 |
+
ask_spans = re.findall(r'\["(.+?)"/ASK\]', sentence_str)
|
20 |
+
sentence = re.findall(r'Sentence: "(.+?)"', sentence_str)
|
21 |
+
if ask_spans:
|
22 |
+
ask_spans = ask_spans[0]
|
23 |
+
else:
|
24 |
+
ask_spans = ""
|
25 |
+
if sentence:
|
26 |
+
sentence = sentence[0]
|
27 |
+
else:
|
28 |
+
sentence = ""
|
29 |
+
ask_spans, sentence = map(lambda x: x.replace(" ", "").strip(), [ask_spans, sentence])
|
30 |
+
return ask_spans, sentence
|
31 |
+
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
example_sample = [
|
35 |
+
"宁波在哪个省份?",
|
36 |
+
"美国的通货是什么?",
|
37 |
+
]
|
38 |
+
|
39 |
+
def demo_func(question):
|
40 |
+
assert type(question) == type("")
|
41 |
+
ask_spans, sentence = one_item_process(
|
42 |
+
{"question": question},
|
43 |
+
loaded_model
|
44 |
+
)
|
45 |
+
return {
|
46 |
+
"Question words": ask_spans
|
47 |
+
}
|
48 |
+
|
49 |
+
|
50 |
+
demo = gr.Interface(
|
51 |
+
fn=demo_func,
|
52 |
+
inputs="text",
|
53 |
+
outputs="json",
|
54 |
+
title=f"Chinese Question Words extractor 🐱 demonstration",
|
55 |
+
examples=example_sample if example_sample else None,
|
56 |
+
cache_examples = False
|
57 |
+
)
|
58 |
+
|
59 |
+
demo.launch(server_name=None, server_port=None)
|