ChintanSatva commited on
Commit
5203be9
·
verified ·
1 Parent(s): 900b5d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, File, UploadFile
2
  import pytesseract
3
  import cv2
4
  import os
@@ -18,29 +18,30 @@ import cachetools
18
  import hashlib
19
  from vllm import LLM
20
 
21
- app = LLMAPI()
22
 
23
  # Configure logging
24
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
25
  logger = logging.getLogger(__name__)
26
 
27
  # Set Tesseract path
28
- pytesseract.pytesseract_cmd = "/usr/bin/tesseract"
29
 
30
  # Initialize BitNet model for CPU-only
31
  try:
32
  llm = LLM(
33
  model="username/bitnet-finetuned-invoice", # Replace with your fine-tuned BitNet model
34
- device="cuda", # Explicitly set to CPU
35
- enforce_eager=True, # Disable CUDA graph compilation for CPU
 
36
  )
37
  except Exception as e:
38
  logger.error(f"Failed to load BitNet model: {str(e)}")
39
- raise RuntimeError("Failed to initialize BitNet model")
40
 
41
- # In-memory caches (1GB TTL)
42
- raw_text_cache = cachetools.TTLTTL(maxsize=100, ttl=3600)
43
- structured_data_cache = cachetools.TTLTTL(maxsize=100, ttl=3600)
44
 
45
  def log_memory_usage():
46
  """Log current memory usage."""
@@ -86,7 +87,7 @@ async def process_pdf_page(img, page_idx):
86
  logger.info(f"Completed OCR for PDF page {page_idx}, took {time.time() - start_time:.2f} seconds, {log_memory_usage()}")
87
  return page_text + "\n"
88
  except Exception as e:
89
- logger.error(f"OCR failed for PDF page {page_idx}: {str(e)}, {log_memory_usage()}")
90
  return ""
91
 
92
  async def process_with_bitnet(filename: str, raw_text: str):
@@ -182,8 +183,8 @@ Output JSON:
182
  }}
183
  }}
184
  """
185
- output = llm.generate([{"role": "user", "content": prompt}])
186
- json_str = output[0].text
187
  json_start = json_str.find("{")
188
  json_end = json_str.rfind("}") + 1
189
  structured_data = json.loads(json_str[json_start:json_end])
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
  import pytesseract
3
  import cv2
4
  import os
 
18
  import hashlib
19
  from vllm import LLM
20
 
21
+ app = FastAPI()
22
 
23
  # Configure logging
24
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
25
  logger = logging.getLogger(__name__)
26
 
27
  # Set Tesseract path
28
+ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
29
 
30
  # Initialize BitNet model for CPU-only
31
  try:
32
  llm = LLM(
33
  model="username/bitnet-finetuned-invoice", # Replace with your fine-tuned BitNet model
34
+ device="cpu",
35
+ enforce_eager=True, # Disable CUDA graph compilation
36
+ max_model_len=2048, # Adjust based on memory (16GB RAM)
37
  )
38
  except Exception as e:
39
  logger.error(f"Failed to load BitNet model: {str(e)}")
40
+ raise HTTPException(status_code=500, detail="BitNet model initialization failed")
41
 
42
+ # In-memory caches (1-hour TTL)
43
+ raw_text_cache = cachetools.TTLCache(maxsize=100, ttl=3600)
44
+ structured_data_cache = cachetools.TTLCache(maxsize=100, ttl=3600)
45
 
46
  def log_memory_usage():
47
  """Log current memory usage."""
 
87
  logger.info(f"Completed OCR for PDF page {page_idx}, took {time.time() - start_time:.2f} seconds, {log_memory_usage()}")
88
  return page_text + "\n"
89
  except Exception as e:
90
+ logger.error(f"OCR failed for PDF page {idx}: {str(e)}, {log_memory_usage()}")
91
  return ""
92
 
93
  async def process_with_bitnet(filename: str, raw_text: str):
 
183
  }}
184
  }}
185
  """
186
+ outputs = llm.generate(prompts=[prompt])
187
+ json_str = outputs[0].outputs[0].text
188
  json_start = json_str.find("{")
189
  json_end = json_str.rfind("}") + 1
190
  structured_data = json.loads(json_str[json_start:json_end])