Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,16 @@ from PIL import Image
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import pytesseract
|
| 5 |
import tempfile
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
| 8 |
-
import re
|
| 9 |
-
import torchaudio
|
| 10 |
|
| 11 |
-
# Set Tesseract path explicitly
|
| 12 |
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
|
| 13 |
|
| 14 |
-
# --- OCR Module (Existing) ---
|
| 15 |
def bangla_reader(image):
|
| 16 |
if image is None:
|
| 17 |
return "কোনো ছবি দেওয়া হয়নি।", None
|
| 18 |
|
| 19 |
# OCR with Bengali support
|
| 20 |
-
|
| 21 |
-
ocr_text = pytesseract.image_to_string(image, lang='ben')
|
| 22 |
-
except Exception as e:
|
| 23 |
-
return f"OCR ত্রুটি: {str(e)}", None
|
| 24 |
|
| 25 |
if not ocr_text.strip():
|
| 26 |
return "কোনো লেখা সনাক্ত করা যায়নি।", None
|
|
@@ -36,103 +28,18 @@ def bangla_reader(image):
|
|
| 36 |
|
| 37 |
return f"OCR ফলাফল:\n{ocr_text.strip()}", audio_path
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
device=device
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# Initialize form extraction model (smaller model for Hugging Face Spaces)
|
| 54 |
-
form_model_name = "csebuetnlp/banglat5_nmt_en_bn" # Alternative: "google/flan-t5-small"
|
| 55 |
-
form_tokenizer = AutoTokenizer.from_pretrained(form_model_name)
|
| 56 |
-
form_model = AutoModelForSeq2SeqLM.from_pretrained(form_model_name)
|
| 57 |
-
|
| 58 |
-
def extract_form_info(text):
|
| 59 |
-
"""Extract name, age, and address from Bangla text using NLU"""
|
| 60 |
-
prompt = (
|
| 61 |
-
"নিচের বাক্য থেকে নাম, বয়স এবং ঠিকানা বের করুন। যদি কোনো তথ্য না থাকে, 'N/A' লিখুন:\n"
|
| 62 |
-
f"{text}\n\n"
|
| 63 |
-
"নাম: \nবয়স: \nঠিকানা:"
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
inputs = form_tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 67 |
-
outputs = form_model.generate(**inputs, max_length=150)
|
| 68 |
-
response = form_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 69 |
-
|
| 70 |
-
# Parse response
|
| 71 |
-
name = re.search(r'নাম:\s*(.*?)(\n|$)', response)
|
| 72 |
-
age = re.search(r'বয়স:\s*(.*?)(\n|$)', response)
|
| 73 |
-
address = re.search(r'ঠিকানা:\s*(.*?)(\n|$)', response)
|
| 74 |
-
|
| 75 |
-
return {
|
| 76 |
-
"name": name.group(1).strip() if name else "N/A",
|
| 77 |
-
"age": age.group(1).strip() if age else "N/A",
|
| 78 |
-
"address": address.group(1).strip() if address else "N/A"
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
def voice_to_form(audio_path):
|
| 82 |
-
"""Process audio input to fill form"""
|
| 83 |
-
if not audio_path:
|
| 84 |
-
return {
|
| 85 |
-
"name": "অডিও দেওয়া হয়নি",
|
| 86 |
-
"age": "",
|
| 87 |
-
"address": ""
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
try:
|
| 91 |
-
# Convert speech to text
|
| 92 |
-
text = asr_pipe(audio_path)["text"]
|
| 93 |
-
|
| 94 |
-
# Extract form information
|
| 95 |
-
form_data = extract_form_info(text)
|
| 96 |
-
|
| 97 |
-
return form_data
|
| 98 |
-
|
| 99 |
-
except Exception as e:
|
| 100 |
-
return {
|
| 101 |
-
"name": f"ত্রুটি: {str(e)}",
|
| 102 |
-
"age": "",
|
| 103 |
-
"address": ""
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
# --- Gradio Interface ---
|
| 107 |
-
with gr.Blocks(title="বাংলা সহায়ক") as demo:
|
| 108 |
-
gr.Markdown("# 📖 বাংলা সহায়ক (Bangla Assistant)")
|
| 109 |
-
|
| 110 |
-
with gr.Tab("ছবি থেকে পাঠ্য ও পাঠ (Image Reader)"):
|
| 111 |
-
gr.Markdown("## ছবির বাংলা লেখা পাঠ্য ও কণ্ঠে রূপান্তর করুন")
|
| 112 |
-
image_input = gr.Image(type="pil", label="বাংলা লেখা সম্বলিত ছবি দিন")
|
| 113 |
-
text_output = gr.Textbox(label="OCR ফলাফল")
|
| 114 |
-
audio_output = gr.Audio(label="বাংলা কণ্ঠে শুনুন")
|
| 115 |
-
image_btn = gr.Button("প্রক্রিয়া করুন")
|
| 116 |
-
image_btn.click(bangla_reader, inputs=image_input, outputs=[text_output, audio_output])
|
| 117 |
-
|
| 118 |
-
with gr.Tab("কণ্ঠ থেকে ফর্ম (Voice Form Filler)"):
|
| 119 |
-
gr.Markdown("## বাংলা কথার মাধ্যমে ফর্ম পূরণ করুন")
|
| 120 |
-
gr.Markdown("উদাহরণ: \"আমার নাম রুবেল ইসলাম, বয়স ৩৫ বছর, ঠিকানা খুলনা সদর\"")
|
| 121 |
-
audio_input = gr.Audio(source="microphone", type="filepath", label="বাংলায় কথা বলুন")
|
| 122 |
-
form_btn = gr.Button("ফর্ম পূরণ করুন")
|
| 123 |
-
|
| 124 |
-
with gr.Row():
|
| 125 |
-
with gr.Column():
|
| 126 |
-
name_out = gr.Textbox(label="নাম")
|
| 127 |
-
with gr.Column():
|
| 128 |
-
age_out = gr.Textbox(label="বয়স")
|
| 129 |
-
address_out = gr.Textbox(label="ঠিকানা")
|
| 130 |
-
|
| 131 |
-
form_btn.click(
|
| 132 |
-
voice_to_form,
|
| 133 |
-
inputs=audio_input,
|
| 134 |
-
outputs=[name_out, age_out, address_out]
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
if __name__ == "__main__":
|
| 138 |
demo.launch()
|
|
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import pytesseract
|
| 5 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# FIX: Set Tesseract path explicitly
|
| 8 |
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
|
| 9 |
|
|
|
|
| 10 |
def bangla_reader(image):
|
| 11 |
if image is None:
|
| 12 |
return "কোনো ছবি দেওয়া হয়নি।", None
|
| 13 |
|
| 14 |
# OCR with Bengali support
|
| 15 |
+
ocr_text = pytesseract.image_to_string(image, lang='ben')
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
if not ocr_text.strip():
|
| 18 |
return "কোনো লেখা সনাক্ত করা যায়নি।", None
|
|
|
|
| 28 |
|
| 29 |
return f"OCR ফলাফল:\n{ocr_text.strip()}", audio_path
|
| 30 |
|
| 31 |
+
# Gradio UI
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=bangla_reader,
|
| 34 |
+
inputs=gr.Image(type="pil", label="বাংলা লেখা সম্বলিত ছবি দিন"),
|
| 35 |
+
outputs=[
|
| 36 |
+
gr.Textbox(label="OCR ফলাফল"),
|
| 37 |
+
gr.Audio(label="বাংলা কণ্ঠে শুনুন")
|
| 38 |
+
],
|
| 39 |
+
title="📖 বাংলা রিডার (Bangla Reader)",
|
| 40 |
+
description="ছবির বাংলা লেখা পড়ে তা পাঠ্য ও কণ্ঠে রূপান্তর করে শোনায়।",
|
| 41 |
+
allow_flagging="never"
|
|
|
|
| 42 |
)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if __name__ == "__main__":
|
| 45 |
demo.launch()
|