VanguardAI commited on
Commit
928fb2c
·
verified ·
1 Parent(s): 98b1d96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -20,6 +20,18 @@ import numpy as np
20
  # Import Arabic text correction module
21
  from arabic_corrector import get_corrector
22
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Constants
24
  MIN_PIXELS = 3136
25
  MAX_PIXELS = 11289600
@@ -427,13 +439,18 @@ def inference(image: Image.Image, prompt: str, max_new_tokens: int = 24000) -> s
427
  primary_device = next(model.parameters()).device
428
  inputs = inputs.to(primary_device)
429
 
430
- # Generate output
 
 
 
 
 
431
  with torch.no_grad():
432
  generated_ids = model.generate(
433
  **inputs,
434
  max_new_tokens=max_new_tokens,
435
- do_sample=False,
436
- temperature=0.1
437
  )
438
 
439
  # Decode output
@@ -459,7 +476,6 @@ def inference(image: Image.Image, prompt: str, max_new_tokens: int = 24000) -> s
459
  def _generate_text_and_confidence_for_crop(
460
  image: Image.Image,
461
  max_new_tokens: int = 128,
462
- temperature: float = 0.1,
463
  ) -> Tuple[str, float]:
464
  """Generate text for a cropped region and compute average per-token confidence from model scores.
465
 
@@ -500,13 +516,17 @@ def _generate_text_and_confidence_for_crop(
500
  primary_device = next(model.parameters()).device
501
  inputs = inputs.to(primary_device)
502
 
503
- # Generate with scores
 
 
 
 
 
504
  with torch.no_grad():
505
  outputs = model.generate(
506
  **inputs,
507
  max_new_tokens=max_new_tokens,
508
- do_sample=False,
509
- temperature=temperature,
510
  output_scores=True,
511
  return_dict_in_generate=True,
512
  )
@@ -1542,4 +1562,4 @@ if __name__ == "__main__":
1542
  share=False,
1543
  debug=True,
1544
  show_error=True
1545
- )
 
20
  # Import Arabic text correction module
21
  from arabic_corrector import get_corrector
22
 
23
+ # ========================================
24
+ # DETERMINISTIC SETTINGS FOR CONSISTENCY
25
+ # ========================================
26
+ # Set seeds for reproducibility - ensures same image always gives same output
27
+ torch.manual_seed(42)
28
+ torch.cuda.manual_seed_all(42)
29
+ np.random.seed(42)
30
+
31
+ # Ensure deterministic behavior in PyTorch operations
32
+ torch.backends.cudnn.deterministic = True
33
+ torch.backends.cudnn.benchmark = False
34
+
35
  # Constants
36
  MIN_PIXELS = 3136
37
  MAX_PIXELS = 11289600
 
439
  primary_device = next(model.parameters()).device
440
  inputs = inputs.to(primary_device)
441
 
442
+ # Generate output - DETERMINISTIC MODE
443
+ # Set seed for complete reproducibility
444
+ torch.manual_seed(42)
445
+ if torch.cuda.is_available():
446
+ torch.cuda.manual_seed_all(42)
447
+
448
  with torch.no_grad():
449
  generated_ids = model.generate(
450
  **inputs,
451
  max_new_tokens=max_new_tokens,
452
+ do_sample=False, # Greedy decoding for deterministic output
453
+ # Remove temperature/top_p/top_k when do_sample=False for consistency
454
  )
455
 
456
  # Decode output
 
476
  def _generate_text_and_confidence_for_crop(
477
  image: Image.Image,
478
  max_new_tokens: int = 128,
 
479
  ) -> Tuple[str, float]:
480
  """Generate text for a cropped region and compute average per-token confidence from model scores.
481
 
 
516
  primary_device = next(model.parameters()).device
517
  inputs = inputs.to(primary_device)
518
 
519
+ # Set seed for deterministic output
520
+ torch.manual_seed(42)
521
+ if torch.cuda.is_available():
522
+ torch.cuda.manual_seed_all(42)
523
+
524
+ # Generate with scores - DETERMINISTIC MODE
525
  with torch.no_grad():
526
  outputs = model.generate(
527
  **inputs,
528
  max_new_tokens=max_new_tokens,
529
+ do_sample=False, # Greedy decoding for deterministic output
 
530
  output_scores=True,
531
  return_dict_in_generate=True,
532
  )
 
1562
  share=False,
1563
  debug=True,
1564
  show_error=True
1565
+ )