Spaces:
Build error
Build error
| 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() | |
| drop_box = [] | |
| for obj in json_obj: | |
| print (obj) | |
| out_config = obj[0] | |
| out_split = obj[1] | |
| print (out_config) | |
| print (out_split) | |
| drop_box.append(obj[0]) | |
| #df = pd.DataFrame.from_dict(json_obj) | |
| return json_obj | |
| 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,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]): | |
| #print (i) | |
| print (ea) | |
| box_1.append(ea) | |
| out = box_1 | |
| if lvl_2 !="": | |
| for ea in (out_json[lvl_1][lvl_2]): | |
| #print (i) | |
| print (ea) | |
| box_2.append(ea) | |
| out = box_2 | |
| if lvl_3 !="": | |
| for ea in (out_json[lvl_1][lvl_2][lvl_3]): | |
| #print (i) | |
| print (ea) | |
| box_3.append(ea) | |
| out = box_3 | |
| if lvl_4 !="": | |
| for ea in (out_json[lvl_1][lvl_2][lvl_3][lvl_4]): | |
| #print (i) | |
| print (ea) | |
| box_4.append(ea) | |
| out = box_4 | |
| if lvl_5 !="": | |
| for ea in (out_json[lvl_1][lvl_2][lvl_3][lvl_4][lvl_5]): | |
| #print (i) | |
| print (ea) | |
| box_5.append(ea) | |
| out = box_5 | |
| return out | |
| with gr.Blocks() as app: | |
| with gr.Row(): | |
| data_set_url=gr.Textbox(label="Dataset (repo/name)") | |
| fetch_btn=gr.Button() | |
| with gr.Row(): | |
| config_set=gr.Textbox(label="Config") | |
| split_set=gr.Textbox(label="Split") | |
| #config_drop = gr.Dropdown(label="Config/Split", choices=[]) | |
| view_data=gr.Button(label="View") | |
| with gr.Row(): | |
| lvl_1=gr.Textbox("lvl_1") | |
| lvl_2=gr.Textbox("lvl_2") | |
| lvl_3=gr.Textbox("lvl_3") | |
| lvl_4=gr.Textbox("lvl_4") | |
| lvl_5=gr.Textbox("lvl_5") | |
| find_btn=gr.Button("Search") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| out_json = gr.JSON() | |
| with gr.Column(scale=3): | |
| out_find = gr.JSON() | |
| out_gal = gr.Gallery() | |
| find_btn.click(upd_drop,[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]) | |
| #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) | |
| app.launch() |