Spaces:
Running
on
Zero
Running
on
Zero
Add Gradio demo
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gr_nlp_toolkit import Pipeline
|
3 |
+
|
4 |
+
# Author: Lefteris Loukas
|
5 |
+
# Date: August 2024
|
6 |
+
# Description: A Gradio interface for the Grεεk NLP Toolkit, which includes Greeklish to Greek conversion, dependency parsing, part-of-speech tagging, and named entity recognition.
|
7 |
+
# Point-of-Contact: http://nlp.cs.aueb.gr/
|
8 |
+
|
9 |
+
# Initialize Pipelines
|
10 |
+
nlp_pos_ner_dp_with_g2g = Pipeline("pos,ner,dp,g2g")
|
11 |
+
|
12 |
+
G2G_PLACEHOLDER = "e.g., H thessaloniki einai mia poli sti boreia ellada"
|
13 |
+
NER_PLACEHOLDER = "e.g., Η Αργεντινή κέρδισε το Παγκόσμιο Κύπελλο το 2022"
|
14 |
+
POS_PLACEHOLDER = "e.g., Μου αρέσει να διαβάζω τα post του Andrew Ng στο Twitter."
|
15 |
+
DP_PLACEHOLDER = "e.g., Προτιμώ την πρωινή πτήση από την Αθήνα στη Θεσσαλονίκη."
|
16 |
+
|
17 |
+
|
18 |
+
def greeklish_to_greek(text):
|
19 |
+
if not text:
|
20 |
+
text = G2G_PLACEHOLDER[5:]
|
21 |
+
|
22 |
+
doc = nlp_pos_ner_dp_with_g2g(text)
|
23 |
+
return " ".join([token.text for token in doc.tokens])
|
24 |
+
|
25 |
+
|
26 |
+
def process_text(text, task):
|
27 |
+
doc = nlp_pos_ner_dp_with_g2g(text)
|
28 |
+
task_mapping = {
|
29 |
+
"dp": lambda token: f"Text: {token.text}, Head: {token.head}, Deprel: {token.deprel}",
|
30 |
+
"pos": lambda token: f"Text: {token.text}, UPOS: {token.upos}, Feats: {token.feats}",
|
31 |
+
"ner": lambda token: f"Text: {token.text}, NER: {token.ner}",
|
32 |
+
}
|
33 |
+
return "\n".join([task_mapping[task](token) for token in doc.tokens])
|
34 |
+
|
35 |
+
|
36 |
+
def dependency_parsing(text):
|
37 |
+
if not text:
|
38 |
+
text = DP_PLACEHOLDER[5:]
|
39 |
+
return process_text(text, "dp")
|
40 |
+
|
41 |
+
|
42 |
+
def pos_tagging(text):
|
43 |
+
if not text:
|
44 |
+
text = POS_PLACEHOLDER[5:]
|
45 |
+
return process_text(text, "pos")
|
46 |
+
|
47 |
+
|
48 |
+
def named_entity_recognition(text):
|
49 |
+
if not text:
|
50 |
+
text = NER_PLACEHOLDER[5:]
|
51 |
+
|
52 |
+
return process_text(text, "ner")
|
53 |
+
|
54 |
+
|
55 |
+
# Define the Gradio interface
|
56 |
+
def create_demo():
|
57 |
+
theme = gr.themes.Soft()
|
58 |
+
with gr.Blocks(theme=theme) as demo:
|
59 |
+
gr.Markdown(
|
60 |
+
"""
|
61 |
+
# The Grεεk NLP Toolkit 🇬🇷
|
62 |
+
An open-source toolkit for state-of-the-art natural language processing in Greek.
|
63 |
+
|
64 |
+
## Key Features:
|
65 |
+
- Greeklish to Greek Conversion (G2G)
|
66 |
+
- Dependency Parsing (DP)
|
67 |
+
- Part-of-Speech (POS) Tagging
|
68 |
+
- Named Entity Recognition (NER)
|
69 |
+
"""
|
70 |
+
)
|
71 |
+
|
72 |
+
with gr.Tab("Greeklish to Greek"):
|
73 |
+
g2g_input = gr.Textbox(
|
74 |
+
label="Enter Greeklish text",
|
75 |
+
placeholder=G2G_PLACEHOLDER,
|
76 |
+
)
|
77 |
+
g2g_output = gr.Textbox(label="Greek text")
|
78 |
+
g2g_button = gr.Button("Submit")
|
79 |
+
g2g_button.click(greeklish_to_greek, inputs=g2g_input, outputs=g2g_output)
|
80 |
+
|
81 |
+
with gr.Tab("Dependency Parsing"):
|
82 |
+
dp_input = gr.Textbox(
|
83 |
+
label="Enter text",
|
84 |
+
placeholder=DP_PLACEHOLDER,
|
85 |
+
)
|
86 |
+
dp_output = gr.Textbox(label="Dependency Parsing annotations")
|
87 |
+
dp_button = gr.Button("Submit")
|
88 |
+
dp_button.click(dependency_parsing, inputs=dp_input, outputs=dp_output)
|
89 |
+
|
90 |
+
with gr.Tab("Part-of-Speech Tagging"):
|
91 |
+
pos_input = gr.Textbox(
|
92 |
+
label="Enter text",
|
93 |
+
placeholder=POS_PLACEHOLDER,
|
94 |
+
)
|
95 |
+
pos_output = gr.Textbox(label="POS Tagging annotations")
|
96 |
+
pos_button = gr.Button("Submit")
|
97 |
+
pos_button.click(pos_tagging, inputs=pos_input, outputs=pos_output)
|
98 |
+
|
99 |
+
with gr.Tab("Named Entity Recognition"):
|
100 |
+
ner_input = gr.Textbox(
|
101 |
+
label="Enter text",
|
102 |
+
placeholder=NER_PLACEHOLDER,
|
103 |
+
)
|
104 |
+
ner_output = gr.Textbox(label="NER annotations")
|
105 |
+
ner_button = gr.Button("Submit")
|
106 |
+
ner_button.click(
|
107 |
+
named_entity_recognition, inputs=ner_input, outputs=ner_output
|
108 |
+
)
|
109 |
+
|
110 |
+
gr.Markdown(
|
111 |
+
"""
|
112 |
+
|
113 |
+
## Installation
|
114 |
+
|
115 |
+
The Grεεk NLP toolkit is available on PyPI for Python 3.9+:
|
116 |
+
|
117 |
+
```sh
|
118 |
+
pip install gr-nlp-toolkit
|
119 |
+
```
|
120 |
+
|
121 |
+
Visit the [GitHub repository]("https://github.com/nlpaueb/gr-nlp-toolkit") for more information, such as documentation and full usage examples.
|
122 |
+
|
123 |
+
## About the Project
|
124 |
+
|
125 |
+
The Greek NLP Toolkit is the state-of-the-art natural language processing toolkit for modern Greek, [developed by the Natural Language Processing Group at the Athens University of Economics and Business](http://nlp.cs.aueb.gr/).
|
126 |
+
It supports named entity recognition, part-of-speech tagging, morphological tagging, dependency parsing,
|
127 |
+
and Greeklish to Greek conversion. This project is part of ongoing research aimed at advancing Greek language processing capabilities.
|
128 |
+
<br>
|
129 |
+
<br>
|
130 |
+
|
131 |
+
<div style="text-align: center;">
|
132 |
+
<a href="https://github.com/nlpaueb/gr-nlp-toolkit">
|
133 |
+
<img src="https://img.shields.io/badge/GitHub-Repository-181717?logo=github" alt="GitHub" style="display: block; margin: auto;">
|
134 |
+
</a>
|
135 |
+
<a href="https://github.com/nlpaueb/gr-nlp-toolkit">https://github.com/nlpaueb/gr-nlp-toolkit</a>
|
136 |
+
</div>
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
© 2024 The Greek NLP Toolkit. All rights reserved.
|
141 |
+
"""
|
142 |
+
)
|
143 |
+
|
144 |
+
return demo
|
145 |
+
|
146 |
+
|
147 |
+
# Launch the Gradio interface
|
148 |
+
if __name__ == "__main__":
|
149 |
+
demo = create_demo()
|
150 |
+
|
151 |
+
DEPLOY_TO_THE_PUBLIC_FLAG = False
|
152 |
+
demo.launch(share=DEPLOY_TO_THE_PUBLIC_FLAG)
|