| import os |
| import sys |
| import uuid |
| import gradio as gr |
| import requests |
| import json |
| import tarfile |
| from pathlib import Path |
| from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi) |
| uid = uuid.uuid4() |
| print(uid) |
| isExist = os.path.exists(f'{uid}') |
| if not isExist: |
| os.makedirs(f'{uid}') |
| |
| o=os.environ['P'] |
| model_list = [ |
| |
| ] |
|
|
| types=[ |
| "A", |
| "B", |
| "C", |
| "R1", |
| ] |
|
|
|
|
| def show_s(name,d_app,token): |
| api = HfApi(token=token) |
| author=name |
| s_ist = (api.list_spaces(author=author)) |
| print (s_ist) |
| file_j = open(f'{uid}/tmp_j','w') |
| file_j.writelines(str(s_ist)) |
| file_j.close() |
| file_b = open(f'{uid}/tmp_j','r') |
| r_file = file_b.readlines() |
| spaces=[] |
| spaces2=[] |
| for line in r_file: |
| if "'id':" in line: |
| line = line.split(":",1)[1] |
| line = line.split("/",1)[1] |
| line2 = line.replace("'","").replace(",","").replace("\n","") |
| spaces.append(line2) |
| print(f"{line2}") |
| return(gr.update(label="Spaces", choices=[s for s in spaces]),gr.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")) |
| return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist])) |
|
|
| def load_space(r_name,s_name,d_app): |
| print(f'https://huggingface.co/spaces/{r_name}/{s_name}/raw/main/{d_app}') |
| r = requests.get(f'https://huggingface.co/spaces/{r_name}/{s_name}/raw/main/{d_app}') |
| |
| |
| |
| |
| file = open(f'{uid}/{d_app}','w') |
| file.writelines(r.text) |
| file.close() |
| return r.text |
| def repl(rs,rt,d_app): |
| file = open(f'{uid}/{d_app}','r') |
| r_file = file.read() |
| r_file = r_file.replace(f'{rs}',f'{rt}') |
| o_file = open(f'{uid}/mod_{d_app}','w') |
| o_file = o_file.write(r_file) |
| file.close() |
| return r_file, f'{uid}/mod_{d_app}' |
|
|
| def get_f(r_name,spaces,token): |
| api = HfApi(token=token) |
| spaces_b=[] |
| files_b=[] |
| files_c=[] |
| |
| for space in spaces: |
| isExist = os.path.exists(f'{space}') |
| if not isExist: |
| os.makedirs(f'{space}') |
| f_ist = (api.list_repo_files(repo_id=f'{r_name}/{space}', repo_type="space")) |
| for file in f_ist: |
| t_file=open(f'{uid}/{space}-{file}','w') |
| zz_file=open(f'{space}/{file}','w') |
| r = requests.get(f'https://huggingface.co/spaces/{r_name}/{space}/raw/main/{file}') |
| t_file.write(r.text) |
| t_file.close() |
| zz_file.write(r.text) |
| zz_file.close() |
| files_b.append(f'{uid}/{space}-{file}') |
| with tarfile.open(f'{space}.tar.gz','w:gz') as tar: |
| tar.add(f"./{space}",arcname=os.path.basename(f"./{space}")) |
|
|
| spaces_b.append(f'{space}.tar.gz') |
| |
| return(files_b,spaces_b) |
|
|
| def build_space(token,r_name,s_name,d_app,rd_name,sd_name,dd_app): |
| if token==None or token=="": |
| token = token_self |
| else: |
| token = token |
| pass |
| if dd_app=="": |
| dd_app=d_app |
| if rd_name=="": |
| rd_name=r_name |
| if sd_name=="": |
| sd_name=s_name |
| api = HfApi(token=token) |
| repo_name=f'{rd_name}/{sd_name}' |
| print (repo_name) |
| try: |
| api.upload_file( |
| path_or_fileobj=f"{uid}/mod_{d_app}", |
| path_in_repo=f"{dd_app}", |
| repo_id=repo_name, |
| token=token, |
| repo_type="space", |
| ) |
| |
| return gr.HTML.update(f'''<center>Link to space <a href="https://huggingface.co/spaces/{repo_name}" target="_blank">https://huggingface.co/spaces/{repo_name}</a><center><br>''') |
| except Exception as e: |
| return gr.HTML.update(f"""{str(e)}""") |
|
|
| |
| 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(): |
| with gr.Column(): |
| r_name = gr.Textbox(label="Source Repo") |
| s_name = gr.Dropdown(label="Spaces", choices=[]) |
| d_app = gr.Dropdown(label="Files", choices=[]) |
| with gr.Column(): |
| rd_name = gr.Textbox(label="Dest Repo") |
| sd_name = gr.Dropdown(label="Spaces", choices=[]) |
| dd_app = gr.Dropdown(label="Files", choices=[]) |
| with gr.Row(): |
| s_btn = gr.Button("Show") |
| l_btn = gr.Button("Load") |
| with gr.Box(): |
| r_s = gr.Textbox(label="Replace") |
| r_w = gr.Textbox(label="with:") |
| r_btn = gr.Button("Replace") |
| with gr.Column(): |
| with gr.Row(): |
| token = gr.Textbox(label="auth") |
| inbut = gr.Button("Update Repo") |
| output_html = gr.HTML("""""") |
| gr.Column(scale=1) |
| with gr.Accordion("Files",open=False): |
| with gr.Tab("Modify"): |
| files=gr.File() |
| with gr.Tab("Download"): |
| with gr.Row(): |
| ss_name = gr.Dropdown(label="Spaces", choices=[],multiselect=True) |
| f_btn=gr.Button() |
| with gr.Row(): |
| a_files=gr.File() |
| z_files=gr.File() |
|
|
| with gr.Row(): |
| r_t=gr.Textbox(max_lines=100) |
| m_t=gr.Textbox(max_lines=100) |
| au=gr.Textbox(visible=False) |
| uu=gr.Textbox(visible=False) |
| |
| def cyc(x): |
| return x,None |
|
|
| f_btn.click(get_f,[r_name,ss_name,token],[a_files,z_files]) |
| r_t.change(cyc,r_t,[m_t,files]) |
| |
| s_btn.click(show_s,r_name,[s_name,ss_name]) |
| s_name.change(show_f,[r_name,s_name],d_app) |
|
|
| l_btn.click(load_space,[r_name,s_name,d_app],[r_t]) |
| r_btn.click(repl,[r_s,r_w,d_app], [m_t,files]) |
| |
| pass_btn.click(checkp,pass_box,[no,yes]) |
| inbut.click(build_space,[token,r_name,s_name,d_app,dd_app],output_html) |
| build.queue(concurrency_count=10).launch() |