Merlintxu commited on
Commit
26e934e
1 Parent(s): 6c338fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -7,9 +7,10 @@ nlp = spacy.load('es_core_news_sm')
7
  text_generator = pipeline('text-generation', model='gpt2')
8
 
9
  pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
 
10
 
11
  # Store the sentence state globally
12
- sentence_state = {'sentence': '', 'tagged_words': []}
13
 
14
  # Generate a sentence and analyze it
15
  def generate_and_analyze():
@@ -17,27 +18,31 @@ def generate_and_analyze():
17
  sentence = result['generated_text']
18
  doc = nlp(sentence)
19
  tagged_words = [(token.text, token.pos_) for token in doc]
 
20
  sentence_state['sentence'] = sentence
21
  sentence_state['tagged_words'] = tagged_words
22
- return sentence, tagged_words
 
23
 
24
  # The game logic
25
  def game_flow(start_game, *args):
26
  if start_game == 'Empezar':
27
- sentence, _ = generate_and_analyze()
28
- return sentence, '', ''
29
  else:
30
- correct_answer = [tag for _, tag in sentence_state['tagged_words']]
31
  user_answer = list(args)
32
  if user_answer == correct_answer:
33
- return sentence_state['sentence'], ' '.join(sentence_state['tagged_words']), '隆Correcto!'
34
  else:
35
- return sentence_state['sentence'], ' '.join(sentence_state['tagged_words']), 'Incorrecto. La respuesta correcta es: ' + ' '.join(correct_answer)
 
36
 
37
  iface = gr.Interface(fn=game_flow,
38
- inputs=[gr.inputs.Textbox(label='Escribe "Empezar" para generar una frase')] +
39
- [gr.inputs.Dropdown(choices=pos_tags, label=f'Palabra {i+1}') for i in range(5)],
40
- outputs=[gr.outputs.Textbox(label='Frase'),
41
- gr.outputs.Textbox(label='Palabras'),
42
- gr.outputs.Textbox(label='Resultado')])
 
43
  iface.launch()
 
7
  text_generator = pipeline('text-generation', model='gpt2')
8
 
9
  pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
10
+ pos_tags_es = ['ADJ', 'PREP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'SUST', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERBO', 'X']
11
 
12
  # Store the sentence state globally
13
+ sentence_state = {'sentence': '', 'tagged_words': [], 'tagged_words_es': []}
14
 
15
  # Generate a sentence and analyze it
16
  def generate_and_analyze():
 
18
  sentence = result['generated_text']
19
  doc = nlp(sentence)
20
  tagged_words = [(token.text, token.pos_) for token in doc]
21
+ tagged_words_es = [(token.text, pos_tags_es[pos_tags.index(token.pos_)]) for token in doc]
22
  sentence_state['sentence'] = sentence
23
  sentence_state['tagged_words'] = tagged_words
24
+ sentence_state['tagged_words_es'] = tagged_words_es
25
+ return sentence, tagged_words, tagged_words_es
26
 
27
  # The game logic
28
  def game_flow(start_game, *args):
29
  if start_game == 'Empezar':
30
+ sentence, _, _ = generate_and_analyze()
31
+ return sentence, '', '', ''
32
  else:
33
+ correct_answer = [tag for _, tag in sentence_state['tagged_words_es']]
34
  user_answer = list(args)
35
  if user_answer == correct_answer:
36
+ return sentence_state['sentence'], ' '.join(sentence_state['tagged_words_es']), '隆Correcto!', ''
37
  else:
38
+ correction = ' '.join([f'{word} es {tag}' for word, tag in sentence_state['tagged_words_es']])
39
+ return sentence_state['sentence'], ' '.join(sentence_state['tagged_words_es']), 'Incorrecto.', 'La respuesta correcta es: ' + correction
40
 
41
  iface = gr.Interface(fn=game_flow,
42
+ inputs=[gr.inputs.Textbox(label='Escribe "Empezar" para iniciar el juego')] +
43
+ [gr.inputs.Dropdown(choices=pos_tags_es, label=f'Funci贸n de la palabra {i+1}') for i in range(5)],
44
+ outputs=[gr.outputs.Textbox(label='Frase generada'),
45
+ gr.outputs.Textbox(label='Funciones de las palabras'),
46
+ gr.outputs.Textbox(label='Evaluaci贸n'),
47
+ gr.outputs.Textbox(label='Correcci贸n')])
48
  iface.launch()