File size: 1,872 Bytes
c2fc6b8
a8bb7b0
b09df44
c2fc6b8
a8bb7b0
 
c2fc6b8
a8bb7b0
dbcf61e
 
6c338fd
856ec32
 
6c338fd
 
 
c2fc6b8
6c338fd
 
856ec32
 
6c338fd
a8bb7b0
6c338fd
 
 
 
 
b09df44
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
## 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']

# Store the sentence state globally
sentence_state = {'sentence': '', 'tagged_words': []}

# 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]
    sentence_state['sentence'] = sentence
    sentence_state['tagged_words'] = tagged_words
    return sentence, tagged_words

# 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']]
        user_answer = list(args)
        if user_answer == correct_answer:
            return sentence_state['sentence'], ' '.join(sentence_state['tagged_words']), '¡Correcto!'
        else:
            return sentence_state['sentence'], ' '.join(sentence_state['tagged_words']), 'Incorrecto. La respuesta correcta es: ' + ' '.join(correct_answer)

iface = gr.Interface(fn=game_flow, 
                     inputs=[gr.inputs.Textbox(label='Escribe "Empezar" para generar una frase')] + 
                            [gr.inputs.Dropdown(choices=pos_tags, label=f'Palabra {i+1}') for i in range(5)], 
                     outputs=[gr.outputs.Textbox(label='Frase'),
                              gr.outputs.Textbox(label='Palabras'),
                              gr.outputs.Textbox(label='Resultado')])
iface.launch()