mr-don88 commited on
Commit
fe2aaa9
·
verified ·
1 Parent(s): d3c917b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
app.py CHANGED
@@ -3,7 +3,7 @@ import os, re, time, random, zipfile, requests, natsort
3
  import gradio as gr
4
  from pydub import AudioSegment
5
 
6
- # ================= API CHECK (GIỐNG COLAB) =================
7
  def check_api_key(api_key):
8
  try:
9
  r = requests.get(
@@ -12,20 +12,15 @@ def check_api_key(api_key):
12
  timeout=10
13
  )
14
  if r.status_code != 200:
15
- return {"valid": False}
16
-
17
  sub = r.json().get("subscription", {})
18
  limit = sub.get("character_limit")
19
  used = sub.get("character_count")
20
  if limit is None or used is None:
21
- return {"valid": False}
22
-
23
- return {
24
- "valid": True,
25
- "remaining": limit - used
26
- }
27
  except:
28
- return {"valid": False}
29
 
30
 
31
  # ================= TEXT =================
@@ -44,6 +39,7 @@ def split_text(text, max_len=200):
44
 
45
  # ================= TTS =================
46
  def tts(text, api_key, voice_id, model):
 
47
  r = requests.post(
48
  f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
49
  headers={
@@ -93,24 +89,24 @@ def create_srt(folder, texts):
93
  f.write("\n".join(lines))
94
 
95
 
96
- # ================= RUN =================
97
  def run(api_text, api_file, voice_id, text, model, fmt):
98
  keys = api_file.decode().splitlines() if api_file else api_text.splitlines()
99
  keys = [k.strip() for k in keys if k.strip()]
100
 
101
- table = "| API KEY | REMAIN |\n|---|---|\n"
102
- valid = []
103
 
104
  for k in keys:
105
- info = check_api_key(k)
106
  show = f"{k[:6]}...{k[-4:]}"
107
- if info.get("valid") and info["remaining"] > 600:
108
- valid.append(k)
109
- table += f"| {show} | {info['remaining']} |\n"
110
  else:
111
  table += f"| {show} | ❌ |\n"
112
 
113
- if not valid:
114
  return "❌ Không có API key >600", None, None, table
115
 
116
  texts = split_text(text)
@@ -118,24 +114,20 @@ def run(api_text, api_file, voice_id, text, model, fmt):
118
  for f in os.listdir("voices"):
119
  os.remove(os.path.join("voices", f))
120
 
121
- key_idx = 0
122
-
123
  for i, t in enumerate(texts):
124
  success = False
125
- while valid:
126
- key = valid[key_idx]
127
  audio = tts(t, key, voice_id, model)
128
  if audio:
129
  with open(f"voices/voice_{i+1:03d}.{fmt}", "wb") as f:
130
  f.write(audio)
131
  success = True
132
  break
133
- else:
134
- valid.pop(key_idx)
135
- key_idx %= max(len(valid), 1)
136
 
137
  if not success:
138
- return "❌ Hết API key khi chạy", None, None, table
139
 
140
  merged = merge_audio("voices", fmt)
141
  create_srt("voices", texts)
@@ -151,12 +143,13 @@ def run(api_text, api_file, voice_id, text, model, fmt):
151
 
152
  # ================= UI =================
153
  with gr.Blocks() as app:
154
- gr.Markdown("## 🔊 ElevenLabs TTS – FIX HẾT API KEY")
155
 
156
  api_text = gr.Textbox(lines=4, label="API key")
157
  api_file = gr.File(type="binary", label="Upload API file")
158
  voice_id = gr.Textbox(label="Voice ID")
159
  text = gr.Textbox(lines=6, label="Text")
 
160
  model = gr.Dropdown(["eleven_multilingual_v2"], value="eleven_multilingual_v2")
161
  fmt = gr.Dropdown(["mp3", "wav"], value="mp3")
162
 
 
3
  import gradio as gr
4
  from pydub import AudioSegment
5
 
6
+ # ================= API CHECK =================
7
  def check_api_key(api_key):
8
  try:
9
  r = requests.get(
 
12
  timeout=10
13
  )
14
  if r.status_code != 200:
15
+ return None
 
16
  sub = r.json().get("subscription", {})
17
  limit = sub.get("character_limit")
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
 
25
 
26
  # ================= TEXT =================
 
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={
 
89
  f.write("\n".join(lines))
90
 
91
 
92
+ # ================= RUN (FIX CORE) =================
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
+ valid_keys = []
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
+ valid_keys.append(k)
105
+ table += f"| {show} | {rem} |\n"
106
  else:
107
  table += f"| {show} | ❌ |\n"
108
 
109
+ if not valid_keys:
110
  return "❌ Không có API key >600", None, None, table
111
 
112
  texts = split_text(text)
 
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
 
132
  merged = merge_audio("voices", fmt)
133
  create_srt("voices", texts)
 
143
 
144
  # ================= UI =================
145
  with gr.Blocks() as app:
146
+ gr.Markdown("## 🔊 ElevenLabs TTS – LOGIC CHUẨN COLAB")
147
 
148
  api_text = gr.Textbox(lines=4, label="API key")
149
  api_file = gr.File(type="binary", label="Upload API file")
150
  voice_id = gr.Textbox(label="Voice ID")
151
  text = gr.Textbox(lines=6, label="Text")
152
+
153
  model = gr.Dropdown(["eleven_multilingual_v2"], value="eleven_multilingual_v2")
154
  fmt = gr.Dropdown(["mp3", "wav"], value="mp3")
155