Spaces:
Running
Running
initial commit
Browse files- app.py +98 -0
- config.json +6 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from TTS.api import TTS
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import requests
|
4 |
+
import streamlit as st
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
import json
|
8 |
+
import datetime
|
9 |
+
|
10 |
+
with open('config.json', 'r') as f:
|
11 |
+
config = json.load(f)
|
12 |
+
|
13 |
+
APP_NAME = config['APP_NAME']
|
14 |
+
APP_LOGO = config['APP_LOGO']
|
15 |
+
APP_DESCRIPTION = config['APP_DESCRIPTION']
|
16 |
+
LANGUAGES_URL = config['LANGUAGES_URL']
|
17 |
+
|
18 |
+
def contains_only_ascii(input_string):
|
19 |
+
return all(ord(char) < 128 for char in input_string)
|
20 |
+
|
21 |
+
def get_iso_languages():
|
22 |
+
response = requests.get(LANGUAGES_URL)
|
23 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
24 |
+
|
25 |
+
p_tags = soup.find_all('p')
|
26 |
+
|
27 |
+
iso_language_dict = {}
|
28 |
+
|
29 |
+
for p_tag in p_tags[1:]: # Skipping the first <p> which contains the header
|
30 |
+
parts = p_tag.get_text().split()
|
31 |
+
if len(parts) == 2:
|
32 |
+
iso_code, language_name = parts
|
33 |
+
if contains_only_ascii(language_name):
|
34 |
+
iso_language_dict[language_name] = iso_code
|
35 |
+
|
36 |
+
return iso_language_dict
|
37 |
+
|
38 |
+
def create_temp_file(input_wav):
|
39 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
40 |
+
temp_file.write(input_wav.read())
|
41 |
+
return temp_file
|
42 |
+
|
43 |
+
def remove_temp_file(temp_file):
|
44 |
+
temp_file.close()
|
45 |
+
os.remove(temp_file.name)
|
46 |
+
|
47 |
+
def update_progress(percent, text):
|
48 |
+
progress_bar.progress(percent)
|
49 |
+
status_text.text(text)
|
50 |
+
|
51 |
+
iso_languages = get_iso_languages()
|
52 |
+
languages = list(iso_languages.keys())
|
53 |
+
|
54 |
+
st.set_page_config(page_title=APP_NAME)
|
55 |
+
st.title(APP_NAME)
|
56 |
+
st.image(APP_LOGO, use_column_width=True)
|
57 |
+
st.markdown(APP_DESCRIPTION)
|
58 |
+
|
59 |
+
language = st.selectbox('Select a language', languages)
|
60 |
+
input_wav = st.file_uploader("Upload a WAV file with your voice", type=["wav"])
|
61 |
+
clone_wav = st.file_uploader("Upload a WAV file with voice to clone", type=["wav"])
|
62 |
+
|
63 |
+
if input_wav and clone_wav:
|
64 |
+
progress_bar = st.progress(0)
|
65 |
+
status_text = st.empty()
|
66 |
+
|
67 |
+
current_datetime = datetime.datetime.now()
|
68 |
+
formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H%M%S")
|
69 |
+
output_filename = f"recording_{formatted_datetime}.wav"
|
70 |
+
|
71 |
+
temp_input_file = create_temp_file(input_wav)
|
72 |
+
temp_clone_file = create_temp_file(clone_wav)
|
73 |
+
|
74 |
+
iso_code = iso_languages[language]
|
75 |
+
|
76 |
+
print(f'Language: {language}')
|
77 |
+
|
78 |
+
update_progress(0, 'Loading TTS model...')
|
79 |
+
api = TTS(f"tts_models/{iso_code}/fairseq/vits")
|
80 |
+
|
81 |
+
update_progress(50, 'Generating audio...')
|
82 |
+
api.voice_conversion_to_file(
|
83 |
+
source_wav=temp_input_file.name,
|
84 |
+
target_wav=temp_clone_file.name,
|
85 |
+
file_path=output_filename
|
86 |
+
)
|
87 |
+
|
88 |
+
remove_temp_file(temp_input_file)
|
89 |
+
remove_temp_file(temp_clone_file)
|
90 |
+
|
91 |
+
audio_file = open(output_filename, 'rb')
|
92 |
+
audio_bytes = audio_file.read()
|
93 |
+
|
94 |
+
update_progress(100, 'Audio generated successfully!')
|
95 |
+
|
96 |
+
st.audio(audio_bytes, format='audio/wav')
|
97 |
+
|
98 |
+
st.download_button('Download WAV', data=audio_bytes, file_name='output.wav')
|
config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"APP_NAME": "TTS-Voice-Cloner",
|
3 |
+
"APP_LOGO": "https://raw.githubusercontent.com/coqui-ai/TTS/main/images/coqui-log-green-TTS.png",
|
4 |
+
"APP_DESCRIPTION": "Simple app that uses [Coqui TTS](https://github.com/coqui-ai/TTS) library to clone your voice!",
|
5 |
+
"LANGUAGES_URL": "https://dl.fbaipublicfiles.com/mms/tts/all-tts-languages.html"
|
6 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
beautifulsoup4
|
2 |
+
TTS
|