Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,11 +14,7 @@ def check_api_key(api_key):
|
|
| 14 |
if r.status_code != 200:
|
| 15 |
return None
|
| 16 |
sub = r.json().get("subscription", {})
|
| 17 |
-
|
| 18 |
-
used = sub.get("character_count")
|
| 19 |
-
if limit is None or used is None:
|
| 20 |
-
return None
|
| 21 |
-
return limit - used
|
| 22 |
except:
|
| 23 |
return None
|
| 24 |
|
|
@@ -37,14 +33,15 @@ def split_text(text, max_len=200):
|
|
| 37 |
return out
|
| 38 |
|
| 39 |
|
| 40 |
-
# ================= TTS =================
|
| 41 |
def tts(text, api_key, voice_id, model):
|
| 42 |
time.sleep(random.uniform(0.8, 1.5))
|
| 43 |
r = requests.post(
|
| 44 |
f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
|
| 45 |
headers={
|
| 46 |
"xi-api-key": api_key,
|
| 47 |
-
"Content-Type": "application/json"
|
|
|
|
| 48 |
},
|
| 49 |
json={
|
| 50 |
"text": text,
|
|
@@ -57,9 +54,9 @@ def tts(text, api_key, voice_id, model):
|
|
| 57 |
"use_speaker_boost": True
|
| 58 |
}
|
| 59 |
},
|
| 60 |
-
timeout=
|
| 61 |
)
|
| 62 |
-
if r.status_code == 200:
|
| 63 |
return r.content
|
| 64 |
return None
|
| 65 |
|
|
@@ -81,32 +78,34 @@ def create_srt(folder, texts):
|
|
| 81 |
files = natsort.natsorted(f for f in os.listdir(folder) if f.startswith("voice_"))
|
| 82 |
for i, (f, txt) in enumerate(zip(files, texts), 1):
|
| 83 |
a = AudioSegment.from_file(os.path.join(folder, f))
|
| 84 |
-
lines += [
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
t += len(a) + 500
|
| 88 |
with open(os.path.join(folder, "output_full.srt"), "w", encoding="utf-8") as f:
|
| 89 |
f.write("\n".join(lines))
|
| 90 |
|
| 91 |
|
| 92 |
-
# ================= RUN
|
| 93 |
def run(api_text, api_file, voice_id, text, model, fmt):
|
| 94 |
keys = api_file.decode().splitlines() if api_file else api_text.splitlines()
|
| 95 |
keys = [k.strip() for k in keys if k.strip()]
|
| 96 |
|
| 97 |
table = "| API KEY | CREDIT |\n|---|---|\n"
|
| 98 |
-
|
| 99 |
|
| 100 |
for k in keys:
|
| 101 |
rem = check_api_key(k)
|
| 102 |
show = f"{k[:6]}...{k[-4:]}"
|
| 103 |
if rem and rem > 600:
|
| 104 |
-
|
| 105 |
table += f"| {show} | {rem} |\n"
|
| 106 |
else:
|
| 107 |
table += f"| {show} | ❌ |\n"
|
| 108 |
|
| 109 |
-
if not
|
| 110 |
return "❌ Không có API key >600", None, None, table
|
| 111 |
|
| 112 |
texts = split_text(text)
|
|
@@ -114,18 +113,15 @@ def run(api_text, api_file, voice_id, text, model, fmt):
|
|
| 114 |
for f in os.listdir("voices"):
|
| 115 |
os.remove(os.path.join("voices", f))
|
| 116 |
|
| 117 |
-
# 🔥 LOGIC ĐÚNG: mỗi đoạn thử TẤT CẢ key
|
| 118 |
for i, t in enumerate(texts):
|
| 119 |
success = False
|
| 120 |
-
|
| 121 |
-
for key in valid_keys:
|
| 122 |
audio = tts(t, key, voice_id, model)
|
| 123 |
if audio:
|
| 124 |
with open(f"voices/voice_{i+1:03d}.{fmt}", "wb") as f:
|
| 125 |
f.write(audio)
|
| 126 |
success = True
|
| 127 |
break
|
| 128 |
-
|
| 129 |
if not success:
|
| 130 |
return "❌ Không API key nào tạo được audio", None, None, table
|
| 131 |
|
|
@@ -143,7 +139,7 @@ def run(api_text, api_file, voice_id, text, model, fmt):
|
|
| 143 |
|
| 144 |
# ================= UI =================
|
| 145 |
with gr.Blocks() as app:
|
| 146 |
-
gr.Markdown("## 🔊 ElevenLabs TTS –
|
| 147 |
|
| 148 |
api_text = gr.Textbox(lines=4, label="API key")
|
| 149 |
api_file = gr.File(type="binary", label="Upload API file")
|
|
|
|
| 14 |
if r.status_code != 200:
|
| 15 |
return None
|
| 16 |
sub = r.json().get("subscription", {})
|
| 17 |
+
return sub.get("character_limit", 0) - sub.get("character_count", 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
except:
|
| 19 |
return None
|
| 20 |
|
|
|
|
| 33 |
return out
|
| 34 |
|
| 35 |
|
| 36 |
+
# ================= TTS (FIX QUAN TRỌNG) =================
|
| 37 |
def tts(text, api_key, voice_id, model):
|
| 38 |
time.sleep(random.uniform(0.8, 1.5))
|
| 39 |
r = requests.post(
|
| 40 |
f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
|
| 41 |
headers={
|
| 42 |
"xi-api-key": api_key,
|
| 43 |
+
"Content-Type": "application/json",
|
| 44 |
+
"Accept": "audio/mpeg" # 🔥 BẮT BUỘC
|
| 45 |
},
|
| 46 |
json={
|
| 47 |
"text": text,
|
|
|
|
| 54 |
"use_speaker_boost": True
|
| 55 |
}
|
| 56 |
},
|
| 57 |
+
timeout=60
|
| 58 |
)
|
| 59 |
+
if r.status_code == 200 and len(r.content) > 1000:
|
| 60 |
return r.content
|
| 61 |
return None
|
| 62 |
|
|
|
|
| 78 |
files = natsort.natsorted(f for f in os.listdir(folder) if f.startswith("voice_"))
|
| 79 |
for i, (f, txt) in enumerate(zip(files, texts), 1):
|
| 80 |
a = AudioSegment.from_file(os.path.join(folder, f))
|
| 81 |
+
lines += [
|
| 82 |
+
str(i),
|
| 83 |
+
f"00:00:{t//1000:02},{t%1000:03} --> 00:00:{(t+len(a))//1000:02},{(t+len(a))%1000:03}",
|
| 84 |
+
txt, ""
|
| 85 |
+
]
|
| 86 |
t += len(a) + 500
|
| 87 |
with open(os.path.join(folder, "output_full.srt"), "w", encoding="utf-8") as f:
|
| 88 |
f.write("\n".join(lines))
|
| 89 |
|
| 90 |
|
| 91 |
+
# ================= RUN =================
|
| 92 |
def run(api_text, api_file, voice_id, text, model, fmt):
|
| 93 |
keys = api_file.decode().splitlines() if api_file else api_text.splitlines()
|
| 94 |
keys = [k.strip() for k in keys if k.strip()]
|
| 95 |
|
| 96 |
table = "| API KEY | CREDIT |\n|---|---|\n"
|
| 97 |
+
valid = []
|
| 98 |
|
| 99 |
for k in keys:
|
| 100 |
rem = check_api_key(k)
|
| 101 |
show = f"{k[:6]}...{k[-4:]}"
|
| 102 |
if rem and rem > 600:
|
| 103 |
+
valid.append(k)
|
| 104 |
table += f"| {show} | {rem} |\n"
|
| 105 |
else:
|
| 106 |
table += f"| {show} | ❌ |\n"
|
| 107 |
|
| 108 |
+
if not valid:
|
| 109 |
return "❌ Không có API key >600", None, None, table
|
| 110 |
|
| 111 |
texts = split_text(text)
|
|
|
|
| 113 |
for f in os.listdir("voices"):
|
| 114 |
os.remove(os.path.join("voices", f))
|
| 115 |
|
|
|
|
| 116 |
for i, t in enumerate(texts):
|
| 117 |
success = False
|
| 118 |
+
for key in valid:
|
|
|
|
| 119 |
audio = tts(t, key, voice_id, model)
|
| 120 |
if audio:
|
| 121 |
with open(f"voices/voice_{i+1:03d}.{fmt}", "wb") as f:
|
| 122 |
f.write(audio)
|
| 123 |
success = True
|
| 124 |
break
|
|
|
|
| 125 |
if not success:
|
| 126 |
return "❌ Không API key nào tạo được audio", None, None, table
|
| 127 |
|
|
|
|
| 139 |
|
| 140 |
# ================= UI =================
|
| 141 |
with gr.Blocks() as app:
|
| 142 |
+
gr.Markdown("## 🔊 ElevenLabs TTS – FIX CHUẨN HF")
|
| 143 |
|
| 144 |
api_text = gr.Textbox(lines=4, label="API key")
|
| 145 |
api_file = gr.File(type="binary", label="Upload API file")
|