tahirsher commited on
Commit
add3a0f
·
verified ·
1 Parent(s): ab2f33a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -1,28 +1,21 @@
1
  import fitz # PyMuPDF for PDF processing
2
  from PIL import Image # For image processing
3
- from transformers import AutoTokenizer, AutoModelForImageTextToText, AutoModelForCausalLM, pipeline
4
  import streamlit as st
5
  import os
6
  import io
7
  from docx import Document # For Word document processing
8
 
9
- # Load the TrOCR model for image-to-text
10
- trocr_tokenizer = AutoTokenizer.from_pretrained("microsoft/trocr-large-printed")
11
- trocr_model = AutoModelForImageTextToText.from_pretrained("microsoft/trocr-large-printed")
12
 
13
- # Load the DeepSeek model for text-to-text translation
14
- translation_tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Llama-8B")
15
- translation_model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Llama-8B")
16
-
17
- # Set up the translation pipeline
18
- translator = pipeline("text-generation", model=translation_model, tokenizer=translation_tokenizer)
19
 
20
  # Function to extract text from an image using TrOCR
21
  def extract_text_from_image(image):
22
- inputs = trocr_tokenizer(image, return_tensors="pt").input_ids
23
- outputs = trocr_model.generate(inputs)
24
- text = trocr_tokenizer.decode(outputs[0], skip_special_tokens=True)
25
- return text
26
 
27
  # Function to extract text from a PDF
28
  def extract_from_pdf(pdf_path):
@@ -51,7 +44,7 @@ def extract_from_word(docx_path):
51
 
52
  # Function to translate text to English
53
  def translate_text(text):
54
- translated_text = translator(text, max_length=400)[0]['generated_text']
55
  return translated_text
56
 
57
  # Function to create a PDF from translated text
 
1
  import fitz # PyMuPDF for PDF processing
2
  from PIL import Image # For image processing
3
+ from transformers import pipeline
4
  import streamlit as st
5
  import os
6
  import io
7
  from docx import Document # For Word document processing
8
 
9
+ # Load the TrOCR model for image-to-text (smaller model)
10
+ trocr_pipeline = pipeline("image-to-text", model="microsoft/trocr-base-printed")
 
11
 
12
+ # Load the translation model (smaller model)
13
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
 
 
 
 
14
 
15
  # Function to extract text from an image using TrOCR
16
  def extract_text_from_image(image):
17
+ result = trocr_pipeline(image)
18
+ return result[0]['generated_text']
 
 
19
 
20
  # Function to extract text from a PDF
21
  def extract_from_pdf(pdf_path):
 
44
 
45
  # Function to translate text to English
46
  def translate_text(text):
47
+ translated_text = translator(text, max_length=400)[0]['translation_text']
48
  return translated_text
49
 
50
  # Function to create a PDF from translated text