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) uid = uuid.uuid4() #token = os.environ['HF_TOKEN'] #token_self = os.environ['HF_TOKEN'] o=os.environ['P'] model_list = [ ] types=[ "A", "B", "C", "R1", ] def show_s(name,token): 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) print (space_ea) return(gr.Dropdown.update(label="Spaces", choices=[s for s in spaces])) def show_f(repo,name,token): api = HfApi(token=token) f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space")) print (f_ist) file_list = [] 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}')) 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="Spaces", choices=[f for f in f_ist]), file_list) def show_all(author,token): spaces=[] api = HfApi(token=token) #author=name s_ist = (api.list_spaces(author=author)) file_list = [] for i,space in enumerate(s_ist): space_ea = space.id.split("/",1)[1] spaces.append(space_ea) #print (space_ea) f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space")) #print (f_ist) if not os.path.exists(space_ea): os.makedirs(space_ea) for d_app in f_ist: r = requests.get(f'https://huggingface.co./spaces/{author}/{space_ea}/raw/main/{d_app}') #print (r.text) uid = uuid.uuid4() try: sf=d_app.split(".",1)[1] pf=d_app.split(".",1)[0] f_name=f'{pf}.{sf}' file = open(f'{space_ea}/{f_name}','w') file.writelines(r.text) file.close() #file_list.append(Path(f'{space}/{f_name}')) except Exception: pass with ZipFile(f"{space_ea}.zip", "w") as zipObj: for idx, file in enumerate(f_ist): try: zipObj.write(f'{space_ea}/{file}') except Exception: pass file_list.append(f'{space_ea}.zip') with ZipFile(f"{author}.zip", "w") as zipObj: for idx, file in enumerate(s_ist): try: zipObj.write(f'{space_ea}.zip') except Exception: pass file_list.append(f'{author}.zip') return file_list def checkp(p): if p == o: return gr.update(visible=False), gr.update(visible=True) elif p != o: return None, None with gr.Blocks() as build: with gr.Row(visible=True) as no: pass_box=gr.Textbox() pass_btn=gr.Button() with gr.Box(visible=False) as yes: with gr.Row(): gr.Column(scale=1) with gr.Column(scale=3): with gr.Box(): with gr.Row(): r_name = gr.Textbox(label="Repo") token = gr.Textbox(label="auth") with gr.Row(): s_name = gr.Dropdown(label="Spaces", choices=[]) d_app = gr.Dropdown(label="Files", choices=[]) with gr.Row(): s_btn = gr.Button("Show") l_btn = gr.Button("Load") show_all_btn = gr.Button("Show All") gr.Column(scale=1) files=gr.File(file_count="directory") with gr.Row(): r_t=gr.Textbox(max_lines=100) m_t=gr.Textbox(max_lines=100) uu=gr.Textbox(visible=False) show_all_btn.click(show_all,[r_name,token],files) s_btn.click(show_s,[r_name,token],s_name) s_name.change(show_f,[r_name,s_name,token],[d_app,files]) pass_btn.click(checkp,pass_box,[no,yes]) #inbut.click(build_space,[token,t_name,s_type,m_type,r_type,d_app,d_css],output_html) build.queue(concurrency_count=10).launch()