Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,27 @@
|
|
1 |
## app.py ##
|
2 |
import gradio as gr
|
3 |
import spacy
|
|
|
4 |
from transformers import pipeline
|
5 |
|
6 |
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 |
sentence_state = {'sentence': '', 'tagged_words': []}
|
12 |
|
13 |
def generate_sentence():
|
14 |
-
result = text_generator('', max_length=10)[0]
|
15 |
sentence = result['generated_text']
|
16 |
tagged_words = analyze_sentence(sentence)
|
17 |
sentence_state['sentence'] = sentence
|
18 |
sentence_state['tagged_words'] = tagged_words
|
19 |
-
return sentence,
|
20 |
|
21 |
def analyze_sentence(sentence):
|
22 |
doc = nlp(sentence)
|
@@ -26,20 +31,23 @@ def check_answer(*args):
|
|
26 |
correct_answer = [tag for word, tag in sentence_state['tagged_words']]
|
27 |
user_answer = list(args)
|
28 |
if user_answer == correct_answer:
|
29 |
-
return 'Correcto!'
|
30 |
else:
|
31 |
-
return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer)
|
|
|
|
|
|
|
32 |
|
33 |
def game_flow(start_game):
|
34 |
-
if start_game == '
|
35 |
sentence, words = generate_sentence()
|
36 |
-
answer = check_answer(*gr.inputs)
|
37 |
-
return sentence, words, answer
|
38 |
|
39 |
iface = gr.Interface(fn=game_flow,
|
40 |
-
inputs=[gr.inputs.Textbox(label='
|
41 |
-
[gr.inputs.Dropdown(choices=
|
42 |
-
outputs=[gr.outputs.Textbox(label='
|
43 |
-
gr.outputs.Textbox(label='
|
44 |
-
gr.outputs.Textbox(label='
|
45 |
-
iface
|
|
|
1 |
## app.py ##
|
2 |
import gradio as gr
|
3 |
import spacy
|
4 |
+
import random
|
5 |
from transformers import pipeline
|
6 |
|
7 |
nlp = spacy.load('es_core_news_sm')
|
8 |
text_generator = pipeline('text-generation', model='gpt2')
|
9 |
|
10 |
pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
|
11 |
+
pos_tags_es = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOMBRE', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNT', 'SCONJ', 'SYM', 'VERBO', 'X'] # Spanish version
|
12 |
+
pos_explanation = { # Add explanations for each POS tag
|
13 |
+
# Add your own explanations here
|
14 |
+
}
|
15 |
|
16 |
sentence_state = {'sentence': '', 'tagged_words': []}
|
17 |
|
18 |
def generate_sentence():
|
19 |
+
result = text_generator('Erase una vez', max_length=10)[0]
|
20 |
sentence = result['generated_text']
|
21 |
tagged_words = analyze_sentence(sentence)
|
22 |
sentence_state['sentence'] = sentence
|
23 |
sentence_state['tagged_words'] = tagged_words
|
24 |
+
return sentence, random.sample(tagged_words, 5) # Only select 5 words to classify
|
25 |
|
26 |
def analyze_sentence(sentence):
|
27 |
doc = nlp(sentence)
|
|
|
31 |
correct_answer = [tag for word, tag in sentence_state['tagged_words']]
|
32 |
user_answer = list(args)
|
33 |
if user_answer == correct_answer:
|
34 |
+
return '¡Correcto!'
|
35 |
else:
|
36 |
+
return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer) + '. ' + explain(correct_answer)
|
37 |
+
|
38 |
+
def explain(tags):
|
39 |
+
return ' '.join(pos_explanation[tag] for tag in tags)
|
40 |
|
41 |
def game_flow(start_game):
|
42 |
+
if start_game == 'Iniciar':
|
43 |
sentence, words = generate_sentence()
|
44 |
+
answer = check_answer(*gr.inputs[1:]) # Skip the first input
|
45 |
+
return sentence, [word for word, _ in words], answer
|
46 |
|
47 |
iface = gr.Interface(fn=game_flow,
|
48 |
+
inputs=[gr.inputs.Textbox(label='Escribe "Iniciar" para generar una frase')] +
|
49 |
+
[gr.inputs.Dropdown(choices=pos_tags_es, label=f'Palabra {i+1}') for i in range(5)], # Only 5 words to classify
|
50 |
+
outputs=[gr.outputs.Textbox(label='Frase'),
|
51 |
+
gr.outputs.Textbox(label='Palabras'),
|
52 |
+
gr.outputs.Textbox(label='Resultado')])
|
53 |
+
iface
|