File size: 2,488 Bytes
616078c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()