GramAPP / app.py
Merlintxu's picture
Update app.py
0344802
raw
history blame
2.24 kB
## app.py ##
import gradio as gr
import spacy
import random
from transformers import pipeline
nlp = spacy.load('es_core_news_sm')
text_generator = pipeline('text-generation', model='gpt2')
pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
pos_tags_es = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOMBRE', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNT', 'SCONJ', 'SYM', 'VERBO', 'X'] # Spanish version
pos_explanation = { # Add explanations for each POS tag
# Add your own explanations here
}
sentence_state = {'sentence': '', 'tagged_words': []}
def generate_sentence():
result = text_generator('Erase una vez', max_length=10)[0]
sentence = result['generated_text']
tagged_words = analyze_sentence(sentence)
sentence_state['sentence'] = sentence
sentence_state['tagged_words'] = tagged_words
return sentence, random.sample(tagged_words, 5) # Only select 5 words to classify
def analyze_sentence(sentence):
doc = nlp(sentence)
return [(token.text, token.pos_) for token in doc]
def check_answer(*args):
correct_answer = [tag for word, tag in sentence_state['tagged_words']]
user_answer = list(args)
if user_answer == correct_answer:
return '¡Correcto!'
else:
return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer) + '. ' + explain(correct_answer)
def explain(tags):
return ' '.join(pos_explanation[tag] for tag in tags)
def game_flow(start_game):
if start_game == 'Iniciar':
sentence, words = generate_sentence()
answer = check_answer(*gr.inputs[1:]) # Skip the first input
return sentence, [word for word, _ in words], answer
iface = gr.Interface(fn=game_flow,
inputs=[gr.inputs.Textbox(label='Escribe "Iniciar" para generar una frase')] +
[gr.inputs.Dropdown(choices=pos_tags_es, label=f'Palabra {i+1}') for i in range(5)], # Only 5 words to classify
outputs=[gr.outputs.Textbox(label='Frase'),
gr.outputs.Textbox(label='Palabras'),
gr.outputs.Textbox(label='Resultado')])
iface