TDN-M commited on
Commit
f746763
·
verified ·
1 Parent(s): 6b68134

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -18
app.py CHANGED
@@ -10,6 +10,7 @@ from docx import Document
10
  from content_generation import create_content, CONTENT_TYPES
11
  from openai import OpenAI
12
  from gradio_client import Client, handle_file # Thêm thư viện để gọi API
 
13
 
14
  # Khởi tạo client OpenAI với API key từ biến môi trường
15
  client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
@@ -47,22 +48,22 @@ def process_docx(file_path):
47
 
48
  def text_to_speech(content, voice_file):
49
  """
50
- Chuyển đổi nội dung thành giọng nói bằng API.
51
  """
52
  try:
53
- client = Client("thinhlpg/vixtts-demo")
54
- result = client.predict(
55
- prompt=content,
56
- language="vi",
57
- audio_file_pth=handle_file(voice_file), # Sử dụng file âm thanh được chọn
58
- normalize_text=True,
59
- api_name="/predict"
60
- )
61
- # Chỉ lấy phần tử đầu tiên (filepath) từ kết quả trả về
62
- return result[0]
63
  except Exception as e:
64
  return f"Lỗi khi chuyển đổi văn bản thành giọng nói: {str(e)}"
65
 
 
 
 
 
 
 
 
66
  def interface():
67
  with gr.Blocks() as app:
68
  gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
@@ -120,13 +121,6 @@ def interface():
120
  docx_path = "script.docx"
121
  create_docx(content, docx_path)
122
 
123
- def convert_content_to_speech(content, voice_file):
124
- audio_path = text_to_speech(content, voice_file)
125
- if os.path.exists(audio_path):
126
- return audio_path # Chỉ trả về filepath để phát audio
127
- else:
128
- return None
129
-
130
  content_button.click(generate_content,
131
  inputs=[prompt, file_upload, content_type],
132
  outputs=[content_output, download_docx, status_message])
 
10
  from content_generation import create_content, CONTENT_TYPES
11
  from openai import OpenAI
12
  from gradio_client import Client, handle_file # Thêm thư viện để gọi API
13
+ from tts import generate_speech
14
 
15
  # Khởi tạo client OpenAI với API key từ biến môi trường
16
  client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
 
48
 
49
  def text_to_speech(content, voice_file):
50
  """
51
+ Chuyển đổi nội dung thành giọng nói bằng hàm generate_speech từ tts.py.
52
  """
53
  try:
54
+ # Gọi hàm generate_speech để tạo file âm thanh
55
+ output_audio = generate_speech(content, language="vi", speaker_wav=voice_file)
56
+ return output_audio
 
 
 
 
 
 
 
57
  except Exception as e:
58
  return f"Lỗi khi chuyển đổi văn bản thành giọng nói: {str(e)}"
59
 
60
+ def convert_content_to_speech(content, voice_file):
61
+ """
62
+ Chuyển đổi nội dung thành giọng nói.
63
+ """
64
+ return text_to_speech(content, voice_file)
65
+
66
+
67
  def interface():
68
  with gr.Blocks() as app:
69
  gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
 
121
  docx_path = "script.docx"
122
  create_docx(content, docx_path)
123
 
 
 
 
 
 
 
 
124
  content_button.click(generate_content,
125
  inputs=[prompt, file_upload, content_type],
126
  outputs=[content_output, download_docx, status_message])