Spaces:
Sleeping
Sleeping
Sunghyun Jun
commited on
Commit
Β·
431ceed
1
Parent(s):
ef15d8b
Update generate_speech demo
Browse files
app.py
CHANGED
@@ -1,70 +1,189 @@
|
|
1 |
-
import gradio as gr
|
2 |
import requests
|
|
|
3 |
import os
|
4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
if not
|
13 |
-
return "
|
14 |
|
|
|
|
|
|
|
|
|
|
|
15 |
headers = {
|
16 |
-
"Authorization": f"Bearer {HF_TOKEN}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
}
|
18 |
|
|
|
|
|
19 |
try:
|
20 |
-
response = requests.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
data = response.json()
|
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 |
if __name__ == "__main__":
|
70 |
-
|
|
|
|
|
1 |
import requests
|
2 |
+
import gradio as gr
|
3 |
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
# νκ²½ λ³μ μ΄κΈ°ν λ° κ²μ¦
|
7 |
+
def initialize_environment():
|
8 |
+
private_space_url = os.getenv("PRIVATE_SPACE_URL")
|
9 |
+
hf_token = os.getenv("HF_TOKEN")
|
10 |
+
|
11 |
+
if not private_space_url:
|
12 |
+
raise EnvironmentError("PRIVATE_SPACE_URL νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
13 |
+
if not hf_token:
|
14 |
+
raise EnvironmentError("HF_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
15 |
+
|
16 |
+
return private_space_url, hf_token
|
17 |
+
|
18 |
+
PRIVATE_SPACE_URL, HF_TOKEN = initialize_environment()
|
19 |
+
|
20 |
+
# Base URLλ‘ GET μμ² λ³΄λ΄λ ν¨μ
|
21 |
+
def test_base_url():
|
22 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
23 |
+
print(f"[DEBUG] Sending GET request to base URL: {PRIVATE_SPACE_URL}")
|
24 |
+
print(f"[DEBUG] HF Token: {HF_TOKEN}")
|
25 |
+
try:
|
26 |
+
response = requests.get(PRIVATE_SPACE_URL, headers=headers)
|
27 |
+
print(f"[DEBUG] Base URL Response Status Code: {response.status_code}")
|
28 |
+
except requests.exceptions.RequestException as e:
|
29 |
+
print(f"[ERROR] Base URL μμ² μ€ μ€λ₯ λ°μ: {e}")
|
30 |
+
|
31 |
+
# Actor ID μ‘°ν ν¨μ
|
32 |
+
def fetch_actor_ids():
|
33 |
+
url = f"{PRIVATE_SPACE_URL}/api/actor"
|
34 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
35 |
+
try:
|
36 |
+
response = requests.get(url, headers=headers)
|
37 |
+
response.raise_for_status()
|
38 |
+
data = response.json()
|
39 |
+
|
40 |
+
# Actor 리μ€νΈμμ μ΄λ¦κ³Ό ID μΆμΆ
|
41 |
+
return {
|
42 |
+
actor["name"]["en"]: actor["actor_id"]
|
43 |
+
for actor in data.get("result", [])
|
44 |
+
}
|
45 |
+
except requests.exceptions.RequestException as e:
|
46 |
+
print(f"[ERROR] Actor ID μ‘°ν μ€ μ€λ₯ λ°μ: {e}")
|
47 |
+
return {}
|
48 |
|
49 |
+
# URL μΉν ν¨μ
|
50 |
+
def replace_speak_url(url):
|
51 |
+
if not url:
|
52 |
+
return None
|
53 |
+
return url.replace("https://create-test.icepeak.ai", PRIVATE_SPACE_URL)
|
54 |
|
55 |
+
# μμ± μμ± ν¨μ
|
56 |
+
def generate_speech(text, actor_name, lang="en", speed_x=1.0, volume=100):
|
57 |
+
actor_ids = fetch_actor_ids()
|
58 |
+
if not actor_ids:
|
59 |
+
return "Actor λͺ©λ‘μ κ°μ Έμ€λ λ° μ€ν¨νμ΅λλ€.", None
|
60 |
|
61 |
+
actor_id = actor_ids.get(actor_name)
|
62 |
+
if not actor_id:
|
63 |
+
return "μ νν Actorλ₯Ό μ°Ύμ μ μμ΅λλ€.", None
|
64 |
+
|
65 |
+
url = f"{PRIVATE_SPACE_URL}/api/speak"
|
66 |
headers = {
|
67 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
68 |
+
"Content-Type": "application/json"
|
69 |
+
}
|
70 |
+
payload = {
|
71 |
+
"actor_id": actor_id,
|
72 |
+
"lang": lang,
|
73 |
+
"text": text,
|
74 |
+
"speed_x": speed_x,
|
75 |
+
"volume": volume,
|
76 |
+
"tts_mode": "actor"
|
77 |
}
|
78 |
|
79 |
+
print(f"[DEBUG] Sending speech generation request to: {url}")
|
80 |
+
print(f"[DEBUG] Payload: {payload}")
|
81 |
try:
|
82 |
+
response = requests.post(url, json=payload, headers=headers)
|
83 |
+
print(f"[DEBUG] Response Status Code: {response.status_code}")
|
84 |
+
print(f"[DEBUG] Response Content: {response.text}")
|
85 |
+
response.raise_for_status()
|
86 |
+
data = response.json()
|
87 |
+
print(f"[DEBUG] Response Data: {data}")
|
88 |
+
|
89 |
+
speak_url = replace_speak_url(data.get("result", {}).get("speak_url"))
|
90 |
+
if not speak_url:
|
91 |
+
return "μ€λ₯: μ ν¨ν speak_urlμ λ°ννμ§ μμμ΅λλ€.", None
|
92 |
|
93 |
+
# Polling for audio generation completion
|
94 |
+
audio_url = poll_audio_url(speak_url)
|
95 |
+
if not audio_url:
|
96 |
+
return "μ€λ₯: μ€λμ€ λ€μ΄λ‘λ URLμ κ°μ Έμ¬ μ μμ΅λλ€.", None
|
97 |
+
|
98 |
+
# Download the audio file
|
99 |
+
audio_content = download_audio(audio_url)
|
100 |
+
if not audio_content:
|
101 |
+
return "μ€λ₯: μμ± νμΌμ λ€μ΄λ‘λν μ μμ΅λλ€.", None
|
102 |
+
|
103 |
+
# Save the audio content to a temporary file
|
104 |
+
audio_file_path = "temp_audio.wav"
|
105 |
+
with open(audio_file_path, "wb") as audio_file:
|
106 |
+
audio_file.write(audio_content)
|
107 |
+
|
108 |
+
# Print debug information
|
109 |
+
file_size = os.path.getsize(audio_file_path)
|
110 |
+
print(f"[DEBUG] Saved audio file: {audio_file_path}, Size: {file_size} bytes")
|
111 |
+
|
112 |
+
return "μμ±μ΄ μ±κ³΅μ μΌλ‘ μμ±λμμ΅λλ€.", audio_file_path
|
113 |
+
except requests.exceptions.RequestException as e:
|
114 |
+
print(f"[ERROR] μμ± μμ± μ€ μ€λ₯ λ°μ: {e}")
|
115 |
+
return f"μ€λ₯: {str(e)}", None
|
116 |
+
|
117 |
+
# Polling ν¨μ
|
118 |
+
def poll_audio_url(speak_url, timeout=30, interval=2):
|
119 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
120 |
+
start_time = time.time()
|
121 |
+
|
122 |
+
while time.time() - start_time < timeout:
|
123 |
+
try:
|
124 |
+
print(f"[DEBUG] Polling speak URL: {speak_url}")
|
125 |
+
response = requests.get(speak_url, headers=headers)
|
126 |
+
response.raise_for_status()
|
127 |
data = response.json()
|
128 |
+
status = data.get("result", {}).get("status")
|
129 |
+
if status == "done":
|
130 |
+
audio_info = data.get("result", {}).get("audio", {})
|
131 |
+
audio_url = replace_speak_url(audio_info.get("url"))
|
132 |
+
print(f"[DEBUG] Audio URL: {audio_url}")
|
133 |
+
return audio_url
|
134 |
+
except requests.exceptions.RequestException as e:
|
135 |
+
print(f"[DEBUG] Polling attempt failed: {e}")
|
136 |
+
time.sleep(interval)
|
137 |
+
|
138 |
+
print("[ERROR] Polling timed out.")
|
139 |
+
return None
|
140 |
+
|
141 |
+
# Audio λ€μ΄λ‘λ ν¨μ
|
142 |
+
def download_audio(audio_url):
|
143 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
144 |
+
try:
|
145 |
+
print(f"[DEBUG] Downloading audio from: {audio_url}")
|
146 |
+
response = requests.get(audio_url, headers=headers, stream=True)
|
147 |
+
response.raise_for_status()
|
148 |
+
return response.content
|
149 |
+
except requests.exceptions.RequestException as e:
|
150 |
+
print(f"[ERROR] Audio λ€μ΄λ‘λ μ€ μ€λ₯ λ°μ: {e}")
|
151 |
+
return None
|
152 |
+
|
153 |
+
# Gradio μΈν°νμ΄μ€ ν¨μ
|
154 |
+
def interface_function(text, actor_name, lang, speed_x, volume):
|
155 |
+
result_message, audio_file_path = generate_speech(text, actor_name, lang, speed_x, volume)
|
156 |
+
if audio_file_path:
|
157 |
+
return result_message, audio_file_path
|
158 |
+
return result_message, None
|
159 |
+
|
160 |
+
# Fetch actors to populate dropdown
|
161 |
+
actors = fetch_actor_ids()
|
162 |
+
actor_names = list(actors.keys())
|
163 |
+
|
164 |
+
if not actor_names:
|
165 |
+
print("[WARNING] Actor λͺ©λ‘μ κ°μ Έμ¬ μ μμ΄ κΈ°λ³Έ μ΅μ
μ΄ μ€μ λ©λλ€.")
|
166 |
+
|
167 |
+
# Base URL ν
μ€νΈ
|
168 |
+
test_base_url()
|
169 |
+
|
170 |
+
# Gradio μΈν°νμ΄μ€ μμ±
|
171 |
+
interface = gr.Interface(
|
172 |
+
fn=interface_function,
|
173 |
+
inputs=[
|
174 |
+
gr.Textbox(label="ν
μ€νΈ μ
λ ₯", placeholder="μ¬κΈ°μ μμ±μ ν©μ±ν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ."),
|
175 |
+
gr.Dropdown(choices=actor_names, label="Actor μ ν", interactive=True),
|
176 |
+
gr.Dropdown(choices=["en", "ko"], value="en", label="μΈμ΄"),
|
177 |
+
gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="μλ"),
|
178 |
+
gr.Slider(minimum=50, maximum=200, step=10, value=100, label="λ³Όλ₯¨"),
|
179 |
+
],
|
180 |
+
outputs=[
|
181 |
+
gr.Textbox(label="κ²°κ³Ό λ©μμ§"),
|
182 |
+
gr.Audio(label="μμ±λ μμ±"),
|
183 |
+
],
|
184 |
+
title="μμ± μμ± λ°λͺ¨",
|
185 |
+
description="쿼리 νλΌλ―Έν°κ° μ μ©λ Actor μ‘°ν ν μμ±μ μμ±ν©λλ€.",
|
186 |
)
|
187 |
|
188 |
if __name__ == "__main__":
|
189 |
+
interface.launch()
|