File size: 2,949 Bytes
f35e3e6
 
 
a393f0e
 
f35e3e6
 
 
 
 
 
 
 
 
 
a393f0e
f35e3e6
5c26806
a393f0e
5c26806
a393f0e
 
f35e3e6
5c26806
a393f0e
5c26806
 
f35e3e6
5c26806
 
 
f35e3e6
a393f0e
 
 
 
 
 
 
 
f35e3e6
 
 
 
 
a393f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f35e3e6
a393f0e
5c26806
f35e3e6
 
 
 
a393f0e
f35e3e6
a393f0e
f35e3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a393f0e
f35e3e6
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import gradio as gr
from gtts import gTTS
import pdfplumber
from pdf2image import convert_from_path
import pytesseract
import re
from pydub import AudioSegment

# 🔹 ভাষা detect
def detect_lang(text):
    if re.search(r'[\u0980-\u09FF]', text):
        return "bn"
    else:
        return "en"

# 🔹 Mixed text to speech
def mixed_text_to_speech(text):
    if not text or not text.strip():
        return None

    # বাংলা + ইংরেজি ব্লকে ভাগ
    chunks = re.findall(r'[\u0980-\u09FF\s,.!?]+|[a-zA-Z0-9\s,.!?]+', text)

    if not chunks:
        return None

    combined = AudioSegment.silent(duration=500)
    for i, chunk in enumerate(chunks):
        chunk = chunk.strip()
        if not chunk:
            continue
        lang = detect_lang(chunk)
        try:
            tts = gTTS(text=chunk, lang=lang)
            filename = f"chunk_{i}.mp3"
            tts.save(filename)
            audio = AudioSegment.from_file(filename, format="mp3")
            combined += audio + AudioSegment.silent(duration=300)
        except Exception as e:
            print(f"Skipping chunk due to error: {chunk} | {e}")

    output_path = "output.mp3"
    combined.export(output_path, format="mp3")
    return output_path

# 🔹 PDF থেকে text বের করা (OCR fallback)
def pdf_to_text(pdf_file):
    text = ""
    # প্রথমে pdfplumber দিয়ে চেষ্টা
    try:
        with pdfplumber.open(pdf_file.name) as pdf:
            for page in pdf.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + "\n"
    except:
        pass

    # যদি text ফাঁকা থাকে, OCR ব্যবহার কর
    if not text.strip():
        images = convert_from_path(pdf_file.name)
        for img in images:
            text += pytesseract.image_to_string(img, lang='ben+eng') + "\n"

    return text

def pdf_to_speech(pdf_file):
    if pdf_file is None:
        return None

    text = pdf_to_text(pdf_file)
    if not text.strip():
        return None

    return mixed_text_to_speech(text)

# 🔹 Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🗣️ Mixed Language TTS (Bangla + English + PDF)")

    with gr.Tab("🔤 Text to Speech"):
        text_input = gr.Textbox(label="Enter Bangla + English text")
        text_output = gr.Audio(label="Generated Voice", type="filepath")
        text_btn = gr.Button("🎙️ Convert")
        text_btn.click(fn=mixed_text_to_speech, inputs=text_input, outputs=text_output)

    with gr.Tab("📘 PDF to Speech"):
        pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
        pdf_output = gr.Audio(label="Generated Voice", type="filepath")
        pdf_btn = gr.Button("📖 Convert PDF")
        pdf_btn.click(fn=pdf_to_speech, inputs=pdf_input, outputs=pdf_output)

demo.launch(show_error=True)