Sompote commited on
Commit
53e83a9
·
verified ·
1 Parent(s): 46c59fe

Upload 4 files

Browse files
Files changed (2) hide show
  1. app.py +33 -18
  2. 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
- # 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:")
 
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.227
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