vuxuanhoan commited on
Commit
49317a0
·
verified ·
1 Parent(s): 30862fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -33
app.py CHANGED
@@ -4,17 +4,13 @@ import io
4
  import os
5
  import time
6
  from gtts.lang import _main_langs
7
- from docx import Document
8
- from ebooklib import epub
9
 
10
  AUDIO_DIR = 'audio_files'
11
  MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
12
 
13
  # Hàm chuyển đổi văn bản thành giọng nói sử dụng gTTS
14
  def text_to_speech(text, lang, tld):
15
- if not text.strip():
16
- raise ValueError("No text to speak")
17
-
18
  lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
19
  lang_code = lang_codes[lang]
20
 
@@ -52,26 +48,13 @@ def docx_to_speech(file, lang, tld):
52
  text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
53
  return text_to_speech(text, lang, tld)
54
 
55
- # Hàm chuyển đổi file .epub thành giọng nói
56
- def epub_to_speech(file, lang, tld):
57
- book = epub.read_epub(file.name)
58
- text = ""
59
-
60
- for item in book.get_items_of_type(epub.EpubHtml):
61
- text += item.get_body_content_str().decode('utf-8') # Lấy nội dung của mỗi trang
62
-
63
- if not text.strip():
64
- raise ValueError("No text to speak from the EPUB file")
65
-
66
- return text_to_speech(text, lang, tld)
67
-
68
  # Tạo giao diện Gradio với tab
69
  with gr.Blocks() as iface:
70
  with gr.Tab("Text to Speech"):
71
  gr.Markdown("### Convert text to speech")
72
  text_input = gr.Textbox(lines=10, label="Enter your text here:")
73
  lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
74
- tld_input = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com")
75
 
76
  audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
77
  gr.Button("Convert").click(fn=lambda text, lang, tld: text_to_speech(text, lang, tld),
@@ -82,7 +65,7 @@ with gr.Blocks() as iface:
82
  gr.Markdown("### Convert .txt file to speech")
83
  file_input = gr.File(label="Upload your .txt file")
84
  lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
85
- tld_input_file = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com")
86
 
87
  audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
88
  gr.Button("Convert").click(fn=lambda file, lang, tld: txt_to_speech(file, lang, tld),
@@ -93,22 +76,11 @@ with gr.Blocks() as iface:
93
  gr.Markdown("### Convert .docx file to speech")
94
  docx_file_input = gr.File(label="Upload your .docx file")
95
  lang_input_docx = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
96
- tld_input_docx = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com")
97
 
98
  audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
99
  gr.Button("Convert").click(fn=lambda file, lang, tld: docx_to_speech(file, lang, tld),
100
  inputs=[docx_file_input, lang_input_docx, tld_input_docx],
101
  outputs=[audio_output_docx, file_output_docx])
102
 
103
- with gr.Tab("EPUB to Speech"):
104
- gr.Markdown("### Convert .epub file to speech")
105
- epub_file_input = gr.File(label="Upload your .epub file")
106
- lang_input_epub = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
107
- tld_input_epub = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com")
108
-
109
- audio_output_epub, file_output_epub = gr.Audio(label="Audio"), gr.File(label="Audio File")
110
- gr.Button("Convert").click(fn=lambda file, lang, tld: epub_to_speech(file, lang, tld),
111
- inputs=[epub_file_input, lang_input_epub, tld_input_epub],
112
- outputs=[audio_output_epub, file_output_epub])
113
-
114
- iface.launch(share=True, enable_queue=True)
 
4
  import os
5
  import time
6
  from gtts.lang import _main_langs
7
+ from docx import Document # Thêm thư viện này để làm việc với tệp docx
 
8
 
9
  AUDIO_DIR = 'audio_files'
10
  MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
11
 
12
  # Hàm chuyển đổi văn bản thành giọng nói sử dụng gTTS
13
  def text_to_speech(text, lang, tld):
 
 
 
14
  lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
15
  lang_code = lang_codes[lang]
16
 
 
48
  text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
49
  return text_to_speech(text, lang, tld)
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # Tạo giao diện Gradio với tab
52
  with gr.Blocks() as iface:
53
  with gr.Tab("Text to Speech"):
54
  gr.Markdown("### Convert text to speech")
55
  text_input = gr.Textbox(lines=10, label="Enter your text here:")
56
  lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
57
+ tld_input = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
58
 
59
  audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
60
  gr.Button("Convert").click(fn=lambda text, lang, tld: text_to_speech(text, lang, tld),
 
65
  gr.Markdown("### Convert .txt file to speech")
66
  file_input = gr.File(label="Upload your .txt file")
67
  lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
68
+ tld_input_file = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
69
 
70
  audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
71
  gr.Button("Convert").click(fn=lambda file, lang, tld: txt_to_speech(file, lang, tld),
 
76
  gr.Markdown("### Convert .docx file to speech")
77
  docx_file_input = gr.File(label="Upload your .docx file")
78
  lang_input_docx = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
79
+ tld_input_docx = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
80
 
81
  audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
82
  gr.Button("Convert").click(fn=lambda file, lang, tld: docx_to_speech(file, lang, tld),
83
  inputs=[docx_file_input, lang_input_docx, tld_input_docx],
84
  outputs=[audio_output_docx, file_output_docx])
85
 
86
+ iface.launch(enable_queue=True)