Spaces:
Sleeping
Sleeping
File size: 7,072 Bytes
d714d3d 8fa564a f894099 60ed47f 49687e4 ed88b70 49687e4 8d217ca 9265cc1 8d217ca 36b3c9a 62a99f6 4a61368 9d347fb 760748f 33cdf93 68603df 4517647 760748f 62a99f6 120956a f894099 3c99043 d714d3d 384f0cb 3c99043 f894099 60ed47f 75bac5c c1d30cd 75bac5c c1d30cd 75bac5c f894099 746d0e8 c1e08f5 a8a0883 36d381f 813a644 1453434 79687a9 eb75b14 746d0e8 eb75b14 746d0e8 eb75b14 746d0e8 eb75b14 746d0e8 f6669c0 e1ee37b 36d381f 5a314c8 344723f 3f32df4 344723f ad1b54b 344723f ad1b54b 344723f ad1b54b 344723f ad1b54b 36d381f ff4e253 1e5a82e f6c149f 1e5a82e f6c149f 3e1699e 1e5a82e f6c149f 3e1699e 1e5a82e f6c149f 3e1699e 1e5a82e f6c149f 3e1699e 1e5a82e d6f165f 1f375b6 9a226eb 4a61368 9a226eb d6f165f 36d381f 9a226eb d6f165f ef02a4a 351c0ce 36d381f 09974cb 36d381f 75bac5c d6f165f 09974cb 1e5a82e 4a61368 d714d3d |
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
import gradio as gr
import pandas as pd
import requests
import json
from datasets import load_dataset
#dataset = load_dataset("nlphuji/flickr30k", split="test[10:20]")
#print (dataset)
#headers = {"Authorization": f"Bearer {API_TOKEN}"}
def query1(fetch_url):
if fetch_url=="":
fetch_url = "nlphuji/flickr30k"
API_URL1 = f"https://datasets-server.huggingface.co/splits?dataset={fetch_url}"
response = requests.get(API_URL1)
json_obj=response.json()
config_box = []
split_box = []
for obj in json_obj:
print (obj)
if obj == "splits":
try:
for split in json_obj[obj]:
print (f'split: {split}')
out_config = split['config']
out_split = split['split']
print (out_config)
print (out_split)
config_box.append(out_config)
split_box.append(out_split)
except Exception as e:
print (e)
pass
#df = pd.DataFrame.from_dict(json_obj)
return json_obj,gr.update(choices=[c for c in config_box]),gr.update(choices=[s for s in split_box])
def query2(fetch_url,config="TEST",split="test",offset=0,length=10):
if fetch_url=="":
fetch_url = "nlphuji/flickr30k"
#offset=0
#length=10
API_URL2 = f"https://datasets-server.huggingface.co/rows?dataset={fetch_url}&config={config}&split={split}&offset={offset}&length={length}"
response = requests.get(API_URL2)
dictionary=response.json()
#print (len(dictionary))
lvl_1=[]
for key in dictionary:
lvl_1.append(key)
print (key)
return dictionary,gr.update(choices=[l for l in lvl_1])
def find_fn(lvl_1,lvl_2,fetch_url,config="TEST",split="test",offset=0,length=10):
#print(out_json['rows'])
#print (inp)
img_list=[]
out_json,_=query2(fetch_url,config,split,offset=10,length=20)
lvl_1=lvl_1.strip("[]")
lvl_2=lvl_2.strip('""').strip("''")
#print(inp)
cnt_lvl = ""
if lvl_2 != "":
ea_lvl_len = len(lvl_2)
print (ea_lvl_len)
for ea_lvl in lvl_2:
cnt_lvl = f'{cnt_lvl}[{ea_lvl}]'
for ea in out_json[f"{lvl_1}"]:
cnt_lvl=cnt_lvl.strip("[]")
#img_ea = ea['row']['image']['src']
img_list.append(ea[f"{cnt_lvl}"])
if lvl_2 == "":
img_list.append(out_json[lvl_1])
return img_list
def upd_drop(lvl_1=None,lvl_2=None,lvl_3=None,lvl_4=None,lvl_5=None,fetch_url="",config="TEST",split="test",offset=0,length=10):
out_json,_=query2(fetch_url,config,split,offset=10,length=20)
box_1=[]
box_2=[]
box_3=[]
box_4=[]
box_5=[]
out1=None
out2=None
out3=None
out4=None
print (f'lvl_1 ::: {lvl_1}')
print (f'lvl_2 ::: {lvl_2}')
print (f'lvl_3 ::: {lvl_3}')
print (f'lvl_4 ::: {lvl_4}')
print (f'lvl_5 ::: {lvl_5}')
out=[]
if lvl_1 == "rows":
lvl_src= out_json[lvl_1][0]
else:
lvl_src=out_json[lvl_1]
print (f"lvl_src:: {lvl_src}")
try:
if lvl_1 !=[]:
for ea in (lvl_src):
box_1.append(ea)
out= gr.update(choices = [m for m in box_1])
except Exception:
pass
try:
if lvl_2 !=[]:
for ea in (lvl_src[lvl_2]):
print (ea)
box_2.append(ea)
out= gr.update(choices = [m for m in box_2])
else:
pass
except Exception:
pass
try:
if lvl_3 !=[]:
for ea in (lvl_src[lvl_2][lvl_3]):
box_3.append(ea)
out= gr.update(choices = [m for m in box_3])
else:
pass
except Exception:
pass
try:
if lvl_4 !=[]:
for ea in (lvl_src[lvl_2][lvl_3][lvl_4]):
box_4.append(ea)
out= gr.update(choices = [m for m in box_4])
else:
pass
return out
def find_items(lvl_1,lvl_2,lvl_3,lvl_4,lvl_5,fetch_url,config="TEST",split="test",offset=0,length=10):
out_json,_=query2(fetch_url,config,split,offset=10,length=20)
box_1=[]
box_2=[]
box_3=[]
box_4=[]
box_5=[]
if lvl_1 !=[]:
for ea in (out_json[lvl_1]):
box_1.append(ea)
out = box_1
if lvl_2 !=[]:
for ea in (out_json[lvl_1]):
box_2.append(ea[lvl_2])
out = box_2
if lvl_3 !=[]:
for ea in (out_json[lvl_1]):
box_3.append(ea[lvl_2][lvl_3])
out = box_3
if lvl_4 !=[]:
for ea in (out_json[lvl_1]):
box_4.append(ea[lvl_2][lvl_3][lvl_4])
out = box_4
if lvl_5 !=[]:
for ea in (out_json[lvl_1]):
box_5.append(ea[lvl_2][lvl_3][lvl_4][lvl_5])
out = box_5
return out
def img_fn(inp):
img_box=[]
for img in inp:
out = Image.open(inp)
img_box.append(out)
return img_box
with gr.Blocks() as app:
with gr.Box():
with gr.Row():
with gr.Column(scale=3):
with gr.Row():
data_set_url=gr.Textbox(label="Dataset (repo/name)")
fetch_btn=gr.Button()
with gr.Row():
config_set=gr.Dropdown(label="Config")
split_set=gr.Dropdown(label="Split")
#config_drop = gr.Dropdown(label="Config/Split", choices=[])
view_data=gr.Button(label="View")
with gr.Row():
lvl_1=gr.Dropdown(label="lvl_1")
lvl_2=gr.Dropdown(label="lvl_2")
lvl_3=gr.Dropdown(label="lvl_3")
lvl_4=gr.Dropdown(label="lvl_4")
lvl_5=gr.Dropdown(label="lvl_5")
find_btn=gr.Button("Search")
with gr.Tab("JSON"):
out_find = gr.JSON()
with gr.Tab("Gallery"):
gal_btn=gr.Button("Load Images")
out_gal = gr.Gallery()
with gr.Column(scale=1):
out_json = gr.JSON()
lvl_1.change(upd_drop,[lvl_1,lvl_2,lvl_3,lvl_4,lvl_5,data_set_url,config_set,split_set],[lvl_2])
lvl_2.change(upd_drop,[lvl_1,lvl_2,lvl_3,lvl_4,lvl_5,data_set_url,config_set,split_set],[lvl_3])
lvl_3.change(upd_drop,[lvl_1,lvl_2,lvl_3,lvl_4,lvl_5,data_set_url,config_set,split_set],[lvl_4])
lvl_4.change(upd_drop,[lvl_1,lvl_2,lvl_3,lvl_4,lvl_5,data_set_url,config_set,split_set],[lvl_5])
find_btn.click(find_items,[lvl_1,lvl_2,lvl_3,lvl_4,lvl_5,data_set_url,config_set,split_set],out_find)
view_data.click(query2,[data_set_url,config_set,split_set],[out_find,lvl_1])
gal_btn.click(img_fn,out_find,out_gal)
#find_btn.click(find_fn,[lvl_1,lvl_2,data_set_url,config_set,split_set],[out_find])
fetch_btn.click(query1,data_set_url,[out_json,config_set,split_set])
app.launch() |