TDN-M commited on
Commit
070807c
·
verified ·
1 Parent(s): f77bed0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -24
app.py CHANGED
@@ -8,13 +8,12 @@ import gradio as gr
8
  from docx import Document
9
  from content_generation import create_content, CONTENT_TYPES
10
  from openai import OpenAI
11
- from gradio_client import Client, handle_file
12
  import subprocess
13
  import time
14
 
15
  # Khởi tạo client OpenAI với API key từ biến môi trường
16
  client_openai = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
17
- client_vixtts = Client("thinhlpg/vixtts-demo")
18
 
19
  def create_docx(content, output_path):
20
  """
@@ -44,30 +43,15 @@ def process_docx(file_path):
44
  text += para.text
45
  return text
46
 
47
- def convert_text_to_speech(text, language='vi', timeout=60):
48
  """
49
- Chuyển đổi văn bản thành giọng nói với thời gian chờ đợi.
50
  """
51
  try:
52
- start_time = time.time()
53
- future = client_vixtts.predict(
54
- prompt=text,
55
- language=language,
56
- audio_file_pth=handle_file(''),
57
- normalize_text=True,
58
- api_name="/predict"
59
- )
60
- while not future.done():
61
- elapsed_time = time.time() - start_time
62
- if elapsed_time > timeout:
63
- raise TimeoutError(f"Chờ quá lâu cho API chuyển đổi văn bản thành giọng nói (>{timeout} giây)")
64
- time.sleep(1) # Kiểm tra mỗi giây
65
-
66
- result = future.result()
67
- audio_file_path = result.audio_file_pth
68
- if not audio_file_path or not os.path.exists(audio_file_path):
69
- raise FileNotFoundError(f"File âm thanh không được tạo ra hoặc không tồn tại: {audio_file_path}")
70
- return audio_file_path
71
  except Exception as e:
72
  raise Exception(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {str(e)}")
73
 
@@ -183,7 +167,7 @@ def interface():
183
  docx_path = "script.docx"
184
  create_docx(text_to_convert, docx_path)
185
  status_message.value = "Đang chuyển đổi văn bản thành giọng nói..."
186
- audio_path = convert_text_to_speech(text_to_convert, language)
187
  status_message.value = "Đã chuyển đổi văn bản thành giọng nói thành công!"
188
  status_message.value = "Đang tạo video..."
189
  video_path = create_video(audio_path, mc_choice, text_color)
 
8
  from docx import Document
9
  from content_generation import create_content, CONTENT_TYPES
10
  from openai import OpenAI
11
+ import edge_tts
12
  import subprocess
13
  import time
14
 
15
  # Khởi tạo client OpenAI với API key từ biến môi trường
16
  client_openai = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
 
17
 
18
  def create_docx(content, output_path):
19
  """
 
43
  text += para.text
44
  return text
45
 
46
+ async def convert_text_to_speech(text, language='vi', output_file="output.wav"):
47
  """
48
+ Chuyển đổi văn bản thành giọng nói sử dụng edge_tts.
49
  """
50
  try:
51
+ communicate = edge_tts.Communicate(text, "vi-VN-JennyNeural")
52
+ with open(output_file, "wb") as file:
53
+ await communicate.save(file)
54
+ return output_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
56
  raise Exception(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {str(e)}")
57
 
 
167
  docx_path = "script.docx"
168
  create_docx(text_to_convert, docx_path)
169
  status_message.value = "Đang chuyển đổi văn bản thành giọng nói..."
170
+ audio_path = await convert_text_to_speech(text_to_convert, language)
171
  status_message.value = "Đã chuyển đổi văn bản thành giọng nói thành công!"
172
  status_message.value = "Đang tạo video..."
173
  video_path = create_video(audio_path, mc_choice, text_color)