Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from gradio import Interface
|
3 |
-
from gradio.components import Textbox, Dropdown
|
4 |
|
5 |
import spacy
|
6 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
@@ -16,14 +15,14 @@ pos_map = {
|
|
16 |
'artículo': 'DET'
|
17 |
}
|
18 |
|
19 |
-
def identify_pos(sentence):
|
20 |
doc = nlp(sentence)
|
21 |
pos_tags = [(token.text, token.pos_) for token in doc]
|
22 |
return pos_tags
|
23 |
|
24 |
-
def game_logic(sentence, user_word, user_pos):
|
25 |
correct_answers = identify_pos(sentence)
|
26 |
-
user_pos = pos_map
|
27 |
for word, pos in correct_answers:
|
28 |
if word == user_word:
|
29 |
if pos.lower() == user_pos.lower():
|
@@ -32,13 +31,13 @@ def game_logic(sentence, user_word, user_pos):
|
|
32 |
return False, f'Incorrecto. "{user_word}" no es un {user_pos}, es un {pos}.'
|
33 |
return False, f'La palabra "{user_word}" no se encuentra en la frase.'
|
34 |
|
35 |
-
def generate_hint(sentence, user_word, user_pos):
|
36 |
inputs = tokenizer.encode(f'Give a hint about the {user_pos} in the sentence "{sentence}"', return_tensors='pt')
|
37 |
outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
38 |
hint = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
return hint
|
40 |
|
41 |
-
def main(sentence, user_word, user_pos):
|
42 |
if sentence and user_word and user_pos and user_pos != 'Selecciona una función gramatical...':
|
43 |
correct, message = game_logic(sentence, user_word, user_pos)
|
44 |
hint = generate_hint(sentence, user_word, user_pos)
|
@@ -48,9 +47,9 @@ def main(sentence, user_word, user_pos):
|
|
48 |
|
49 |
iface = Interface(fn=main,
|
50 |
inputs=[
|
51 |
-
Textbox(lines=2, placeholder='Introduce una frase aquí...'),
|
52 |
-
Textbox(lines=1, placeholder='Introduce una palabra aquí...'),
|
53 |
-
Dropdown(choices=['Selecciona una función gramatical...', 'sustantivo', 'verbo', 'adjetivo', 'artículo'])
|
54 |
],
|
55 |
-
outputs=[Textbox(), Textbox()])
|
56 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio import Interface, inputs, outputs
|
|
|
3 |
|
4 |
import spacy
|
5 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
|
15 |
'artículo': 'DET'
|
16 |
}
|
17 |
|
18 |
+
def identify_pos(sentence: str):
|
19 |
doc = nlp(sentence)
|
20 |
pos_tags = [(token.text, token.pos_) for token in doc]
|
21 |
return pos_tags
|
22 |
|
23 |
+
def game_logic(sentence: str, user_word: str, user_pos: str):
|
24 |
correct_answers = identify_pos(sentence)
|
25 |
+
user_pos = pos_map.get(user_pos.lower(), '')
|
26 |
for word, pos in correct_answers:
|
27 |
if word == user_word:
|
28 |
if pos.lower() == user_pos.lower():
|
|
|
31 |
return False, f'Incorrecto. "{user_word}" no es un {user_pos}, es un {pos}.'
|
32 |
return False, f'La palabra "{user_word}" no se encuentra en la frase.'
|
33 |
|
34 |
+
def generate_hint(sentence: str, user_word: str, user_pos: str):
|
35 |
inputs = tokenizer.encode(f'Give a hint about the {user_pos} in the sentence "{sentence}"', return_tensors='pt')
|
36 |
outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
37 |
hint = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
return hint
|
39 |
|
40 |
+
def main(sentence: str, user_word: str, user_pos: str):
|
41 |
if sentence and user_word and user_pos and user_pos != 'Selecciona una función gramatical...':
|
42 |
correct, message = game_logic(sentence, user_word, user_pos)
|
43 |
hint = generate_hint(sentence, user_word, user_pos)
|
|
|
47 |
|
48 |
iface = Interface(fn=main,
|
49 |
inputs=[
|
50 |
+
inputs.Textbox(lines=2, placeholder='Introduce una frase aquí...'),
|
51 |
+
inputs.Textbox(lines=1, placeholder='Introduce una palabra aquí...'),
|
52 |
+
inputs.Dropdown(choices=['Selecciona una función gramatical...', 'sustantivo', 'verbo', 'adjetivo', 'artículo'])
|
53 |
],
|
54 |
+
outputs=[outputs.Textbox(), outputs.Textbox()])
|
55 |
iface.launch()
|