Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from paddlenlp import Taskflow
|
5 |
+
|
6 |
+
model_name = "uie-base-en"
|
7 |
+
|
8 |
+
task_instance = Taskflow("information_extraction", model=model_name)
|
9 |
+
|
10 |
+
|
11 |
+
def analyze(text, schema_name):
|
12 |
+
|
13 |
+
|
14 |
+
schema = schemas.get(schema_name.split()[0])
|
15 |
+
if schema is None:
|
16 |
+
return {"error": "Bad schema"}
|
17 |
+
|
18 |
+
task_instance.set_schema(schema)
|
19 |
+
# task_instance.set_argument(argument)
|
20 |
+
|
21 |
+
try:
|
22 |
+
result = task_instance(text)
|
23 |
+
except Exception as e:
|
24 |
+
return {"error": e}
|
25 |
+
|
26 |
+
return result
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
schema_choices = [
|
31 |
+
"Entities (people, organizations, locations, date)",
|
32 |
+
"Relation1 (person, work for)",
|
33 |
+
"Event1 (start job, [date, company, employee])",
|
34 |
+
|
35 |
+
# "Event2 ("
|
36 |
+
]
|
37 |
+
|
38 |
+
examples = [
|
39 |
+
["Nicholas is an engineer working at a company called Hugging Face.", schema_choices[0]],
|
40 |
+
["Nicholas is an engineer working at a company called Hugging Face.", schema_choices[1]],
|
41 |
+
["Chris Wigley was appointed CEO of Genomics England, starting in October 2019.", schema_choices[2]],
|
42 |
+
["Chris Wigley was appointed CEO of Genomics England, starting in October 2019.", schema_choices[1]],
|
43 |
+
["Wigley is a former McKinsey executive known for applying machine learning and artificial intelligence technology", schema_choices[0]],
|
44 |
+
]
|
45 |
+
|
46 |
+
schemas = {
|
47 |
+
# Entity Extraction
|
48 |
+
"Entities": ['Person', 'Organization', 'Location'],
|
49 |
+
"Relation1": [{"Person":["work for"]}],
|
50 |
+
"Event1": [{'start-position': ['date', "company", "employee"]}],
|
51 |
+
}
|
52 |
+
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("# Universal Information Extraction (UIE) \n\n \
|
55 |
+
For Genomics England Workshop \
|
56 |
+
")
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
with gr.Column():
|
60 |
+
schema = gr.Dropdown(choices=schema_choices, value=schema_choices[0])
|
61 |
+
# schema_setup = gr.Textbox(label="Schema setup", placeholder=json.dumps(schemas[schema]))
|
62 |
+
text_input = gr.Textbox(label="Text Input", placeholder=examples[0])
|
63 |
+
example_textbox1 = gr.Textbox(visible=False)
|
64 |
+
example_textbox2 = gr.Textbox(visible=False)
|
65 |
+
gr.Examples(
|
66 |
+
examples=examples,
|
67 |
+
inputs=[example_textbox1, example_textbox2]
|
68 |
+
)
|
69 |
+
with gr.Column():
|
70 |
+
btn = gr.Button("Run model")
|
71 |
+
json_output = gr.JSON(label="JSON Output")
|
72 |
+
|
73 |
+
|
74 |
+
example_textbox1.change(fn=lambda x, y: (x, y), inputs=[example_textbox1, example_textbox2], outputs=[text_input, schema])
|
75 |
+
btn.click(fn=analyze, inputs=[text_input, schema], outputs=json_output)
|
76 |
+
|
77 |
+
demo.launch(debug=True, )
|