Spaces:
Sleeping
Sleeping
import stanza | |
import gradio as gr | |
import os | |
import base64 | |
# Load local model directory | |
MODEL_DIR = "viv/UD_Greek-GUD" | |
# Initialize Stanza pipeline | |
nlp = stanza.Pipeline( | |
lang='el', | |
package='gud', | |
processors='tokenize,pos,lemma,depparse', | |
dir=MODEL_DIR, | |
use_gpu=False, | |
verbose=True | |
) | |
# Generate HTML table with POS and lemma annotations | |
def analyze_text_as_html(text): | |
doc = nlp(text) | |
rows = [] | |
for sent in doc.sentences: | |
for word in sent.words: | |
lemma_style = "font-weight:bold;" | |
pos_color = { | |
"NOUN": "background-color:lightblue;", | |
"VERB": "background-color:lightgreen;", | |
"ADJ": "background-color:lightcoral;", | |
"ADV": "background-color:khaki;" | |
}.get(word.upos, "") | |
row = f""" | |
<tr> | |
<td>{word.text}</td> | |
<td style="{lemma_style}">{word.lemma}</td> | |
<td style="{pos_color}">{word.upos}</td> | |
</tr> | |
""" | |
rows.append(row) | |
html_table = f""" | |
<table style='border-collapse: collapse; width: 100%;'> | |
<thead> | |
<tr> | |
<th>Λέξη</th> | |
<th>Λήμμα</th> | |
<th>Μέρος του λόγου</th> | |
</tr> | |
</thead> | |
<tbody> | |
{''.join(rows)} | |
</tbody> | |
</table> | |
""" | |
return html_table | |
# Generate dependency relations text | |
def show_dependencies(text, with_labels=True): | |
doc = nlp(text) | |
visuals = [] | |
for sentence in doc.sentences: | |
arrows = [] | |
for word in sentence.words: | |
head = "ROOT" if word.head == 0 else sentence.words[word.head - 1].text | |
if with_labels: | |
arrows.append(f"{word.text} → {head} ({word.deprel})") | |
else: | |
if word.head != 0: | |
arrows.append(f"{word.text} → {head}") | |
visuals.append("\n".join(arrows)) | |
return "\n\n".join(visuals) | |
# Gradio interface | |
with gr.Blocks() as demo: | |
gr.HTML(f""" | |
<div style='display: flex; align-items: center; gap: 20px; margin-bottom: 20px;'> | |
<h1 style='font-size: 28px; font-weight: bold; margin: 0;'>Demo Parse</h1> | |
</div> | |
""") | |
gr.Markdown("## Ανάλυση Κειμένου με το Μοντέλο GUD (Νέα Ελληνική)") | |
input_text = gr.Textbox(lines=4, label="Εισαγωγή Κειμένου") | |
with gr.Column(): | |
pos_output = gr.HTML(label="Ανάλυση (με χρώματα)") | |
dep_output = gr.Textbox(label="Σχέσεις Εξάρτησης (με ετικέτες)", lines=8) | |
simple_dep_output = gr.Textbox(label="Σχέσεις Εξάρτησης (χωρίς ετικέτες)", lines=8) | |
analyze_button = gr.Button("Ανάλυσε") | |
def process(text): | |
return ( | |
analyze_text_as_html(text), | |
show_dependencies(text, with_labels=True), | |
show_dependencies(text, with_labels=False), | |
) | |
analyze_button.click( | |
fn=process, | |
inputs=input_text, | |
outputs=[pos_output, dep_output, simple_dep_output] | |
) | |
demo.launch() | |