File size: 1,291 Bytes
a8bb7b0
b09df44
 
a8bb7b0
ee758c2
a8bb7b0
ee758c2
 
b09df44
a8bb7b0
b09df44
ee758c2
 
 
 
 
 
 
a8bb7b0
b09df44
ee758c2
a8bb7b0
 
 
 
2b39abd
 
 
 
 
 
 
 
 
 
b09df44
2b39abd
a8bb7b0
2b39abd
 
 
ee758c2
9e5dc42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()