Merlintxu commited on
Commit
76a01d9
1 Parent(s): 76b759d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -23
app.py CHANGED
@@ -1,30 +1,56 @@
1
- from flask import Flask, request, render_template
2
- from transformers import pipeline
 
3
 
4
- app = Flask(__name__)
 
5
 
6
- nlp = pipeline('sentiment-analysis')
7
-
8
- @app.route('/')
9
- def home():
10
- return render_template('index.html')
11
 
12
- @app.route('/predict',methods=['POST'])
13
- def predict():
14
- if request.method == 'POST':
15
- message = request.form['message']
16
- prediction = nlp(message)
17
- return render_template('index.html', prediction_text=prediction)
18
 
19
- if __name__ == "__main__":
20
- app.run(debug=True)
21
- from transformers import GPT3LMHeadModel, GPT2Tokenizer
 
22
 
23
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
24
- model = GPT3LMHeadModel.from_pretrained("gpt3")
 
 
 
 
 
 
 
 
25
 
26
- def get_response(prompt):
27
- inputs = tokenizer.encode(prompt, return_tensors="pt")
28
  outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
29
- response = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
30
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()