File size: 2,561 Bytes
c2fc6b8 a8bb7b0 b09df44 c2fc6b8 a8bb7b0 c2fc6b8 a8bb7b0 dbcf61e 26e934e dbcf61e 6c338fd 26e934e 856ec32 6c338fd c2fc6b8 6c338fd 26e934e 856ec32 26e934e a8bb7b0 be336e7 6c338fd 26e934e b09df44 26e934e 6c338fd be336e7 6c338fd 26e934e be336e7 6c338fd 26e934e 6c338fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
## app.py ##
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']
# Store the sentence state globally
sentence_state = {'sentence': '', 'tagged_words': [], 'tagged_words_es': []}
# Generate a sentence and analyze it
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
# Initialize the game
generate_and_analyze()
# The game logic
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()
|