Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import pdfplumber
|
| 4 |
+
import re
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
|
| 7 |
+
# 🔹 ভাষা detect
|
| 8 |
+
def detect_lang(text):
|
| 9 |
+
if re.search(r'[\u0980-\u09FF]', text):
|
| 10 |
+
return "bn"
|
| 11 |
+
else:
|
| 12 |
+
return "en"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def mixed_text_to_speech(text):
|
| 16 |
+
if not text.strip():
|
| 17 |
+
return None
|
| 18 |
+
# টেক্সটকে বাংলা/ইংরেজি ব্লকে ভাগ করা
|
| 19 |
+
chunks = re.findall(r'[\u0980-\u09FF]+|[a-zA-Z0-9 ,.!?]+', text)
|
| 20 |
+
combined = AudioSegment.silent(duration=500) # শুরুতে সামান্য pause
|
| 21 |
+
|
| 22 |
+
for i, chunk in enumerate(chunks):
|
| 23 |
+
lang = detect_lang(chunk)
|
| 24 |
+
tts = gTTS(text=chunk, lang=lang)
|
| 25 |
+
filename = f"chunk_{i}.mp3"
|
| 26 |
+
tts.save(filename)
|
| 27 |
+
audio = AudioSegment.from_file(filename, format="mp3")
|
| 28 |
+
combined += audio + AudioSegment.silent(duration=300) #
|
| 29 |
+
|
| 30 |
+
output_path = "output.mp3"
|
| 31 |
+
combined.export(output_path, format="mp3")
|
| 32 |
+
return output_path
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# 🔹 PDF থেকে টেক্সট → Speech
|
| 37 |
+
def pdf_to_speech(pdf_file):
|
| 38 |
+
if pdf_file is None:
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
text = ""
|
| 42 |
+
with pdfplumber.open(pdf_file.name) as pdf:
|
| 43 |
+
for page in pdf.pages:
|
| 44 |
+
page_text = page.extract_text()
|
| 45 |
+
if page_text:
|
| 46 |
+
text += page_text + "\n"
|
| 47 |
+
|
| 48 |
+
if not text.strip():
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
return mixed_text_to_speech(text)
|
| 52 |
+
|
| 53 |
+
# 🔹 Gradio UI
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("## 🗣️ Mixed Language TTS (Bangla + English + PDF)")
|
| 56 |
+
|
| 57 |
+
with gr.Tab("🔤 Text to Speech"):
|
| 58 |
+
text_input = gr.Textbox(label="Enter Bangla + English text")
|
| 59 |
+
text_output = gr.Audio(label="Generated Voice", type="filepath")
|
| 60 |
+
text_btn = gr.Button("🎙️ Convert")
|
| 61 |
+
text_btn.click(fn=mixed_text_to_speech, inputs=text_input, outputs=text_output)
|
| 62 |
+
|
| 63 |
+
with gr.Tab("📘 PDF to Speech"):
|
| 64 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 65 |
+
pdf_output = gr.Audio(label="Generated Voice", type="filepath")
|
| 66 |
+
pdf_btn = gr.Button("📖 Convert PDF")
|
| 67 |
+
pdf_btn.click(fn=pdf_to_speech, inputs=pdf_input, outputs=pdf_output)
|
| 68 |
+
|
| 69 |
+
demo.launch(show_error=True)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|