Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -193,22 +193,26 @@ def ocr(image):
|
|
193 |
# # Return the extracted text
|
194 |
# return res
|
195 |
try:
|
|
|
|
|
|
|
|
|
196 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
197 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
198 |
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id).to(device)
|
199 |
-
|
200 |
# Ensure the /tmp directory exists
|
201 |
temp_dir = "/tmp"
|
202 |
if not os.path.exists(temp_dir):
|
203 |
os.makedirs(temp_dir)
|
204 |
-
|
205 |
# Save the image to a temporary file in /tmp directory
|
206 |
temp_image_path = os.path.join(temp_dir, "temp_image.jpg")
|
207 |
image.save(temp_image_path, format='JPEG')
|
208 |
-
|
209 |
# Perform OCR on the image using the file path
|
210 |
res = model.chat(tokenizer=tokenizer, image=temp_image_path, ocr_type='ocr') # Pass the file path here
|
211 |
-
|
212 |
# Return the extracted text
|
213 |
return res['text'] # Adjust this based on the actual return structure
|
214 |
except Exception as e:
|
|
|
193 |
# # Return the extracted text
|
194 |
# return res
|
195 |
try:
|
196 |
+
# Convert image to PIL Image if it's a NumPy array
|
197 |
+
if isinstance(image, np.ndarray):
|
198 |
+
image = Image.fromarray(image)
|
199 |
+
|
200 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
201 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
202 |
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id).to(device)
|
203 |
+
|
204 |
# Ensure the /tmp directory exists
|
205 |
temp_dir = "/tmp"
|
206 |
if not os.path.exists(temp_dir):
|
207 |
os.makedirs(temp_dir)
|
208 |
+
|
209 |
# Save the image to a temporary file in /tmp directory
|
210 |
temp_image_path = os.path.join(temp_dir, "temp_image.jpg")
|
211 |
image.save(temp_image_path, format='JPEG')
|
212 |
+
|
213 |
# Perform OCR on the image using the file path
|
214 |
res = model.chat(tokenizer=tokenizer, image=temp_image_path, ocr_type='ocr') # Pass the file path here
|
215 |
+
|
216 |
# Return the extracted text
|
217 |
return res['text'] # Adjust this based on the actual return structure
|
218 |
except Exception as e:
|