|
|
|
import gradio as gr |
|
import spacy |
|
from transformers import pipeline |
|
|
|
nlp = spacy.load('es_core_news_sm') |
|
text_generator = pipeline('text-generation', model='gpt2') |
|
|
|
pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X'] |
|
pos_tags_es = ['ADJ', 'PREP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'SUST', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERBO', 'X'] |
|
|
|
|
|
sentence_state = {'sentence': '', 'tagged_words': [], 'tagged_words_es': []} |
|
|
|
|
|
def generate_and_analyze(): |
|
result = text_generator('', max_length=10, do_sample=True, language='es')[0] |
|
sentence = result['generated_text'] |
|
doc = nlp(sentence) |
|
tagged_words = [(token.text, token.pos_) for token in doc] |
|
tagged_words_es = [(token.text, pos_tags_es[pos_tags.index(token.pos_)]) for token in doc] |
|
sentence_state['sentence'] = sentence |
|
sentence_state['tagged_words'] = tagged_words |
|
sentence_state['tagged_words_es'] = tagged_words_es |
|
return sentence, tagged_words, tagged_words_es |
|
|
|
|
|
generate_and_analyze() |
|
|
|
|
|
def game_flow(start_game, *args): |
|
if start_game == 'Empezar': |
|
sentence, _, _ = generate_and_analyze() |
|
return sentence, '', '', '' |
|
else: |
|
correct_answer = [tag for _, tag in sentence_state['tagged_words_es']] |
|
user_answer = list(args) |
|
if user_answer == correct_answer: |
|
return sentence_state['sentence'], ' '.join([f'{word} ({tag})' for word, tag in sentence_state['tagged_words_es']]), '隆Correcto!', '' |
|
else: |
|
correction = ' '.join([f'{word} es {tag}' for word, tag in sentence_state['tagged_words_es']]) |
|
return sentence_state['sentence'], ' '.join([f'{word} ({tag})' for word, tag in sentence_state['tagged_words_es']]), 'Incorrecto.', 'La respuesta correcta es: ' + correction |
|
|
|
iface = gr.Interface(fn=game_flow, |
|
inputs=[gr.inputs.Textbox(label='Escribe "Empezar" para iniciar el juego')] + |
|
[gr.inputs.Dropdown(choices=pos_tags_es, label=f'Funci贸n de la palabra {i+1}') for i in range(5)], |
|
outputs=[gr.outputs.Textbox(label='Frase generada'), |
|
gr.outputs.Textbox(label='Funciones de las palabras'), |
|
gr.outputs.Textbox(label='Evaluaci贸n'), |
|
gr.outputs.Textbox(label='Correcci贸n')]) |
|
iface.launch() |
|
|