Download app.py from QuickLearnerAI/Bilingual-PDF_Text-Reader: direct link, hf CLI and curl.
- Browser
- Download file 2.95 kB
-
https://huggingface.co/spaces/QuickLearnerAI/Bilingual-PDF_Text-Reader/resolve/main/app.py
- Command line
-
hf download hf://spaces/QuickLearnerAI/Bilingual-PDF_Text-Reader/app.py
-
curl -L -o app.py https://huggingface.co/spaces/QuickLearnerAI/Bilingual-PDF_Text-Reader/resolve/main/app.py
2.95 kB
| 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) | |