GramAPP / app.py
Merlintxu's picture
Update app.py
2b39abd
raw
history blame
1.29 kB
import gradio as gr
import spacy
from transformers import pipeline
# Load the Spanish language model
nlp = spacy.load('es_core_news_sm')
# Load the text generation pipeline
text_generator = pipeline('text-generation', model='gpt2')
def generate_sentence():
# Generate a sentence
try:
result = text_generator('', max_length=50)[0]
sentence = result['generated_text']
return sentence
except Exception as e:
return str(e)
def analyze_sentence(sentence):
# Analyze the sentence
doc = nlp(sentence)
tagged_words = [(token.text, token.pos_) for token in doc]
return tagged_words
def game_handler(action, sentence, answer):
if action == 'generate':
return generate_sentence(), ''
elif action == 'check':
tagged_words = analyze_sentence(sentence)
correct_answer = [tag for word, tag in tagged_words]
if answer == correct_answer:
return sentence, 'Correcto!'
else:
return sentence, 'Incorrecto. La respuesta correcta es: ' + str(correct_answer)
else:
return sentence, 'Accion desconocida.'
iface = gr.Interface(fn=game_handler,
inputs=['dropdown', 'text', 'list'],
outputs=['text', 'text'])
iface.launch()