Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,32 @@
|
|
1 |
import fitz # PyMuPDF for PDF processing
|
2 |
from PIL import Image
|
3 |
-
from transformers import pipeline
|
4 |
import streamlit as st
|
5 |
import os
|
6 |
import re
|
7 |
from docx import Document
|
8 |
from langdetect import detect
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
12 |
|
13 |
|
14 |
def extract_text_from_image(image):
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
def extract_from_pdf(pdf_path):
|
@@ -47,7 +60,7 @@ def translate_text(text):
|
|
47 |
if detected_language == "en":
|
48 |
return "The text is already in English."
|
49 |
|
50 |
-
chunks = [text[i:i +
|
51 |
translated_text = ""
|
52 |
for chunk in chunks:
|
53 |
translated_chunk = translator(chunk, max_length=400)
|
|
|
1 |
import fitz # PyMuPDF for PDF processing
|
2 |
from PIL import Image
|
3 |
+
from transformers import pipeline, AutoProcessor, AutoModelForImageTextToText
|
4 |
import streamlit as st
|
5 |
import os
|
6 |
import re
|
7 |
from docx import Document
|
8 |
from langdetect import detect
|
9 |
|
10 |
+
# Load Qwen model for image-to-text
|
11 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct")
|
12 |
+
model = AutoModelForImageTextToText.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct")
|
13 |
+
|
14 |
+
# Load translation model
|
15 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
16 |
|
17 |
|
18 |
def extract_text_from_image(image):
|
19 |
+
"""Extract text from image using Qwen and TrOCR fallback."""
|
20 |
+
# Convert image to RGB format for processing
|
21 |
+
image = image.convert("RGB")
|
22 |
+
|
23 |
+
# Use Qwen processor and model
|
24 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
25 |
+
result = model.generate(pixel_values)
|
26 |
+
decoded_text = processor.batch_decode(result, skip_special_tokens=True)[0]
|
27 |
+
|
28 |
+
# Ensure extracted text is clean
|
29 |
+
return decoded_text.strip()
|
30 |
|
31 |
|
32 |
def extract_from_pdf(pdf_path):
|
|
|
60 |
if detected_language == "en":
|
61 |
return "The text is already in English."
|
62 |
|
63 |
+
chunks = [text[i:i + 500] for i in range(0, len(text), 500)]
|
64 |
translated_text = ""
|
65 |
for chunk in chunks:
|
66 |
translated_chunk = translator(chunk, max_length=400)
|