Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +30 -4
- requirements.txt +6 -3
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 7 |
from ultralytics import YOLO
|
| 8 |
import Levenshtein
|
|
@@ -21,10 +22,35 @@ if 'models_loaded' not in st.session_state:
|
|
| 21 |
# Load models
|
| 22 |
@st.cache_resource
|
| 23 |
def load_models():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Thai provinces list
|
| 30 |
thai_provinces = [
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
+
import torch
|
| 7 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 8 |
from ultralytics import YOLO
|
| 9 |
import Levenshtein
|
|
|
|
| 22 |
# Load models
|
| 23 |
@st.cache_resource
|
| 24 |
def load_models():
|
| 25 |
+
try:
|
| 26 |
+
# Load YOLO model first
|
| 27 |
+
yolo_model = YOLO('best.pt')
|
| 28 |
+
|
| 29 |
+
# Load TrOCR with specific model configuration
|
| 30 |
+
processor = TrOCRProcessor.from_pretrained(
|
| 31 |
+
'openthaigpt/thai-trocr',
|
| 32 |
+
use_auth_token=False,
|
| 33 |
+
trust_remote_code=True
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Load OCR model with specific configuration
|
| 37 |
+
ocr_model = VisionEncoderDecoderModel.from_pretrained(
|
| 38 |
+
'openthaigpt/thai-trocr',
|
| 39 |
+
use_auth_token=False,
|
| 40 |
+
trust_remote_code=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Move model to CPU if no GPU available
|
| 44 |
+
if not torch.cuda.is_available():
|
| 45 |
+
ocr_model = ocr_model.to('cpu')
|
| 46 |
+
|
| 47 |
+
return processor, ocr_model, yolo_model
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"Error loading models: {str(e)}")
|
| 50 |
+
st.error("Detailed error information for debugging:")
|
| 51 |
+
import traceback
|
| 52 |
+
st.code(traceback.format_exc())
|
| 53 |
+
return None, None, None
|
| 54 |
|
| 55 |
# Thai provinces list
|
| 56 |
thai_provinces = [
|
requirements.txt
CHANGED
|
@@ -2,7 +2,10 @@ streamlit==1.29.0
|
|
| 2 |
opencv-python-headless==4.8.1.78
|
| 3 |
numpy==1.26.2
|
| 4 |
Pillow==10.1.0
|
| 5 |
-
transformers==4.
|
| 6 |
-
torch==2.1.
|
|
|
|
| 7 |
ultralytics==8.0.227
|
| 8 |
-
python-Levenshtein==0.23.0
|
|
|
|
|
|
|
|
|
| 2 |
opencv-python-headless==4.8.1.78
|
| 3 |
numpy==1.26.2
|
| 4 |
Pillow==10.1.0
|
| 5 |
+
transformers==4.35.2
|
| 6 |
+
torch==2.1.1
|
| 7 |
+
torchvision==0.16.1
|
| 8 |
ultralytics==8.0.227
|
| 9 |
+
python-Levenshtein==0.23.0
|
| 10 |
+
sentencepiece==0.1.99
|
| 11 |
+
protobuf==4.25.1
|