|
import gradio as gr |
|
import spacy |
|
from transformers import pipeline |
|
|
|
nlp = spacy.load('es_core_news_sm') |
|
text_generator = pipeline('text-generation', model='datificate/gpt-2-small-spanish') |
|
|
|
pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X'] |
|
|
|
sentence = "" |
|
tagged_words = [] |
|
|
|
def generate_sentence(): |
|
global sentence, tagged_words |
|
result = text_generator('', max_length=50)[0] |
|
sentence = result['generated_text'] |
|
tagged_words = analyze_sentence(sentence) |
|
return sentence, [word for word, _ in tagged_words] |
|
|
|
def analyze_sentence(sentence): |
|
doc = nlp(sentence) |
|
tagged_words = [(token.text, token.pos_) for token in doc] |
|
return tagged_words |
|
|
|
def check_answer(*args): |
|
correct_answer = [tag for word, tag in tagged_words] |
|
user_answer = list(args) |
|
if user_answer == correct_answer: |
|
return 'Correcto!' |
|
else: |
|
return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer) |
|
|
|
iface = gr.Interface(fn=generate_sentence, inputs='button', outputs=['textbox', 'dynamic']) |
|
iface.add_interface(fn=check_answer, inputs=gr.inputs.Dynamic(type="dropdown", choices=pos_tags, label='Word Tags'), outputs='textbox') |
|
iface.launch() |
|
|