Spaces:
Runtime error
Runtime error
File size: 4,409 Bytes
b152d54 |
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
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)
|