import gradio as gr import json import random import math import requests css = """ #small span{ font-size: 3.0em; } """ def loadpic(): global c1, e1, e2, e3, e4 #res = json.loads(requests.get("http://shibe.online/api/shibes?count=3").text) res = json.loads(requests.get("https://api.thecatapi.com/v1/images/search?has_breeds=1").text) cheetahs = res c1 = [res[0]['url']] #; c2 = [res[1]]; c3 = [res[2]] #print(str(c1)) #e = ['https://http.cat/images/404.jpg'] #res= requests.get("https://api.thecatapi.com/v1/images/search?has_breeds=1").text #print(str(res)) res= requests.get("https://random.imagecdn.app/v1/image?width=300&height=300&category=buildings&format=json").text e1 = [(json.loads(res))['url']] #res= requests.get("https://random.imagecdn.app/v1/image?width=300&height=300&category=buildings&format=json").text e2 = [(json.loads(res))['url']] #res= requests.get("https://random.imagecdn.app/v1/image?width=300&height=300&category=buildings&format=json").text e3 = [(json.loads(res))['url']] #res= requests.get("https://random.imagecdn.app/v1/image?width=300&height=300&category=buildings&format=json").text e4 = [(json.loads(res))['url']] return None with gr.Blocks(css=css) as demo: infile = 'test.json' with open(infile, 'r',encoding='utf-8') as f: json_data = json.load(f,strict=False) l = len(json_data) r = math.floor(random.random()*l) qa = json_data[r] q = qa['question'] d3 = qa['distractor3'] d2 = qa['distractor2'] d1 = qa['distractor1'] a = qa['correct_answer'] s = qa['support'] ds = [] ds.append(d3); ds.append(d2); ds.append(d1); ds.append(a) #random.shuffle(ds) global c1, e1, e2, e3, e4 #res = json.loads(requests.get("http://shibe.online/api/shibes?count=3").text) res = json.loads(requests.get("https://api.thecatapi.com/v1/images/search?has_breeds=1").text) cheetahs = res c1 = [res[0]['url']] #; c2 = [res[1]]; c3 = [res[2]] ''' cheetahs = [ "https://api.thecatapi.com/v1/images/search", "https://gradio-builds.s3.amazonaws.com/assets/lite-logo.png", "https://gradio-builds.s3.amazonaws.com/assets/TheCheethcat.jpg", ] ''' ''' def update_examples(): global q, d3, d2, d1, a, s, json_data global qa, ds #samples = get_new_examples() r = math.floor(random.random()*l) qa = json_data[r] q = qa['question'] d3 = qa['distractor3'] d2 = qa['distractor2'] d1 = qa['distractor1'] a = qa['correct_answer'] ds = [] ds.append(d3); ds.append(d2); ds.append(d1); ds.append(a) random.shuffle(ds) #btn1.value = q return None #return gr.Button.update(value=q) ''' def update_q(): global q, d3, d2, d1, a, s, json_data global qa, ds #samples = get_new_examples() r = math.floor(random.random()*l) qa = json_data[r] q = qa['question'] d3 = qa['distractor3'] d2 = qa['distractor2'] d1 = qa['distractor1'] a = qa['correct_answer'] ds = [] ds.append(d3); ds.append(d2); ds.append(d1); ds.append(a) #random.shuffle(ds) loadpic() return q with gr.Row(): with gr.Column(): t = gr.Textbox(value=q, label = 'league of animal', show_label=True) #global gtn1 btn1 = gr.Button(value=ds[0], elem_id="small") btn2 = gr.Button(value=ds[1], elem_id="small") btn3 = gr.Button(value=ds[2], elem_id="small") btn = gr.Button(value=ds[3], elem_id="small") btn_refresh = gr.Button(value="Next") #update_examples() btn_refresh.click(update_q, None, [t]) btn_refresh.click(lambda: ds[0], None, [btn]) btn_refresh.click(lambda: ds[1], None, [btn1]) btn_refresh.click(lambda: ds[2], None, [btn2]) btn_refresh.click(lambda: ds[3], None, [btn3]) print(str(ds)) #btn_refresh.click(update_examples, None, None) #btn_refresh.click(None, js="window.location.reload()") #btn_refresh.click(update_examples,inputs=None,outputs=[btn1]) with gr.Column(): gallery = gr.Gallery(label='', show_download_button=True) gr.Audio(visible=False) #with gr.Column(): # select_output = gr.Textbox(label="Select Data") ''' def select(select_data: gr.SelectData): #return select_data.value['image']['url'] return s gallery.select(select, None, select_output) ''' #def combine(a, b): # return "https://gradio-builds.s3.amazonaws.com/assets/cheetah-003.jpg" if(btn.value==a): btn.click(lambda: c1, None, [gallery]) else: btn.click(lambda: e1, None, [gallery]) if(btn1.value==a): btn1.click(lambda: c1, None, [gallery]) else: btn1.click(lambda: e2, None, [gallery]) if(btn2.value==a): btn2.click(lambda: c1, None, [gallery]) else: btn2.click(lambda: e3, None, [gallery]) if(btn3.value==a): btn3.click(lambda: c1, None, [gallery]) else: btn3.click(lambda: e4, None, [gallery]) #loadpic() if __name__ == "__main__": demo.launch(share=True, debug=True) #demo.launch() demo.launch(share=True, debug=True)