Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,38 @@
|
|
1 |
-
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
if __name__ == "__main__":
|
23 |
-
app.run(host='0.0.0.0', port=5000) # Adjust port if necessary
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
# Assuming 'model' and 'tokenizer' are defined elsewhere in your code
|
4 |
+
# from your_model_file import model, tokenizer
|
5 |
|
6 |
+
def load_image(image_file):
|
7 |
+
"""Load and preprocess the image."""
|
8 |
+
if isinstance(image_file, Image.Image): # Check if the input is an Image object
|
9 |
+
return image_file.convert("RGB") # Convert to RGB if necessary
|
10 |
+
elif isinstance(image_file, str) and (image_file.startswith('http') or image_file.startswith('https')):
|
11 |
+
# Handle URL case (you can use an external library to fetch the image if needed)
|
12 |
+
return Image.open(requests.get(image_file, stream=True).raw).convert("RGB")
|
13 |
+
else:
|
14 |
+
# Handle file path case
|
15 |
+
return Image.open(image_file).convert("RGB")
|
16 |
|
17 |
+
def perform_ocr(image):
|
18 |
+
"""Perform OCR on the uploaded image."""
|
19 |
+
try:
|
20 |
+
# Load and preprocess the image
|
21 |
+
processed_image = load_image(image)
|
22 |
+
# Use the model for OCR
|
23 |
+
res = model.chat(tokenizer, processed_image, ocr_type='ocr')
|
24 |
+
return res
|
25 |
+
except Exception as e:
|
26 |
+
return str(e) # Return the error message
|
27 |
|
28 |
+
# Gradio interface setup
|
29 |
+
iface = gr.Interface(
|
30 |
+
fn=perform_ocr,
|
31 |
+
inputs=gr.Image(type="pil"), # Ensure Gradio accepts images as PIL images
|
32 |
+
outputs="text",
|
33 |
+
title="OCR Application",
|
34 |
+
description="Upload an image to perform Optical Character Recognition (OCR)."
|
35 |
+
)
|
36 |
|
37 |
+
if _name_ == "_main_":
|
38 |
+
iface.launch()
|
|
|
|