import os
import sys
import uuid
import gradio as gr
import requests
import json
from zipfile import ZipFile
import shutil
from pathlib import Path
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi,snapshot_download)
from PIL import Image
uid = uuid.uuid4()
css="""
.wrap.svelte-vm32wk.svelte-vm32wk.svelte-vm32wk {
display:block;
}
.wrap.svelte-1k4wjf2.svelte-1k4wjf2.svelte-1k4wjf2{
display:block;
}
"""
def show_spaces(name,token): #show_s
spaces=[]
spaces.append("")
api = HfApi(token=token)
author=name
s_ist = (api.list_spaces(author=author))
for i,space in enumerate(s_ist):
space_ea = space.id.split("/",1)[1]
spaces.append(space_ea)
return(gr.Dropdown.update(label="Spaces", choices=[s for s in spaces]),
gr.update(choices=[s for s in spaces],interactive=True),
gr.update(choices=[s for s in spaces],interactive=True))
def show_files(repo,name,token): #show_f
api = HfApi(token=token)
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
file_list = []
if not os.path.exists(name):
os.makedirs(name)
for d_app in f_ist:
if "/" in d_app:
dir_1=d_app.split("/",1)[0]
rem_1=d_app.split("/",1)[1]
if not os.path.exists(f'{name}/{dir_1}'):
os.makedirs(f'{name}/{dir_1}')
if "/" in rem_1:
dir_2=rem_1.split("/",1)[0]
rem_2=rem_1.split("/",1)[1]
if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
os.makedirs(f'{name}/{dir_1}/{dir_2}')
sf=rem_2.split(".",1)[1]
pf=rem_2.split(".",1)[0]
f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
else:
sf=rem_1.split(".",1)[1]
pf=rem_1.split(".",1)[0]
f_name=f'{dir_1}/{pf}.{sf}'
else:
sf=d_app.split(".",1)[1]
pf=d_app.split(".",1)[0]
f_name=f'{pf}.{sf}'
pass
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
uid = uuid.uuid4()
file = open(f'{name}/{f_name}','wb')
file.write(r.content)
file.close()
file_list.append(Path(f'{name}/{f_name}'))
with ZipFile(f"{name}.zip", "w") as zipObj:
for idx, file in enumerate(f_ist):
zipObj.write(f'{name}/{file}')
file_list.append(f'{name}.zip')
s_info=api.space_info(f'{repo}/{name}',files_metadata=True)
'''
with open(f'{uid}-tmp.json','w') as f:
json.dump(s_info,f,indent=4)
f.close()
'''
return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist],interactive=True),
file_list,
gr.update(choices=[f for f in f_ist],interactive=True),
api.get_space_runtime(f'{repo}/{name}'))
def show_f2(repo,name,token):
api = HfApi(token=token)
#f_ist = snapshot_download(repo_id=f'{repo}/{name}', repo_type="space")
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
print (f_ist)
file_list = []
file_out = []
if not os.path.exists(name):
os.makedirs(name)
for d_app in f_ist:
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
#print (r.text)
uid = uuid.uuid4()
sf=d_app.split(".",1)[1]
pf=d_app.split(".",1)[0]
f_name=f'{pf}.{sf}'
file = open(f'{name}/{f_name}','w')
file.writelines(r.text)
file.close()
file_list.append(Path(f'{name}/{f_name}'))
file_out.append(d_app)
with ZipFile(f"{name}.zip", "w") as zipObj:
for idx, file in enumerate(f_ist):
zipObj.write(f'{name}/{file}')
file_list.append(f'{name}.zip')
return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist],interactive=True), file_list)
def show_f_cont(repo,name,file,token):
html_text = '\n
\n
\n'
images=[".png" , ".jpg" , ".gif" , ".webm" , ".mp4"]
is_im=False
for x in images:
if x in file:
html_text += f''
out_text = "Image File"
is_im=True
else:
pass
if is_im==False:
print(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
text=r.text
html_text += f'