Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ import asyncio
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
async def get_voices():
|
| 9 |
# Mock voice list to include specified voices
|
|
@@ -15,6 +19,40 @@ async def get_voices():
|
|
| 15 |
]
|
| 16 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
async def text_to_speech(text, voice, rate, pitch):
|
| 19 |
if not text.strip():
|
| 20 |
return None, "বাংলা লেখা সংযুক্ত করুন"
|
|
@@ -40,8 +78,14 @@ async def tts_interface(text, voice, rate, pitch):
|
|
| 40 |
return audio, None, gr.Warning(warning)
|
| 41 |
return audio, audio, None
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def reset_fields():
|
| 44 |
-
return "", "", 0, 0, None, None, ""
|
| 45 |
|
| 46 |
async def create_demo():
|
| 47 |
voices = await get_voices()
|
|
@@ -78,7 +122,7 @@ async def create_demo():
|
|
| 78 |
background: linear-gradient(45deg, #d32f2f, #e91e63) !important;
|
| 79 |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2) !important;
|
| 80 |
}
|
| 81 |
-
.gr-textbox, .gr-dropdown, .gr-slider {
|
| 82 |
border-radius: 8px !important;
|
| 83 |
border: 1px solid #b0bec5 !important;
|
| 84 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
|
|
@@ -107,18 +151,23 @@ async def create_demo():
|
|
| 107 |
with gr.Blocks(css=css, analytics_enabled=False) as demo:
|
| 108 |
gr.Markdown(
|
| 109 |
"""
|
| 110 |
-
# 🎙️ Edge TTS
|
| 111 |
-
লেখা থেকে উচ্চ-মানের কণ্ঠস্বরে রূপান্তর করুন। বাংলা ভাষায় স্বাভাবিক এবং সুন্দর কণ্ঠস্বর উপভোগ করুন।
|
| 112 |
""",
|
| 113 |
elem_classes=["container"]
|
| 114 |
)
|
| 115 |
|
| 116 |
with gr.Group():
|
| 117 |
-
gr.Markdown("###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
text_input = gr.Textbox(
|
| 119 |
label="প্রদত্ত লেখা",
|
| 120 |
lines=5,
|
| 121 |
-
placeholder="এখানে আপনার বাংলা লেখা
|
| 122 |
show_copy_button=True
|
| 123 |
)
|
| 124 |
voice_dropdown = gr.Dropdown(
|
|
@@ -155,6 +204,12 @@ async def create_demo():
|
|
| 155 |
warning_md = gr.Markdown(label="Warning", visible=False)
|
| 156 |
|
| 157 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
generate_btn.click(
|
| 159 |
fn=tts_interface,
|
| 160 |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
|
|
@@ -164,7 +219,7 @@ async def create_demo():
|
|
| 164 |
reset_btn.click(
|
| 165 |
fn=reset_fields,
|
| 166 |
inputs=[],
|
| 167 |
-
outputs=[text_input, voice_dropdown, rate_slider, pitch_slider, audio_output, download_output, warning_md]
|
| 168 |
)
|
| 169 |
|
| 170 |
return demo
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
+
import fitz
|
| 8 |
+
import ebooklib
|
| 9 |
+
from ebooklib import epub
|
| 10 |
+
from bs4 import BeautifulSoup
|
| 11 |
|
| 12 |
async def get_voices():
|
| 13 |
# Mock voice list to include specified voices
|
|
|
|
| 19 |
]
|
| 20 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
| 21 |
|
| 22 |
+
def extract_text_from_file(file):
|
| 23 |
+
if file is None:
|
| 24 |
+
return None, "কোনো ফাইল আপলোড করা হয়নি"
|
| 25 |
+
|
| 26 |
+
file_path = file.name
|
| 27 |
+
file_ext = os.path.splitext(file_path)[1].lower()
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
if file_ext == ".pdf":
|
| 31 |
+
doc = fitz.open(file_path)
|
| 32 |
+
text = ""
|
| 33 |
+
for page in doc:
|
| 34 |
+
text += page.get_text("text")
|
| 35 |
+
doc.close()
|
| 36 |
+
return text.strip(), None
|
| 37 |
+
elif file_ext == ".epub":
|
| 38 |
+
book = epub.read_epub(file_path)
|
| 39 |
+
text = ""
|
| 40 |
+
for item in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
|
| 41 |
+
content = item.get_content().decode("utf-8")
|
| 42 |
+
soup = BeautifulSoup(content, "lxml")
|
| 43 |
+
for element in soup.find_all(text=True):
|
| 44 |
+
if element.strip():
|
| 45 |
+
text += element.strip() + "\n"
|
| 46 |
+
return text.strip(), None
|
| 47 |
+
elif file_ext == ".txt":
|
| 48 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 49 |
+
text = f.read()
|
| 50 |
+
return text.strip(), None
|
| 51 |
+
else:
|
| 52 |
+
return None, "অসমর্থিত ফাইল ফরম্যাট। শুধুমাত্র PDF, EPUB, এবং TXT ফাইল সমর্থিত।"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return None, f"টেক্সট নিষ্কাশনে ত্রুটি: {str(e)}"
|
| 55 |
+
|
| 56 |
async def text_to_speech(text, voice, rate, pitch):
|
| 57 |
if not text.strip():
|
| 58 |
return None, "বাংলা লেখা সংযুক্ত করুন"
|
|
|
|
| 78 |
return audio, None, gr.Warning(warning)
|
| 79 |
return audio, audio, None
|
| 80 |
|
| 81 |
+
async def handle_file_upload(file):
|
| 82 |
+
text, warning = extract_text_from_file(file)
|
| 83 |
+
if warning:
|
| 84 |
+
return None, gr.Warning(warning)
|
| 85 |
+
return text, None
|
| 86 |
+
|
| 87 |
def reset_fields():
|
| 88 |
+
return None, "", "", 0, 0, None, None, ""
|
| 89 |
|
| 90 |
async def create_demo():
|
| 91 |
voices = await get_voices()
|
|
|
|
| 122 |
background: linear-gradient(45deg, #d32f2f, #e91e63) !important;
|
| 123 |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2) !important;
|
| 124 |
}
|
| 125 |
+
.gr-textbox, .gr-dropdown, .gr-slider, .gr-file {
|
| 126 |
border-radius: 8px !important;
|
| 127 |
border: 1px solid #b0bec5 !important;
|
| 128 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
|
|
|
|
| 151 |
with gr.Blocks(css=css, analytics_enabled=False) as demo:
|
| 152 |
gr.Markdown(
|
| 153 |
"""
|
| 154 |
+
# 🎙️ Edge TTS Text-to-Speech
|
| 155 |
+
লেখা বা ফাইল (PDF, EPUB, TXT) থেকে উচ্চ-মানের কণ্ঠস্বরে রূপান্তর করুন। বাংলা ভাষায় স্বাভাবিক এবং সুন্দর কণ্ঠস্বর উপভোগ করুন।
|
| 156 |
""",
|
| 157 |
elem_classes=["container"]
|
| 158 |
)
|
| 159 |
|
| 160 |
with gr.Group():
|
| 161 |
+
gr.Markdown("### ফাইল আপলোড এবং লেখা ইনপুট")
|
| 162 |
+
file_input = gr.File(
|
| 163 |
+
label="ফাইল আপলোড করুন (PDF, EPUB, TXT)",
|
| 164 |
+
file_types=[".pdf", ".epub", ".txt"],
|
| 165 |
+
file_count="single"
|
| 166 |
+
)
|
| 167 |
text_input = gr.Textbox(
|
| 168 |
label="প্রদত্ত লেখা",
|
| 169 |
lines=5,
|
| 170 |
+
placeholder="এখানে আপনার বাংলা লেখা লিখুন বা ফাইল আপলোড করুন, যেমন: 'আমি বাংলায় কথা বলি।'",
|
| 171 |
show_copy_button=True
|
| 172 |
)
|
| 173 |
voice_dropdown = gr.Dropdown(
|
|
|
|
| 204 |
warning_md = gr.Markdown(label="Warning", visible=False)
|
| 205 |
|
| 206 |
# Event handlers
|
| 207 |
+
file_input.change(
|
| 208 |
+
fn=handle_file_upload,
|
| 209 |
+
inputs=[file_input],
|
| 210 |
+
outputs=[text_input, warning_md],
|
| 211 |
+
show_progress=True
|
| 212 |
+
)
|
| 213 |
generate_btn.click(
|
| 214 |
fn=tts_interface,
|
| 215 |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
|
|
|
|
| 219 |
reset_btn.click(
|
| 220 |
fn=reset_fields,
|
| 221 |
inputs=[],
|
| 222 |
+
outputs=[file_input, text_input, voice_dropdown, rate_slider, pitch_slider, audio_output, download_output, warning_md]
|
| 223 |
)
|
| 224 |
|
| 225 |
return demo
|