import urllib.request import random import json import gradio page = urllib.request.urlopen('https://raw.githubusercontent.com/mazyvan/most-common-spanish-words/master/most-common-spanish-words-v5.txt').read().decode() words = page.split('\n') len(words) len_to_words = {} for word in words: if len(word) not in len_to_words: len_to_words[len(word)] = [word] else: len_to_words[len(word)].append(word) def get_random_word(n_letters, forbidden_letters='', required_letters=''): random.shuffle(len_to_words[n_letters]) for word in len_to_words[n_letters]: lower_word = word.lower() if any(letter in lower_word for letter in forbidden_letters): continue if required_letters: if all(letter in lower_word for letter in required_letters): return '# ' + word else: return '# ' + word description = """ Genera palabras aleatorias con el número deseado de letras para aprender a leer. Se puede forzar a que las palabras tengan o no tengan determinadas letras """ interface = gradio.Interface( get_random_word, inputs=[gradio.Slider(2, 15, value=5, step=1, label='Número de letras'), gradio.Textbox(label='Letras prohibidas'), gradio.Textbox(label='Letras obligatorias')], #outputs=[gradio.Textbox(label='')], outputs=[gradio.Markdown(label='')], title='Aprende a leer', description=description, allow_flagging=False) interface.launch(server_name="0.0.0.0")