ikraamkb commited on
Commit
bca0a86
·
verified ·
1 Parent(s): 4a9a960

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -5,7 +5,7 @@ import shutil
5
  from PIL import Image
6
  from transformers import ViltProcessor, ViltForQuestionAnswering, pipeline
7
  from gtts import gTTS
8
- import pytesseract
9
  import torch
10
  import tempfile
11
  import gradio as gr
@@ -19,6 +19,9 @@ vqa_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetune
19
  # Load image captioning model
20
  captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
21
 
 
 
 
22
  def classify_question(question: str):
23
  question_lower = question.lower()
24
  if any(word in question_lower for word in ["text", "say", "written", "read"]):
@@ -36,7 +39,8 @@ def answer_question_from_image(image, question):
36
 
37
  if mode == "ocr":
38
  try:
39
- text = pytesseract.image_to_string(image)
 
40
  answer = text.strip() or "No readable text found."
41
  except Exception as e:
42
  answer = f"OCR Error: {e}"
@@ -89,4 +93,4 @@ app = gr.mount_gradio_app(app, gui, path="/")
89
 
90
  @app.get("/")
91
  def home():
92
- return RedirectResponse(url="/")
 
5
  from PIL import Image
6
  from transformers import ViltProcessor, ViltForQuestionAnswering, pipeline
7
  from gtts import gTTS
8
+ import easyocr
9
  import torch
10
  import tempfile
11
  import gradio as gr
 
19
  # Load image captioning model
20
  captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
21
 
22
+ # Load EasyOCR reader
23
+ reader = easyocr.Reader(['en'])
24
+
25
  def classify_question(question: str):
26
  question_lower = question.lower()
27
  if any(word in question_lower for word in ["text", "say", "written", "read"]):
 
39
 
40
  if mode == "ocr":
41
  try:
42
+ result = reader.readtext(image)
43
+ text = " ".join([entry[1] for entry in result])
44
  answer = text.strip() or "No readable text found."
45
  except Exception as e:
46
  answer = f"OCR Error: {e}"
 
93
 
94
  @app.get("/")
95
  def home():
96
+ return RedirectResponse(url="/")