Nassira commited on
Commit
ed1f562
·
verified ·
1 Parent(s): 7c234e4

Update ocr_utils.py

Browse files
Files changed (1) hide show
  1. ocr_utils.py +8 -43
ocr_utils.py CHANGED
@@ -1,50 +1,15 @@
 
1
  import logging
2
- from huggingface_hub import hf_hub_download
3
- from llama_cpp import Llama
4
- import json
5
- import re
6
 
7
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8
 
9
- def load_model():
10
- try:
11
- logging.info("Starting model download...")
12
- model_path = hf_hub_download("TheBloke/Mistral-7B-Instruct-v0.2-GGUF", filename="mistral-7b-instruct-v0.2.Q4_K_M.gguf")
13
- logging.info(f"Model downloaded to {model_path}")
14
- logging.info("Initializing Llama model...")
15
- llm = Llama(model_path=model_path, n_ctx=32768, n_gpu_layers=2)
16
- logging.info("Llama model initialized successfully")
17
- return llm
18
- except Exception as e:
19
- logging.error(f"Error loading model: {str(e)}", exc_info=True)
20
- return None
21
 
22
- def get_llm_response(prompt, model):
23
- if model is None:
24
- return {"error": "Model not loaded"}
25
  try:
26
- response = model(prompt, max_tokens=2000)
27
- text_response = response['choices'][0]['text']
28
- logging.info(f"Raw LLM response: {text_response}")
29
-
30
- # Try to find and parse JSON in the response
31
- json_match = re.search(r'\{.*\}', text_response, re.DOTALL)
32
- if json_match:
33
- json_str = json_match.group()
34
- try:
35
- return json.loads(json_str)
36
- except json.JSONDecodeError:
37
- logging.error(f"Failed to parse JSON: {json_str}")
38
- return {"error": "Invalid JSON in LLM response"}
39
- else:
40
- logging.error("No JSON found in LLM response")
41
- return {"error": "No JSON found in LLM response"}
42
  except Exception as e:
43
- logging.error(f"Error in LLM response: {str(e)}")
44
- return {"error": str(e)}
45
-
46
- def test_llm(model):
47
- test_prompt = "What is 2 + 2? Respond with a single number."
48
- response = get_llm_response(test_prompt, model)
49
- logging.info(f"Test LLM response: {response}")
50
- return response
 
1
+ import boto3
2
  import logging
 
 
 
 
3
 
4
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
5
 
6
+ textract_client = boto3.client('textract', region_name='us-west-2')
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ def extract_text_aws(file_content):
 
 
9
  try:
10
+ response = textract_client.detect_document_text(Document={'Bytes': file_content})
11
+ text = ' '.join([item['Text'] for item in response['Blocks'] if item['BlockType'] == 'WORD'])
12
+ return text
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  except Exception as e:
14
+ logging.error(f"Error in AWS Textract: {str(e)}")
15
+ return ""