File size: 5,536 Bytes
f8ffd97 a7cf647 f8ffd97 9a461ac |
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 144 145 146 147 148 149 150 151 152 153 154 155 |
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) |