File size: 2,366 Bytes
117e709
 
16848a3
117e709
 
2058f6d
117e709
2058f6d
 
 
 
 
 
117e709
2058f6d
117e709
 
 
 
 
96042a5
117e709
2058f6d
117e709
 
 
 
 
2058f6d
 
 
16848a3
 
 
 
 
46048ce
16848a3
2058f6d
117e709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96042a5
 
5f13d80
117e709
 
 
 
 
96042a5
 
117e709
 
 
 
 
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 requests
from huggingface_hub import whoami, hf_hub_download, upload_file
from huggingface_hub.utils import build_hf_headers, hf_raise_for_status

app_script = hf_hub_download(repo_id="whisper-event/whisper-demo", filename="app.py", repo_type="space")

def replace_line(file_name, line_num, text):
    lines = open(file_name, 'r').readlines()
    lines[line_num] = text
    out = open(file_name, 'w')
    out.writelines(lines)
    out.close()

def duplicate(destination_repo, model_id, token):
    try:
        _ = whoami(token)
        # ^ this will throw if token is invalid

        r = requests.post(
            "https://huggingface.co/api/spaces/whisper-event/whisper-demo/duplicate",
            headers=build_hf_headers(token=token),
            json={"repository": destination_repo},
        )
        hf_raise_for_status(r)

        repo_url = r.json().get("url")


        app_script = hf_hub_download(repo_id="whisper-event/whisper-demo", filename="app.py", repo_type="space")
        replace_line(app_script, 7, f"MODEL_NAME = \"{model_id}\"\n")
        upload_file(
            path_or_fileobj=app_script,
            path_in_repo="app.py",
            repo_id=destination_repo,
            repo_type="space",
            token=token
        )

        return (
            f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
            "sp.jpg",
        )

    except Exception as e:
        return (
            f"""
        ### Error 😢😢😢
        
        {e}
        """,
            None,
        )


interface = gr.Interface(
    fn=duplicate,
    inputs=[
        gr.Textbox(placeholder="Destination repository (e.g. username/dst_repo_name)"),
        gr.Textbox(placeholder="Model id (e.g. openai/whisper-small)"),
        gr.Textbox(placeholder="Write access token", type="password"),
    ],
    outputs=[
        gr.Markdown(label="output"),
        gr.Image(show_label=False),
    ],
    title="Build your Whisper demo!",
    description="Build your Whisper demo! You need to specify a write token obtained in https://hf.co/settings/tokens.",
    article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
    allow_flagging="never",
    live=False,
)
interface.launch(enable_queue=True)