osanseviero's picture
Update app.py
46048ce
raw
history blame
2.37 kB
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)