Sompote commited on
Commit
47587db
·
verified ·
1 Parent(s): 70502ce

Upload 4 files

Browse files
Files changed (2) hide show
  1. app.py +42 -25
  2. requirements.txt +10 -7
app.py CHANGED
@@ -19,6 +19,41 @@ st.set_page_config(
19
  if 'models_loaded' not in st.session_state:
20
  st.session_state['models_loaded'] = False
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Load models
23
  @st.cache_resource
24
  def load_models():
@@ -28,41 +63,23 @@ def load_models():
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:")
66
  import traceback
67
  st.code(traceback.format_exc())
68
  return None, None, None
 
19
  if 'models_loaded' not in st.session_state:
20
  st.session_state['models_loaded'] = False
21
 
22
+ def load_ocr_models():
23
+ """Load OCR models with proper error handling"""
24
+ try:
25
+ # Set environment variables to suppress warnings
26
+ os.environ['TOKENIZERS_PARALLELISM'] = 'false'
27
+
28
+ # Load processor with specific config
29
+ processor = TrOCRProcessor.from_pretrained(
30
+ 'openthaigpt/thai-trocr',
31
+ revision='main',
32
+ use_auth_token=False,
33
+ trust_remote_code=True,
34
+ local_files_only=False
35
+ )
36
+
37
+ # Load OCR model with specific config
38
+ ocr_model = VisionEncoderDecoderModel.from_pretrained(
39
+ 'openthaigpt/thai-trocr',
40
+ revision='main',
41
+ use_auth_token=False,
42
+ trust_remote_code=True,
43
+ local_files_only=False
44
+ )
45
+
46
+ # Move model to CPU explicitly
47
+ ocr_model = ocr_model.to('cpu')
48
+
49
+ return processor, ocr_model
50
+ except Exception as e:
51
+ st.error(f"Error loading OCR models: {str(e)}")
52
+ st.error("Detailed error information:")
53
+ import traceback
54
+ st.code(traceback.format_exc())
55
+ return None, None
56
+
57
  # Load models
58
  @st.cache_resource
59
  def load_models():
 
63
  st.error("YOLO model weights (best.pt) not found in the current directory!")
64
  return None, None, None
65
 
66
+ # Load YOLO model
67
  try:
68
  yolo_model = YOLO('best.pt', task='detect')
69
  except Exception as yolo_error:
70
  st.error(f"Error loading YOLO model: {str(yolo_error)}")
 
71
  return None, None, None
72
 
73
+ # Load OCR models
74
+ processor, ocr_model = load_ocr_models()
75
+ if processor is None or ocr_model is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  return None, None, None
77
+
78
  return processor, ocr_model, yolo_model
79
 
80
  except Exception as e:
81
+ st.error(f"Error in model loading: {str(e)}")
82
+ st.error("Detailed error information:")
83
  import traceback
84
  st.code(traceback.format_exc())
85
  return None, None, None
requirements.txt CHANGED
@@ -2,12 +2,15 @@ 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.31.0
6
- tokenizers==0.13.3
7
- torch==2.1.1
8
- torchvision==0.16.1
9
  ultralytics==8.3.0
10
  python-Levenshtein==0.23.0
11
- sentencepiece==0.1.98
12
- protobuf==3.20.3
13
- huggingface-hub==0.16.4
 
 
 
 
2
  opencv-python-headless==4.8.1.78
3
  numpy==1.26.2
4
  Pillow==10.1.0
5
+ transformers==4.28.1
6
+ tokenizers==0.13.2
7
+ torch==2.0.1
8
+ torchvision==0.15.2
9
  ultralytics==8.3.0
10
  python-Levenshtein==0.23.0
11
+ sentencepiece==0.1.97
12
+ protobuf==3.20.0
13
+ huggingface-hub==0.14.1
14
+ regex==2023.6.3
15
+ requests==2.31.0
16
+ tqdm==4.65.0