File size: 2,446 Bytes
6090f88
f4ce499
656b8f3
6090f88
 
13ebfc4
 
 
 
 
 
 
 
656b8f3
 
 
6090f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593dc7f
6090f88
 
 
 
3c459c5
 
6090f88
 
 
 
7b22fab
6090f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import time
import os, shutil, requests, zipfile, io, threading
from huggingface_hub import HfApi
SECRET_KEY = os.getenv('SECRET', None)
def restart_space():
    api = HfApi(
        token=os.getenv('HF_TOKEN')
    )
    while True:
        time.sleep(60 * 60 * 3)
        print("Restarting space")
        api.restart_space(repo_id='mrfakename/sync_f5')
restart_thread = threading.Thread(target=restart_space)
restart_thread.daemon = True
restart_thread.start()
def refresh(token):
    if SECRET_KEY and token == SECRET_KEY:
        print("Clone repo")
        try:
            shutil.rmtree("repo")
        except:
            pass
        url = "https://github.com/SWivid/F5-TTS/archive/refs/heads/main.zip"
        response = requests.get(url)
        zip_file = io.BytesIO(response.content)
        repo_folder = "repo"
        os.makedirs(repo_folder)
        with zipfile.ZipFile(zip_file, 'r') as zip_ref:
            zip_ref.extractall(repo_folder)
        
        os.rename("repo/F5-TTS-main/src/f5_tts/infer/infer_gradio.py", "repo/F5-TTS-main/app.py")
        os.unlink("repo/F5-TTS-main/.gitignore")
        os.rename("repo/F5-TTS-main/README.md", "repo/F5-TTS-main/README_REPO.md")
        with open("repo/F5-TTS-main/README.md", "w") as f, open("README_TEMPLATE.md", "r") as template:
            f.write(template.read())
        with open("repo/F5-TTS-main/requirements.txt", "w") as f, open("REQUIREMENTS_TEMPLATE.txt", "r") as template:
            f.write(template.read())
        api = HfApi(token=os.getenv('HF_TOKEN'))
        print("Upload")
        api.upload_folder(
            folder_path="repo/F5-TTS-main",
            repo_id="mrfakename/E2-F5-TTS",
            repo_type="space",
            commit_message="Sync from GitHub repo",
            commit_description="This Space is synced from the GitHub repo: https://github.com/SWivid/F5-TTS. Please submit contributions to the Space there"
        )
        try:
            shutil.rmtree("repo")
        except:
            pass
        return "Synced"
    else:
        raise gr.Error("Invalid token")
    

with gr.Blocks() as demo:
    gr.Markdown("This Space is to sync the F5-TTS HF demo with the GitHub repo.")
    token = gr.Textbox(label="Password", info="Enter password to sync demo")
    btn = gr.Button("Refresh")
    out = gr.Markdown("Press Refresh to sync")
    btn.click(refresh, inputs=token, outputs=out)

demo.queue().launch()