Spaces:
Runtime error
Runtime error
import gradio as gr | |
import random | |
import json | |
with open('species.json') as f: | |
lista = json.load(f) | |
def inicializar_preguntas(): | |
num_questions = 20 | |
muestra_juego = random.sample(lista, num_questions) | |
questions = [] | |
for especie in muestra_juego: | |
correcta = especie['name'] | |
revision = True | |
while revision: | |
opciones = random.sample(lista, 3) | |
op_names = [op['name'] for op in opciones] | |
if not correcta in op_names: | |
op_names.append(correcta) | |
random.shuffle(op_names) | |
revision = False | |
# posicion_ultima_barra = especie['image_url'].rfind('/') | |
# enlace_recortado = especie['image_url'][:posicion_ultima_barra] | |
questions.append({"image": especie['image_url'], "options": op_names, "answer": correcta}) | |
return questions | |
def check_question(option, ind, score, questions): | |
try: | |
questions = questions.value | |
except: | |
questions = questions | |
try: | |
score = score.value | |
except: | |
score = score | |
try: | |
ind = ind.value | |
except: | |
ind = ind | |
if ind == len(questions) - 1: | |
# # _, ind, score, image, options, _ = fin_preguntas()# restart_quiz() | |
# questions_list = inicializar_preguntas() | |
# questions = gr.State(questions_list) | |
# #ind = gr.State(0) | |
image = gr.Image(height=300, width=300) | |
options = gr.Radio() #choices=questions[ind.value]["options"]) | |
if option == questions[ind]["answer"]: | |
score += 1 | |
texto_corr = "Correcto" | |
else: | |
texto_corr = "Incorrecto\nLa respuesta correcta es: " + questions[ind]["answer"] | |
# try: | |
return texto_corr, ind, score, image, options, f"Terminó el quiz\nTuviste {score} de {ind} preguntas correctas\nScore: {int(score/len(questions)*100)}\nReinicie para empezar de nuevo" | |
# except: | |
# return texto_corr, ind, score, image, options, f"Terminó el quiz\nScore: {int(score.value/len(questions)*100)}\nReinicie para empezar de nuevo" | |
if option == questions[ind]["answer"]: | |
score += 1 | |
texto_corr = "Correcto" | |
else: | |
texto_corr = "Incorrecto\nLa respuesta correcta es: " + questions[ind]["answer"] | |
ind += 1 | |
image = gr.Image(value=questions[ind]["image"], height=300, width=300) | |
options = gr.Radio(choices=questions[ind]["options"]) | |
return texto_corr, ind, score, image, options, f"Llevas: {score} de {ind} preguntas" | |
def restart_quiz(): | |
score = gr.State(0) | |
questions_list = inicializar_preguntas() | |
questions = gr.State(questions_list) | |
try: | |
questions = questions.value | |
except: | |
questions = questions | |
try: | |
score = score.value | |
except: | |
score = score | |
# questions, ind, image, options = inicializar_preguntas() | |
ind = gr.State(0) | |
try: | |
ind = ind.value | |
except: | |
ind = ind | |
image = gr.Image(value=questions[ind]["image"], height=300, width=300) | |
options = gr.Radio(choices=questions[ind]["options"]) | |
output = "" | |
return output, ind, score, image, options, f"", questions | |
def fin_preguntas(): | |
# score = gr.State(0) | |
questions_list = inicializar_preguntas() | |
questions = gr.State(questions_list) | |
# questions, ind, image, options = inicializar_preguntas() | |
return "Fin", ind, score, image, options, f"Score: {int(score.value/len(questions)*100)}" | |
with gr.Blocks() as demo: | |
# Preguntas al azar | |
questions_list = inicializar_preguntas() | |
questions = gr.State(questions_list) | |
# Variables | |
score = gr.State(0) | |
ind = gr.State(0) | |
# Componentes | |
image = gr.Image(value=questions.value[ind.value]["image"], height=300, width=300) | |
options = gr.Radio(choices=questions.value[ind.value]["options"]) | |
button = gr.Button("Siguiente pregunta") | |
output = gr.Textbox(label="Output Box") | |
output_score = gr.Textbox(label="Output Box") | |
restart_button = gr.Button("Restart") | |
# Funciones | |
button.click(fn=check_question, inputs=[options, ind, score, questions], outputs=[output, ind, score, image, options, output_score]) | |
restart_button.click(fn=restart_quiz, inputs=[], outputs=[output, ind, score, image, options, output_score, questions]) | |
if __name__ == "__main__": | |
demo.launch(share=True) | |