Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
import pdfplumber
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
from pydub import AudioSegment
|
| 6 |
|
|
@@ -11,16 +13,16 @@ def detect_lang(text):
|
|
| 11 |
else:
|
| 12 |
return "en"
|
| 13 |
|
| 14 |
-
|
| 15 |
def mixed_text_to_speech(text):
|
| 16 |
if not text or not text.strip():
|
| 17 |
-
return None
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
chunks = re.findall(r'[\u0980-\u09FF]+|[a-zA-Z0-9
|
| 21 |
|
| 22 |
if not chunks:
|
| 23 |
-
return None
|
| 24 |
|
| 25 |
combined = AudioSegment.silent(duration=500)
|
| 26 |
for i, chunk in enumerate(chunks):
|
|
@@ -28,36 +30,50 @@ def mixed_text_to_speech(text):
|
|
| 28 |
if not chunk:
|
| 29 |
continue
|
| 30 |
lang = detect_lang(chunk)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
output_path = "output.mp3"
|
| 38 |
combined.export(output_path, format="mp3")
|
| 39 |
return output_path
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
def pdf_to_speech(pdf_file):
|
| 45 |
if pdf_file is None:
|
| 46 |
return None
|
| 47 |
|
| 48 |
-
text =
|
| 49 |
-
with pdfplumber.open(pdf_file.name) as pdf:
|
| 50 |
-
for page in pdf.pages:
|
| 51 |
-
page_text = page.extract_text()
|
| 52 |
-
if page_text:
|
| 53 |
-
text += page_text + "\n"
|
| 54 |
-
|
| 55 |
if not text.strip():
|
| 56 |
-
return None
|
| 57 |
|
| 58 |
return mixed_text_to_speech(text)
|
| 59 |
|
| 60 |
-
|
| 61 |
# 🔹 Gradio UI
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
gr.Markdown("## 🗣️ Mixed Language TTS (Bangla + English + PDF)")
|
|
@@ -75,6 +91,7 @@ with gr.Blocks() as demo:
|
|
| 75 |
pdf_btn.click(fn=pdf_to_speech, inputs=pdf_input, outputs=pdf_output)
|
| 76 |
|
| 77 |
demo.launch(show_error=True)
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
import pdfplumber
|
| 4 |
+
from pdf2image import convert_from_path
|
| 5 |
+
import pytesseract
|
| 6 |
import re
|
| 7 |
from pydub import AudioSegment
|
| 8 |
|
|
|
|
| 13 |
else:
|
| 14 |
return "en"
|
| 15 |
|
| 16 |
+
# 🔹 Mixed text to speech
|
| 17 |
def mixed_text_to_speech(text):
|
| 18 |
if not text or not text.strip():
|
| 19 |
+
return None
|
| 20 |
|
| 21 |
+
# বাংলা + ইংরেজি ব্লকে ভাগ
|
| 22 |
+
chunks = re.findall(r'[\u0980-\u09FF\s,.!?]+|[a-zA-Z0-9\s,.!?]+', text)
|
| 23 |
|
| 24 |
if not chunks:
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
combined = AudioSegment.silent(duration=500)
|
| 28 |
for i, chunk in enumerate(chunks):
|
|
|
|
| 30 |
if not chunk:
|
| 31 |
continue
|
| 32 |
lang = detect_lang(chunk)
|
| 33 |
+
try:
|
| 34 |
+
tts = gTTS(text=chunk, lang=lang)
|
| 35 |
+
filename = f"chunk_{i}.mp3"
|
| 36 |
+
tts.save(filename)
|
| 37 |
+
audio = AudioSegment.from_file(filename, format="mp3")
|
| 38 |
+
combined += audio + AudioSegment.silent(duration=300)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Skipping chunk due to error: {chunk} | {e}")
|
| 41 |
|
| 42 |
output_path = "output.mp3"
|
| 43 |
combined.export(output_path, format="mp3")
|
| 44 |
return output_path
|
| 45 |
|
| 46 |
+
# 🔹 PDF থেকে text বের করা (OCR fallback)
|
| 47 |
+
def pdf_to_text(pdf_file):
|
| 48 |
+
text = ""
|
| 49 |
+
# প্রথমে pdfplumber দিয়ে চেষ্টা
|
| 50 |
+
try:
|
| 51 |
+
with pdfplumber.open(pdf_file.name) as pdf:
|
| 52 |
+
for page in pdf.pages:
|
| 53 |
+
page_text = page.extract_text()
|
| 54 |
+
if page_text:
|
| 55 |
+
text += page_text + "\n"
|
| 56 |
+
except:
|
| 57 |
+
pass
|
| 58 |
+
|
| 59 |
+
# যদি text ফাঁকা থাকে, OCR ব্যবহার কর
|
| 60 |
+
if not text.strip():
|
| 61 |
+
images = convert_from_path(pdf_file.name)
|
| 62 |
+
for img in images:
|
| 63 |
+
text += pytesseract.image_to_string(img, lang='ben+eng') + "\n"
|
| 64 |
|
| 65 |
+
return text
|
| 66 |
|
|
|
|
| 67 |
def pdf_to_speech(pdf_file):
|
| 68 |
if pdf_file is None:
|
| 69 |
return None
|
| 70 |
|
| 71 |
+
text = pdf_to_text(pdf_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if not text.strip():
|
| 73 |
+
return None
|
| 74 |
|
| 75 |
return mixed_text_to_speech(text)
|
| 76 |
|
|
|
|
| 77 |
# 🔹 Gradio UI
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
gr.Markdown("## 🗣️ Mixed Language TTS (Bangla + English + PDF)")
|
|
|
|
| 91 |
pdf_btn.click(fn=pdf_to_speech, inputs=pdf_input, outputs=pdf_output)
|
| 92 |
|
| 93 |
demo.launch(show_error=True)
|
| 94 |
+
|
| 95 |
|
| 96 |
|
| 97 |
|