Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +33 -18
- requirements.txt +1 -1
app.py
CHANGED
@@ -23,28 +23,43 @@ if 'models_loaded' not in st.session_state:
|
|
23 |
@st.cache_resource
|
24 |
def load_models():
|
25 |
try:
|
26 |
-
#
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Load TrOCR with specific model configuration
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
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:")
|
|
|
23 |
@st.cache_resource
|
24 |
def load_models():
|
25 |
try:
|
26 |
+
# Check if YOLO weights exist
|
27 |
+
if not os.path.exists('best.pt'):
|
28 |
+
st.error("YOLO model weights (best.pt) not found in the current directory!")
|
29 |
+
return None, None, None
|
30 |
+
|
31 |
+
# Load YOLO model with error handling
|
32 |
+
try:
|
33 |
+
yolo_model = YOLO('best.pt', task='detect')
|
34 |
+
except Exception as yolo_error:
|
35 |
+
st.error(f"Error loading YOLO model: {str(yolo_error)}")
|
36 |
+
st.error("This might be due to a version mismatch. Please ensure you're using the correct model weights.")
|
37 |
+
return None, None, None
|
38 |
|
39 |
# Load TrOCR with specific model configuration
|
40 |
+
try:
|
41 |
+
processor = TrOCRProcessor.from_pretrained(
|
42 |
+
'openthaigpt/thai-trocr',
|
43 |
+
use_auth_token=False,
|
44 |
+
trust_remote_code=True
|
45 |
+
)
|
46 |
+
|
47 |
+
ocr_model = VisionEncoderDecoderModel.from_pretrained(
|
48 |
+
'openthaigpt/thai-trocr',
|
49 |
+
use_auth_token=False,
|
50 |
+
trust_remote_code=True
|
51 |
+
)
|
52 |
+
|
53 |
+
# Move model to CPU if no GPU available
|
54 |
+
if not torch.cuda.is_available():
|
55 |
+
ocr_model = ocr_model.to('cpu')
|
56 |
+
|
57 |
+
except Exception as ocr_error:
|
58 |
+
st.error(f"Error loading OCR model: {str(ocr_error)}")
|
59 |
+
return None, None, None
|
60 |
|
61 |
return processor, ocr_model, yolo_model
|
62 |
+
|
63 |
except Exception as e:
|
64 |
st.error(f"Error loading models: {str(e)}")
|
65 |
st.error("Detailed error information for debugging:")
|
requirements.txt
CHANGED
@@ -5,7 +5,7 @@ Pillow==10.1.0
|
|
5 |
transformers==4.35.2
|
6 |
torch==2.1.1
|
7 |
torchvision==0.16.1
|
8 |
-
ultralytics==8.0.
|
9 |
python-Levenshtein==0.23.0
|
10 |
sentencepiece==0.1.99
|
11 |
protobuf==4.25.1
|
|
|
5 |
transformers==4.35.2
|
6 |
torch==2.1.1
|
7 |
torchvision==0.16.1
|
8 |
+
ultralytics==8.0.196
|
9 |
python-Levenshtein==0.23.0
|
10 |
sentencepiece==0.1.99
|
11 |
protobuf==4.25.1
|