ikraamkb commited on
Commit
aa2a251
·
verified ·
1 Parent(s): fac31c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -2,9 +2,7 @@ from fastapi import FastAPI
2
  from fastapi.responses import RedirectResponse
3
  import gradio as gr
4
  from PIL import Image
5
- import io
6
  import numpy as np
7
- import easyocr
8
  from transformers import pipeline
9
  from gtts import gTTS
10
  import tempfile
@@ -12,8 +10,8 @@ import os
12
 
13
  app = FastAPI()
14
 
15
- # OCR Reader
16
- ocr_reader = easyocr.Reader(['en'], gpu=False)
17
 
18
  # Captioning and VQA Pipelines
19
  caption_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
@@ -24,6 +22,12 @@ def process_image_question(image: Image.Image, question: str):
24
  return "No image uploaded.", None
25
 
26
  try:
 
 
 
 
 
 
27
  # Convert PIL image to numpy array
28
  np_image = np.array(image)
29
 
 
2
  from fastapi.responses import RedirectResponse
3
  import gradio as gr
4
  from PIL import Image
 
5
  import numpy as np
 
6
  from transformers import pipeline
7
  from gtts import gTTS
8
  import tempfile
 
10
 
11
  app = FastAPI()
12
 
13
+ # OCR Reader (lazy import inside function to avoid ImportError on Spaces)
14
+ ocr_reader = None
15
 
16
  # Captioning and VQA Pipelines
17
  caption_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
 
22
  return "No image uploaded.", None
23
 
24
  try:
25
+ # Import EasyOCR only when needed
26
+ global ocr_reader
27
+ if ocr_reader is None:
28
+ import easyocr
29
+ ocr_reader = easyocr.Reader(['en'], gpu=False)
30
+
31
  # Convert PIL image to numpy array
32
  np_image = np.array(image)
33