yamanavijayavardhan commited on
Commit
ef2032a
·
1 Parent(s): 84b78a0

printing extracted text22

Browse files
Files changed (3) hide show
  1. HTR/app.py +11 -2
  2. all_models.py +41 -24
  3. main.py +7 -1
HTR/app.py CHANGED
@@ -17,8 +17,17 @@ from HTR.strike import struck_images
17
  from HTR.hcr import text
18
  from HTR.spell_and_gramer_check import spell_grammer
19
 
20
- # Use direct path to local model
21
- model_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'models', 'vit-base-beans')
 
 
 
 
 
 
 
 
 
22
 
23
  def preprocess_image(img):
24
  """Preprocess image to improve text detection"""
 
17
  from HTR.hcr import text
18
  from HTR.spell_and_gramer_check import spell_grammer
19
 
20
+ # Get absolute path to project root
21
+ project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
22
+ model_path = os.path.join(project_root, 'models', 'vit-base-beans')
23
+
24
+ # Log model path for debugging
25
+ logger.info(f"Using model path: {model_path}")
26
+ if not os.path.exists(model_path):
27
+ logger.error(f"Model directory not found at: {model_path}")
28
+ else:
29
+ files = os.listdir(model_path)
30
+ logger.info(f"Found model files: {files}")
31
 
32
  def preprocess_image(img):
33
  """Preprocess image to improve text detection"""
all_models.py CHANGED
@@ -271,52 +271,69 @@ class ModelSingleton:
271
  raise
272
 
273
  def get_vit_model(self):
274
- """Get ViT model with reference counting"""
275
  try:
276
  if self.vit_model is None:
277
- from transformers import ViTImageProcessor, ViTForImageClassification
278
- logger.info("Loading local ViT model...")
279
 
280
- # Use direct path to local model
281
- model_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'models', 'vit-base-beans')
282
- logger.info(f"Using local model path: {model_path}")
 
283
 
 
284
  if not os.path.exists(model_path):
285
  raise FileNotFoundError(f"Local model directory not found at: {model_path}")
286
 
287
- # Check for required files
288
- required_files = ['model.safetensors', 'config.json']
289
- missing_files = [f for f in required_files if not os.path.exists(os.path.join(model_path, f))]
290
- if missing_files:
291
- raise FileNotFoundError(f"Missing required model files: {missing_files}")
292
 
293
- # Create processor with default settings (no download)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  self.vit_processor = ViTImageProcessor(
295
  do_resize=True,
296
- size=224,
297
- do_normalize=True,
298
- image_mean=[0.5, 0.5, 0.5],
299
- image_std=[0.5, 0.5, 0.5]
300
  )
301
 
302
- # Load model from local files only
303
- logger.info("Loading ViT model from local files...")
304
  self.vit_model = ViTForImageClassification.from_pretrained(
305
  model_path,
306
- local_files_only=True, # Prevent downloads
307
- use_safetensors=True, # Use .safetensors file
308
- trust_remote_code=False # Don't trust or download remote code
 
 
 
309
  )
310
 
311
- # Move model to device
312
  self.vit_model.to(self.device)
313
  self.vit_model.eval()
314
- logger.info(f"Successfully loaded local ViT model and moved to {self.device}")
315
 
316
  self._reference_counts['vit'] += 1
317
  return self.vit_model, self.vit_processor
 
318
  except Exception as e:
319
- logger.error(f"Error loading local ViT model: {e}")
320
  raise
321
 
322
  def release_similarity_model(self):
 
271
  raise
272
 
273
  def get_vit_model(self):
274
+ """Get ViT model using only local files - no downloads"""
275
  try:
276
  if self.vit_model is None:
277
+ from transformers import ViTConfig, ViTImageProcessor, ViTForImageClassification
278
+ logger.info("Loading local ViT model from files...")
279
 
280
+ # Get absolute path to model directory
281
+ model_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
282
+ model_path = os.path.join(model_root, 'models', 'vit-base-beans')
283
+ logger.info(f"Using local model directory: {model_path}")
284
 
285
+ # Check model directory exists
286
  if not os.path.exists(model_path):
287
  raise FileNotFoundError(f"Local model directory not found at: {model_path}")
288
 
289
+ # Get paths to required files
290
+ model_file = os.path.join(model_path, 'model.safetensors')
291
+ config_file = os.path.join(model_path, 'config.json')
 
 
292
 
293
+ # Verify files exist
294
+ if not os.path.exists(model_file):
295
+ raise FileNotFoundError(f"Local model weights file not found at: {model_file}")
296
+ if not os.path.exists(config_file):
297
+ raise FileNotFoundError(f"Local model config file not found at: {config_file}")
298
+
299
+ logger.info("Found all required local model files:")
300
+ logger.info(f"- Using model weights: {model_file}")
301
+ logger.info(f"- Using config file: {config_file}")
302
+
303
+ # Load config directly from file
304
+ logger.info("Loading model configuration from local file...")
305
+ config = ViTConfig.from_json_file(config_file)
306
+
307
+ # Create processor from local config
308
+ logger.info("Creating image processor from local config...")
309
  self.vit_processor = ViTImageProcessor(
310
  do_resize=True,
311
+ size=config.image_size,
312
+ do_normalize=True
 
 
313
  )
314
 
315
+ # Load model directly from local files
316
+ logger.info("Loading model weights from local file...")
317
  self.vit_model = ViTForImageClassification.from_pretrained(
318
  model_path,
319
+ config=config,
320
+ local_files_only=True,
321
+ use_safetensors=True,
322
+ trust_remote_code=False,
323
+ from_tf=False,
324
+ _fast_init=True
325
  )
326
 
327
+ logger.info(f"Moving model to {self.device}...")
328
  self.vit_model.to(self.device)
329
  self.vit_model.eval()
330
+ logger.info("Local model loaded successfully!")
331
 
332
  self._reference_counts['vit'] += 1
333
  return self.vit_model, self.vit_processor
334
+
335
  except Exception as e:
336
+ logger.error(f"Error loading local ViT model: {str(e)}")
337
  raise
338
 
339
  def release_similarity_model(self):
main.py CHANGED
@@ -52,6 +52,11 @@ def get_user_cache_dir():
52
  BASE_DIR = get_user_cache_dir()
53
  log_print(f"Using base directory: {BASE_DIR}")
54
 
 
 
 
 
 
55
  # Set environment variables before any other imports
56
  cache_dirs = {
57
  'root': BASE_DIR,
@@ -65,7 +70,8 @@ cache_dirs = {
65
  'logs': os.path.join(BASE_DIR, 'logs'),
66
  'uploads': os.path.join(BASE_DIR, 'uploads'),
67
  'images': os.path.join(BASE_DIR, 'images'),
68
- 'ans_image': os.path.join(BASE_DIR, 'ans_image')
 
69
  }
70
 
71
  # Create all necessary directories with proper permissions
 
52
  BASE_DIR = get_user_cache_dir()
53
  log_print(f"Using base directory: {BASE_DIR}")
54
 
55
+ # Get absolute path to project root for models
56
+ PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
57
+ os.environ['MODEL_ROOT'] = PROJECT_ROOT
58
+ log_print(f"Set MODEL_ROOT to: {PROJECT_ROOT}")
59
+
60
  # Set environment variables before any other imports
61
  cache_dirs = {
62
  'root': BASE_DIR,
 
70
  'logs': os.path.join(BASE_DIR, 'logs'),
71
  'uploads': os.path.join(BASE_DIR, 'uploads'),
72
  'images': os.path.join(BASE_DIR, 'images'),
73
+ 'ans_image': os.path.join(BASE_DIR, 'ans_image'),
74
+ 'models': os.path.join(PROJECT_ROOT, 'models') # Add models directory
75
  }
76
 
77
  # Create all necessary directories with proper permissions