Spaces:
Sleeping
Sleeping
File size: 6,960 Bytes
f6c62cd 431ceed ef15d8b 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed f6c62cd 431ceed |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import requests
import gradio as gr
import os
import time
# νκ²½ λ³μ μ΄κΈ°ν λ° κ²μ¦
def initialize_environment():
private_space_url = os.getenv("PRIVATE_SPACE_URL")
hf_token = os.getenv("HF_TOKEN")
if not private_space_url:
raise EnvironmentError("PRIVATE_SPACE_URL νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
if not hf_token:
raise EnvironmentError("HF_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
return private_space_url, hf_token
PRIVATE_SPACE_URL, HF_TOKEN = initialize_environment()
# Base URLλ‘ GET μμ² λ³΄λ΄λ ν¨μ
def test_base_url():
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
print(f"[DEBUG] Sending GET request to base URL: {PRIVATE_SPACE_URL}")
print(f"[DEBUG] HF Token: {HF_TOKEN}")
try:
response = requests.get(PRIVATE_SPACE_URL, headers=headers)
print(f"[DEBUG] Base URL Response Status Code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"[ERROR] Base URL μμ² μ€ μ€λ₯ λ°μ: {e}")
# Actor ID μ‘°ν ν¨μ
def fetch_actor_ids():
url = f"{PRIVATE_SPACE_URL}/api/actor"
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
# Actor 리μ€νΈμμ μ΄λ¦κ³Ό ID μΆμΆ
return {
actor["name"]["en"]: actor["actor_id"]
for actor in data.get("result", [])
}
except requests.exceptions.RequestException as e:
print(f"[ERROR] Actor ID μ‘°ν μ€ μ€λ₯ λ°μ: {e}")
return {}
# URL μΉν ν¨μ
def replace_speak_url(url):
if not url:
return None
return url.replace("https://create-test.icepeak.ai", PRIVATE_SPACE_URL)
# μμ± μμ± ν¨μ
def generate_speech(text, actor_name, lang="en", speed_x=1.0, volume=100):
actor_ids = fetch_actor_ids()
if not actor_ids:
return "Actor λͺ©λ‘μ κ°μ Έμ€λ λ° μ€ν¨νμ΅λλ€.", None
actor_id = actor_ids.get(actor_name)
if not actor_id:
return "μ νν Actorλ₯Ό μ°Ύμ μ μμ΅λλ€.", None
url = f"{PRIVATE_SPACE_URL}/api/speak"
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"actor_id": actor_id,
"lang": lang,
"text": text,
"speed_x": speed_x,
"volume": volume,
"tts_mode": "actor"
}
print(f"[DEBUG] Sending speech generation request to: {url}")
print(f"[DEBUG] Payload: {payload}")
try:
response = requests.post(url, json=payload, headers=headers)
print(f"[DEBUG] Response Status Code: {response.status_code}")
print(f"[DEBUG] Response Content: {response.text}")
response.raise_for_status()
data = response.json()
print(f"[DEBUG] Response Data: {data}")
speak_url = replace_speak_url(data.get("result", {}).get("speak_url"))
if not speak_url:
return "μ€λ₯: μ ν¨ν speak_urlμ λ°ννμ§ μμμ΅λλ€.", None
# Polling for audio generation completion
audio_url = poll_audio_url(speak_url)
if not audio_url:
return "μ€λ₯: μ€λμ€ λ€μ΄λ‘λ URLμ κ°μ Έμ¬ μ μμ΅λλ€.", None
# Download the audio file
audio_content = download_audio(audio_url)
if not audio_content:
return "μ€λ₯: μμ± νμΌμ λ€μ΄λ‘λν μ μμ΅λλ€.", None
# Save the audio content to a temporary file
audio_file_path = "temp_audio.wav"
with open(audio_file_path, "wb") as audio_file:
audio_file.write(audio_content)
# Print debug information
file_size = os.path.getsize(audio_file_path)
print(f"[DEBUG] Saved audio file: {audio_file_path}, Size: {file_size} bytes")
return "μμ±μ΄ μ±κ³΅μ μΌλ‘ μμ±λμμ΅λλ€.", audio_file_path
except requests.exceptions.RequestException as e:
print(f"[ERROR] μμ± μμ± μ€ μ€λ₯ λ°μ: {e}")
return f"μ€λ₯: {str(e)}", None
# Polling ν¨μ
def poll_audio_url(speak_url, timeout=30, interval=2):
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
start_time = time.time()
while time.time() - start_time < timeout:
try:
print(f"[DEBUG] Polling speak URL: {speak_url}")
response = requests.get(speak_url, headers=headers)
response.raise_for_status()
data = response.json()
status = data.get("result", {}).get("status")
if status == "done":
audio_info = data.get("result", {}).get("audio", {})
audio_url = replace_speak_url(audio_info.get("url"))
print(f"[DEBUG] Audio URL: {audio_url}")
return audio_url
except requests.exceptions.RequestException as e:
print(f"[DEBUG] Polling attempt failed: {e}")
time.sleep(interval)
print("[ERROR] Polling timed out.")
return None
# Audio λ€μ΄λ‘λ ν¨μ
def download_audio(audio_url):
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
try:
print(f"[DEBUG] Downloading audio from: {audio_url}")
response = requests.get(audio_url, headers=headers, stream=True)
response.raise_for_status()
return response.content
except requests.exceptions.RequestException as e:
print(f"[ERROR] Audio λ€μ΄λ‘λ μ€ μ€λ₯ λ°μ: {e}")
return None
# Gradio μΈν°νμ΄μ€ ν¨μ
def interface_function(text, actor_name, lang, speed_x, volume):
result_message, audio_file_path = generate_speech(text, actor_name, lang, speed_x, volume)
if audio_file_path:
return result_message, audio_file_path
return result_message, None
# Fetch actors to populate dropdown
actors = fetch_actor_ids()
actor_names = list(actors.keys())
if not actor_names:
print("[WARNING] Actor λͺ©λ‘μ κ°μ Έμ¬ μ μμ΄ κΈ°λ³Έ μ΅μ
μ΄ μ€μ λ©λλ€.")
# Base URL ν
μ€νΈ
test_base_url()
# Gradio μΈν°νμ΄μ€ μμ±
interface = gr.Interface(
fn=interface_function,
inputs=[
gr.Textbox(label="ν
μ€νΈ μ
λ ₯", placeholder="μ¬κΈ°μ μμ±μ ν©μ±ν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ."),
gr.Dropdown(choices=actor_names, label="Actor μ ν", interactive=True),
gr.Dropdown(choices=["en", "ko"], value="en", label="μΈμ΄"),
gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="μλ"),
gr.Slider(minimum=50, maximum=200, step=10, value=100, label="λ³Όλ₯¨"),
],
outputs=[
gr.Textbox(label="κ²°κ³Ό λ©μμ§"),
gr.Audio(label="μμ±λ μμ±"),
],
title="μμ± μμ± λ°λͺ¨",
description="쿼리 νλΌλ―Έν°κ° μ μ©λ Actor μ‘°ν ν μμ±μ μμ±ν©λλ€.",
)
if __name__ == "__main__":
interface.launch()
|