John6666's picture
Upload 3 files
616078c verified
import gradio as gr
import spaces
import os
import re
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
def is_repo_exists(repo_id):
from huggingface_hub import HfApi
api = HfApi()
try:
if api.repo_exists(repo_id=repo_id): return True
else: return False
except Exception as e:
print(f"Error: Failed to connect {repo_id}. ")
print(e)
return True # for safe
all_repos = []
def extract_hf_reponame(line: str):
d = {"line": line, "repos": [], "exist": True, "unique": True}
if line.strip().startswith("#"): return d
m = re.findall(r'[/,\s\"\']?([^/,\s\"\']+/[^/,\s\"\']+)[/,\s\"\']?', line, re.DOTALL)
for s in m:
if s in all_repos: d["unique"] = False
if not is_repo_exists(s): d["exist"] = False
d["repos"].append(s)
all_repos.append(s)
return d
def extract_hf_reponames(repos: str, progress=gr.Progress(track_tqdm=True)):
from tqdm import tqdm
global all_repos
all_repos = []
repojson = []
for line in tqdm(repos.splitlines(), desc="[Processing]"):
repojson.append(extract_hf_reponame(line))
output = ""
error_md = "## Error:\n"
for d in repojson:
if not d["exist"] or not ["unique"]: output += "# "
output += d["line"].removesuffix("\n")
if not d["exist"]:
output += " # repo doesn't exist."
error_md += f"- Repo doesn't exist in '{d['line']}'\n"
if not d["unique"]:
output += " # duplicate elements."
error_md += f"- Duplicate elements in '{d['line']}'\n"
output += "\n"
return output, error_md
css = """"""
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
with gr.Column():
repos_text = gr.Textbox(label="Paste model list", value="", lines=8)
#repos_code = gr.Code(label="Paste model list in python code", language="python", value="", lines=8, interactive=True)
run_button = gr.Button(value="Submit")
#debug_json = gr.JSON(value=[{}])
error_md = gr.Markdown()
output_code = gr.Code(label="Output", language="python", value="", lines=8, interactive=True)
gr.DuplicateButton(value="Duplicate Space")
gr.on(
triggers=[run_button.click],
fn=extract_hf_reponames,
inputs=[repos_text],
outputs=[output_code, error_md],
)
demo.queue().launch()