Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,56 @@
|
|
1 |
-
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
nlp =
|
7 |
-
|
8 |
-
|
9 |
-
def home():
|
10 |
-
return render_template('index.html')
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
def
|
27 |
-
inputs = tokenizer.encode(
|
28 |
outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
29 |
-
|
30 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
7 |
|
8 |
+
nlp = spacy.load('es_core_news_sm')
|
9 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
10 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
|
|
|
|
11 |
|
12 |
+
pos_map = {
|
13 |
+
'sustantivo': 'NOUN',
|
14 |
+
'verbo': 'VERB',
|
15 |
+
'adjetivo': 'ADJ',
|
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[user_pos.lower()]
|
27 |
+
for word, pos in correct_answers:
|
28 |
+
if word == user_word:
|
29 |
+
if pos.lower() == user_pos.lower():
|
30 |
+
return True, f'¡Correcto! "{user_word}" es un {user_pos}.'
|
31 |
+
else:
|
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)
|
45 |
+
return message, hint
|
46 |
+
else:
|
47 |
+
return 'Por favor, introduce una frase, una palabra y selecciona una función gramatical válida (sustantivo, verbo, adjetivo, artículo).', ''
|
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()
|