GramAPP / app.py
Merlintxu's picture
Update app.py
6c338fd
raw
history blame
1.87 kB
## app.py ##
import gradio as gr
import spacy
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']
# Store the sentence state globally
sentence_state = {'sentence': '', 'tagged_words': []}
# Generate a sentence and analyze it
def generate_and_analyze():
result = text_generator('', max_length=10, do_sample=True, language='es')[0]
sentence = result['generated_text']
doc = nlp(sentence)
tagged_words = [(token.text, token.pos_) for token in doc]
sentence_state['sentence'] = sentence
sentence_state['tagged_words'] = tagged_words
return sentence, tagged_words
# The game logic
def game_flow(start_game, *args):
if start_game == 'Empezar':
sentence, _ = generate_and_analyze()
return sentence, '', ''
else:
correct_answer = [tag for _, tag in sentence_state['tagged_words']]
user_answer = list(args)
if user_answer == correct_answer:
return sentence_state['sentence'], ' '.join(sentence_state['tagged_words']), '¡Correcto!'
else:
return sentence_state['sentence'], ' '.join(sentence_state['tagged_words']), 'Incorrecto. La respuesta correcta es: ' + ' '.join(correct_answer)
iface = gr.Interface(fn=game_flow,
inputs=[gr.inputs.Textbox(label='Escribe "Empezar" para generar una frase')] +
[gr.inputs.Dropdown(choices=pos_tags, label=f'Palabra {i+1}') for i in range(5)],
outputs=[gr.outputs.Textbox(label='Frase'),
gr.outputs.Textbox(label='Palabras'),
gr.outputs.Textbox(label='Resultado')])
iface.launch()