Spaces:
Sleeping
Sleeping
import gradio as gr | |
from huggingface_hub import HfApi, list_files_info | |
import requests | |
import json | |
token = "" | |
source_dataset = "jbilcke-hf/ai-tube-index" | |
api = HfApi(token=token) | |
def find_dataset(author=""): | |
s_ist = (api.list_datasets(author=author,search="ai-tube")) | |
#print(api.whoami()) | |
channels=[] | |
videos=[] | |
for space in s_ist: | |
try: | |
channels.append(space.id) | |
except Exception: | |
pass | |
return (gr.Dropdown(label="Channels", choices=[s for s in channels],interactive=True)) | |
def load_vid(channel): | |
out_html="" | |
label_html="" | |
vid_list=[] | |
sync_list=[] | |
try: | |
file_box = [info.path for info in list_files_info(f'{channel}', repo_type="dataset")] | |
ai_tube_box = [info.path for info in list_files_info(f'{source_dataset}', repo_type="dataset")] | |
for file in file_box: | |
if file.startswith("prompt_"): | |
prompt_slug = file.split("prompt_",1)[1].split(".",1)[0] | |
out_html=f'{out_html}<pre> • {file}</pre>' | |
#print(prompt_slug) | |
for vid_file in ai_tube_box: | |
try: | |
vid_slug=vid_file.split("videos/",1)[1].split(".",1)[0] | |
#print(vid_slug) | |
try: | |
if vid_slug == prompt_slug: | |
out_html=f'{out_html}<pre> + {vid_file}</pre>' | |
if vid_file.endswith(".json"): | |
r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/raw/main/videos/{vid_slug}.json') | |
#r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/blob/main/videos/00b4bcda-7b4a-40f8-9833-e490425a7b91.mp4') | |
json_out=r.json() | |
label_html=f'{label_html}{json_out["label"]}<br>{json_out["description"]}<br>' | |
#print (r.json()) | |
if vid_file.endswith(".mp4"): | |
vid_list.append(json_out["label"]) | |
sync_list.append(vid_file) | |
#<source src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/{vid_file}" type="video/mp4"> | |
print(vid_file) | |
label_html=f'''{label_html} | |
<video src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/{vid_file}" controls> | |
</video>''' | |
#uid = uuid.uuid4() | |
#file = open(f'{name}/{f_name}','wb') | |
#file.write(r.content) | |
except Exception as e: | |
print (f'error:: {e}') | |
pass | |
except Exception: | |
pass | |
except Exception as e: | |
print(e) | |
return(out_html,gr.Dropdown(label="Videos",type="index", choices=[s for s in vid_list],interactive=True),sync_list) | |
def show_vid(vid_file,list_hid): | |
#vid_slug=vid_file.split("videos/",1)[1].split(".",1)[0] | |
label_html=f'''{label_html} | |
<video src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/{list_hid[int(vid_file)]}" controls> | |
</video>''' | |
return label_html | |
def load_vid_og(channel): | |
out_html="" | |
label_html="" | |
try: | |
file_box = [info.path for info in list_files_info(f'{channel}', repo_type="dataset")] | |
ai_tube_box = [info.path for info in list_files_info(f'{source_dataset}', repo_type="dataset")] | |
for file in file_box: | |
if file.startswith("prompt_"): | |
prompt_slug = file.split("prompt_",1)[1].split(".",1)[0] | |
out_html=f'{out_html}<pre> • {file}</pre>' | |
#print(prompt_slug) | |
for vid_file in ai_tube_box: | |
try: | |
vid_slug=vid_file.split("videos/",1)[1].split(".",1)[0] | |
#print(vid_slug) | |
try: | |
if vid_slug == prompt_slug: | |
out_html=f'{out_html}<pre> + {vid_file}</pre>' | |
if vid_file.endswith(".json"): | |
r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/raw/main/videos/{vid_slug}.json') | |
#r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/blob/main/videos/00b4bcda-7b4a-40f8-9833-e490425a7b91.mp4') | |
json_out=r.json() | |
label_html=f'{label_html}{json_out["label"]}<br>{json_out["description"]}<br>' | |
print (r.json()) | |
if vid_file.endswith(".mp4"): | |
#<source src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/{vid_file}" type="video/mp4"> | |
print(vid_file) | |
label_html=f'''{label_html} | |
<video src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/{vid_file}" controls> | |
</video>''' | |
#uid = uuid.uuid4() | |
#file = open(f'{name}/{f_name}','wb') | |
#file.write(r.content) | |
except Exception as e: | |
print (f'error:: {e}') | |
pass | |
except Exception: | |
pass | |
except Exception as e: | |
print(e) | |
return(out_html,label_html) | |
with gr.Blocks() as app: | |
with gr.Row(): | |
channel = gr.Dropdown() | |
videos = gr.Dropdown() | |
button = gr.Button() | |
output = gr.HTML() | |
label_output = gr.HTML() | |
list_hid=gr.Textbox() | |
app.load(find_dataset,None,channel) | |
channel.change(load_vid,channel,[output,videos,list_hid]) | |
videos.change(show_vid,[videos,list_hid],[label_output]) | |
app.launch(show_api=False,share=False) |