Spaces:
Running
Running
File size: 2,278 Bytes
48cb310 |
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 |
import gspread
import random
import os
import unicodedata
import gradio as gr
import keyfiledict
from oauth2client.service_account import ServiceAccountCredentials
from dotenv import load_dotenv
load_dotenv()
import json
def getData():
gc = gspread.service_account_from_dict(keyfiledict.create_keyfile_dict())
spreadsheet = gc.open_by_key(os.environ['OPEN_BY_KEY'])
data = spreadsheet.worksheet('main').get_all_values()
data = [[unicodedata.normalize('NFKC', row[0]).strip()] + row[1:] for row in data][1:] # normalize chars and remove header row
return data
##### game #####
## globals
data = getData()
possibleAnswers = [unicodedata.normalize('NFKC', filename.rsplit('.', 1)[0]).strip() for filename in os.listdir('./assets')]
answer = ""
minPairGroup = []
score = 0
## setters
def setRandAnswer():
global answer
global possibleAnswers
answer = random.choice(possibleAnswers)
def setMinPairGroup():
global answer
global minPairGroup
global data
answerRowIndex = next((i for i, sublist in enumerate(data) if sublist and sublist[0] == answer), -1)
minPairGroup = [sublist[0] for sublist in data if sublist[1] == data[answerRowIndex][1]]
def updateScore(radio):
global score
if radio == answer:
score += 5
else:
score -= 1
## gradio
# submit button function. updates everything.
def onSubmit(image, radio, scorebox):
global answer
global minPairGroup
global score
updateScore(radio)
setRandAnswer()
setMinPairGroup()
image = gr.Image(f'./assets/{answer}.jpeg', label=answer, show_label= False)
radio = gr.Radio(choices=minPairGroup, label='Which word is pictured?', value=None)
scorebox = gr.Markdown(f"# Score: {score}")
return image, radio, scorebox
# the interface
with gr.Blocks() as demo:
setRandAnswer()
setMinPairGroup()
with gr.Column():
scorebox = gr.Markdown(f"# Score: {score}")
image = gr.Image(f'./assets/{answer}.jpeg', label=answer, show_label= False)
radio = gr.Radio(choices=minPairGroup, label='Which word is pictured?')
submitButton = gr.Button("Submit")
submitButton.click(onSubmit, inputs= [image, radio, scorebox], outputs=[image, radio, scorebox])
demo.launch() |