Spaces:
Runtime error
Runtime error
acumplid
commited on
Commit
·
f7a2a3f
1
Parent(s):
28d06a9
Replaced Interface by blocks demo
Browse files
app.py
CHANGED
@@ -2,23 +2,106 @@ import gradio as gr
|
|
2 |
from AinaTheme import AinaGradioTheme
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
ner_pipeline = pipeline("token-classification", model="projecte-aina/multiner_ceil",aggregation_strategy="first")
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from AinaTheme import AinaGradioTheme
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
+
from gradio.components import Textbox, Button, HighlightedText, Markdown
|
6 |
+
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
MAX_INPUT_CHARACTERS= int(os.environ.get("MAX_INPUT_CHARACTERS", default=1000))
|
12 |
|
13 |
ner_pipeline = pipeline("token-classification", model="projecte-aina/multiner_ceil",aggregation_strategy="first")
|
14 |
|
15 |
+
def submit_input(text):
|
16 |
+
if text.strip() == "":
|
17 |
+
gr.Warning('Not possible to inference an empty input')
|
18 |
+
return None
|
19 |
+
|
20 |
+
model_output = ner_pipeline(text)
|
21 |
+
|
22 |
+
if model_output is None:
|
23 |
+
gr.Warning('Inference endpoint is not available right now. Please try again later.')
|
24 |
+
|
25 |
+
return {"text": text, "entities": model_output}
|
26 |
+
|
27 |
+
def check_max_characters(text, max_char):
|
28 |
+
if len(text.strip()) > int(max_char):
|
29 |
+
return gr.update(interactive = True), gr.update(interactive = False)
|
30 |
+
return gr.update(interactive = True), gr.update(interactive = True)
|
31 |
+
|
32 |
+
def clear():
|
33 |
+
return (
|
34 |
+
None,
|
35 |
+
None,
|
36 |
+
)
|
37 |
+
|
38 |
+
with gr.Blocks(**AinaGradioTheme().get_kwargs()) as demo:
|
39 |
+
with gr.Row():
|
40 |
+
with gr.Column():
|
41 |
+
gr.Markdown(
|
42 |
+
""" Multiner is a Named Entity Recognition (NER) model for the Catalan language fine-tuned from the [BERTa] model, a RoBERTa base model pre-trained on a medium-size corpus collected from publicly available corpora and crawlers (check the BERTa model card for more details).
|
43 |
+
It has been trained with a dataset (CEIL: Catalan Entity Identification and Linking ) that contains 9 main types and 52 subtypes on all kinds of short texts, with almost 59K documents.
|
44 |
+
This result has been driven and funded by the Government of Catalonia through the [Aina](https://projecteaina.cat/)
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
|
48 |
+
with gr.Row( equal_height=False):
|
49 |
+
with gr.Column(variant="panel"):
|
50 |
+
placeholder_max_characters = Textbox(
|
51 |
+
visible=False,
|
52 |
+
interactive=False,
|
53 |
+
value= MAX_INPUT_CHARACTERS
|
54 |
+
)
|
55 |
+
input_ = Textbox(
|
56 |
+
lines=8,
|
57 |
+
label="Input",
|
58 |
+
placeholder="e.g. Enter sentence here"
|
59 |
+
)
|
60 |
+
with gr.Row(variant="panel", equal_height=True):
|
61 |
+
gr.HTML("""<span id="countertext" style="display: flex; justify-content: start; color:#ef4444; font-weight: bold;"></span>""")
|
62 |
+
gr.HTML(f"""<span id="counter" style="display: flex; justify-content: end;"> <span id="inputlenght">0</span> / {MAX_INPUT_CHARACTERS}</span>""")
|
63 |
+
|
64 |
+
|
65 |
+
with gr.Column(variant="panel"):
|
66 |
+
output = HighlightedText(
|
67 |
+
container=True,
|
68 |
+
label="Output",
|
69 |
+
)
|
70 |
+
with gr.Row(variant="panel"):
|
71 |
+
clear_btn = Button(
|
72 |
+
"Clear",
|
73 |
+
)
|
74 |
+
submit_btn = Button(
|
75 |
+
"Submit",
|
76 |
+
variant="primary",
|
77 |
+
)
|
78 |
+
|
79 |
+
|
80 |
+
input_.change(
|
81 |
+
fn=check_max_characters,
|
82 |
+
inputs=[input_, placeholder_max_characters],
|
83 |
+
outputs=[clear_btn, submit_btn]
|
84 |
+
)
|
85 |
+
|
86 |
+
input_.change(fn=None, inputs=[input_, placeholder_max_characters], _js="""(i, m) => {
|
87 |
+
document.getElementById('countertext').textContent = i.length > m && 'Max length ' + m + ' characters. ' || ''
|
88 |
+
document.getElementById('inputlenght').textContent = i.length + ' '
|
89 |
+
document.getElementById('inputlenght').style.color = (i.length > m) ? "#ef4444" : "";
|
90 |
+
}""")
|
91 |
+
|
92 |
+
clear_btn.click(
|
93 |
+
fn=clear,
|
94 |
+
inputs=[],
|
95 |
+
outputs=[input_, output],
|
96 |
+
queue=False
|
97 |
+
)
|
98 |
+
|
99 |
+
submit_btn.click(
|
100 |
+
fn=submit_input,
|
101 |
+
inputs=[input_],
|
102 |
+
outputs=[output]
|
103 |
+
)
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
demo.queue(concurrency_count=1, api_open=False)
|
107 |
+
demo.launch(show_api=False)
|