Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,44 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import spacy
|
3 |
-
from transformers import
|
4 |
|
5 |
nlp = spacy.load('es_core_news_sm')
|
6 |
-
|
7 |
-
# Load pre-trained model tokenizer (vocabulary)
|
8 |
-
tokenizer = GPT2Tokenizer.from_pretrained('datificate/gpt-2-small-spanish')
|
9 |
-
|
10 |
-
# Load pre-trained model (weights)
|
11 |
-
model = GPT2LMHeadModel.from_pretrained('datificate/gpt-2-small-spanish')
|
12 |
|
13 |
pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
|
14 |
|
15 |
-
|
16 |
-
tagged_words = []
|
17 |
|
18 |
def generate_sentence():
|
19 |
-
|
20 |
-
|
21 |
-
input_ids = tokenizer.encode('', return_tensors='pt')
|
22 |
-
output = model.generate(input_ids, max_length=50)
|
23 |
-
sentence = tokenizer.decode(output[0], skip_special_tokens=True)
|
24 |
tagged_words = analyze_sentence(sentence)
|
|
|
|
|
25 |
return sentence, [word for word, _ in tagged_words]
|
26 |
|
27 |
def analyze_sentence(sentence):
|
28 |
doc = nlp(sentence)
|
29 |
-
|
30 |
-
return tagged_words
|
31 |
|
32 |
def check_answer(*args):
|
33 |
-
correct_answer = [tag for word, tag in tagged_words]
|
34 |
user_answer = list(args)
|
35 |
if user_answer == correct_answer:
|
36 |
return 'Correcto!'
|
37 |
else:
|
38 |
return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer)
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
iface.launch()
|
|
|
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('')[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, [word for word, _ in tagged_words]
|
20 |
|
21 |
def analyze_sentence(sentence):
|
22 |
doc = nlp(sentence)
|
23 |
+
return [(token.text, token.pos_) for token in doc]
|
|
|
24 |
|
25 |
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():
|
34 |
+
sentence, words = generate_sentence()
|
35 |
+
answer = check_answer(*gr.inputs)
|
36 |
+
return sentence, words, answer
|
37 |
+
|
38 |
+
iface = gr.Interface(fn=game_flow,
|
39 |
+
inputs=[gr.inputs.Button(label='Generate Sentence')] +
|
40 |
+
[gr.inputs.Dropdown(choices=pos_tags, label=f'Word {i+1}') for i in range(len(sentence_state['tagged_words']))],
|
41 |
+
outputs=[gr.outputs.Textbox(label='Sentence'),
|
42 |
+
gr.outputs.Textbox(label='Words'),
|
43 |
+
gr.outputs.Textbox(label='Result')])
|
44 |
iface.launch()
|