Download app.py from xp3857/mod: direct link, hf CLI and curl.
- Browser
- Download file 9.05 kB
-
https://huggingface.co/spaces/xp3857/mod/resolve/main/app.py
- Command line
-
hf download hf://spaces/xp3857/mod/app.py
-
curl -L -o app.py https://huggingface.co/spaces/xp3857/mod/resolve/main/app.py
9.05 kB
| 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}') | |
| #token_self = os.environ['HF_TOKEN'] | |
| 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_s_d(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])) | |
| 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() | |
| o_file = open(f'{uid}/mod_{d_app}','w') | |
| o_file.writelines(r.text) | |
| return r.text,r.text | |
| def load_space_d(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{d_app}','w') | |
| file.writelines(r.text) | |
| file.close() | |
| o_file = open(f'{uid}/mod_d_{d_app}','w') | |
| o_file.writelines(r.text) | |
| return r.text | |
| def upd(x,d_app): | |
| file = open(f'{uid}/{d_app}','w') | |
| file.writelines(x) | |
| file.close() | |
| o_file = open(f'{uid}/mod_{d_app}','w') | |
| o_file.writelines(x) | |
| return x | |
| 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,priv): | |
| if token==None or token=="": | |
| token = token_self | |
| else: | |
| token = token | |
| pass | |
| if dd_app=="" or dd_app==None: | |
| dd_app=d_app | |
| if rd_name=="" or rd_name==None: | |
| rd_name=r_name | |
| if sd_name=="" or sd_name==None: | |
| 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", | |
| ) | |
| if priv==True: | |
| api.update_repo_visibility(repo_id=repo_name,private=True,repo_type="space") | |
| #os.remove(f"{uid}at.py") | |
| 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=[]) | |
| s_btn = gr.Button("Show") | |
| l_btn = gr.Button("Load") | |
| with gr.Column(): | |
| rd_name = gr.Textbox(label="Dest Repo") | |
| sd_name = gr.Dropdown(label="Spaces", choices=[]) | |
| dd_app = gr.Dropdown(label="Files", choices=[]) | |
| sd_btn = gr.Button("Show") | |
| ld_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(): | |
| with gr.Column(scale=3): | |
| token = gr.Textbox(label="auth") | |
| inbut = gr.Button("Update Repo") | |
| priv = gr.Checkbox(label="Private",value=True) | |
| 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(): | |
| rm_but=gr.Button("Update Source") | |
| with gr.Tab("Source"): | |
| with gr.Row(): | |
| r_t=gr.Textbox(lines=30,max_lines=100) | |
| m_t=gr.Textbox(lines=30,max_lines=100) | |
| with gr.Tab("Mod Source"): | |
| with gr.Row(): | |
| rm_t=gr.Textbox(lines=30,max_lines=100,interactive=True) | |
| mm_t=gr.Textbox(lins=30,max_lines=100) | |
| with gr.Tab("Dest"): | |
| with gr.Row(): | |
| rd_t=gr.Textbox(lines=30,max_lines=100) | |
| md_t=gr.Textbox(lins=30,max_lines=100) | |
| au=gr.Textbox(visible=False) | |
| uu=gr.Textbox(visible=False) | |
| def cyc(x): | |
| return x,None | |
| rm_but.click(upd,[rm_t,d_app],[r_t]) | |
| f_btn.click(get_f,[r_name,ss_name,token],[a_files,z_files]) | |
| r_t.change(cyc,r_t,[m_t,files]) | |
| sd_btn.click(show_s_d,rd_name,[sd_name]) | |
| sd_name.change(show_f,[rd_name,sd_name],dd_app) | |
| ld_btn.click(load_space_d,[rd_name,sd_name,dd_app],[rd_t]) | |
| 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,rm_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,rd_name,sd_name,dd_app,priv],output_html) | |
| build.queue(concurrency_count=10).launch() |