Spaces:
Runtime error
Runtime error
Update utility/utils.py
Browse files42 unmodified lines4344454647484950515253545556575859606146 unmodified lines1081091101111121131141151161171184 unmodified lines1231241251261271281291301311321331342 unmodified lines137138139140141142143144145146147148149150151166 unmodified lines31831932032132232332432532632732832933033153 unmodified lines38538638738842 unmodified lines# Initialize the InferenceClientclient = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.3", token=HFT)# Specify a custom model storage directory (ensure this path is writable)#model_storage_directory = '/app/models'# Create the reader object and set the model storage directory#reader = easyocr.Reader(['en'], model_storage_directory=model_storage_directory)def draw_boxes(image, bounds, color='red', width=2):draw = ImageDraw.Draw(image)for bound in bounds:p0, p1, p2, p3 = bound[0]draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)return image# Load image using OpenCVdef load_image(image_path):image = cv2.imread(image_path)46 unmodified linesreturn final_imagedef ocr_with_paddle(img):finaltext = ''#model_dir = os.getenv('PADDLEOCR_MODEL_DIR', '/tmp/.paddleocr')#ocr = PaddleOCR(lang='en', use_angle_cls=True, det_model_dir=model_dir)#ocr = PaddleOCR(lang='en', use_angle_cls=True, det_model_dir=os.environ['PADDLEOCR_HOME'])logging.info(f"PADDLEOCR_HOME: {os.environ['PADDLEOCR_HOME']}")ocr = PaddleOCR(lang='en',4 unmodified lines)result = ocr.ocr(img)for i in range(len(result[0])):text = result[0][i][1][0]finaltext += ' ' + textreturn finaltextdef extract_text_from_images(image_paths):all_extracted_texts = {}all_extracted_imgs = {}2 unmodified lines# Enhance the image before OCRenhanced_image = process_image(image_path, scale=2)# Draw boxes on the processed image (optional, requires bounds)img_result = Image.fromarray(enhanced_image)result_image_path = os.path.join(RESULT_FOLDER, f'result_{os.path.basename(image_path)}')img_result.save(result_image_path) # Save the processed image# Perform OCR on the enhanced imageresult = ocr_with_paddle(enhanced_image)all_extracted_texts[image_path] = resultall_extracted_imgs[image_path] = result_image_pathexcept ValueError as ve:166 unmodified lines# Email regexemail_regex = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b')# Profile links regex, updated to avoid conflicts with email domains#link_regex = re.compile(r'\b(?:https?://)?(?:www\.)?(?:linkedin\.com|github\.com|indeed\.com|[A-Za-z0-9-]+\.[A-Za-z]{2,})[\w./?-]*\b')#link_regex = re.compile(r'\b(?:https?://)?(?:www\.)?[a-zA-Z0-9-]+\.(?:[a-zA-Z]{2,})(?:\.[a-zA-Z]{2,})?(?:\.[a-zA-Z]{2,})?(?:[/\w.-]*)*[\w/?&=-]*\b')link_regex = re.compile(r'\b(?:https?:\/\/)?(?:www\.)[a-zA-Z0-9-]+\.(?:com|co\.in|co|io|org|net|edu|gov|mil|int|uk|us|in|de|au|app|tech|xyz|info|biz|fr|dev)\b')# Find all matches in the textphone_numbers = [num for num in combined_phone_regex.findall(text) if len(num) >= 5]print("phone_numbers--->",phone_numbers)emails = email_regex.findall(text)links_RE = [link for link in link_regex.findall(text) if len(link)>=11]# Remove profile links that might conflict with emails53 unmodified linesprocessed_data['email'].extend(cont_data.get("emails", []))processed_data['contact_number'].extend(cont_data.get("phone_numbers", []))processed_data['Link'].extend(cont_data.get("links_RE", []))return processed_data42 unmodified lines43444546474846 unmodified lines95969798991001011021031044 unmodified lines1091101111121131141151161171181191201211221231241251261271281291302 unmodified lines133134135136137138139140141142143144145146147148149150166 unmodified lines31731831932032132232332432532632732832933053 unmodified lines38438538638742 unmodified lines# Initialize the InferenceClientclient = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.3", token=HFT)# Load image using OpenCVdef load_image(image_path):image = cv2.imread(image_path)46 unmodified linesreturn final_image# Function for OCR with PaddleOCR, returning both text and bounding boxesdef ocr_with_paddle(img):final_text = ''logging.info(f"PADDLEOCR_HOME: {os.environ['PADDLEOCR_HOME']}")ocr = PaddleOCR(lang='en',4 unmodified lines)result = ocr.ocr(img)boxes = []for line in result[0]:box, text, _ = lineboxes.append(box) # Append the bounding boxfinal_text += ' ' + textreturn final_text, boxes# Function to draw bounding boxes around textdef draw_boxes(image, boxes):draw = ImageDraw.Draw(image)for box in boxes:draw.polygon(box, outline="red", width=3)return image# Extract text and create a result image with bounding boxesdef extract_text_from_images(image_paths):all_extracted_texts = {}all_extracted_imgs = {}2 unmodified lines# Enhance the image before OCRenhanced_image = process_image(image_path, scale=2)# Perform OCR on the enhanced image and get boxesresult, boxes = ocr_with_paddle(enhanced_image)# Draw bounding boxes on the processed imageimg_result = Image.fromarray(enhanced_image)img_with_boxes = draw_boxes(img_result, boxes)# Save the image with boxesresult_image_path = os.path.join(RESULT_FOLDER, f'result_{os.path.basename(image_path)}')img_with_boxes.save(result_image_path)# Store the text and image result pathsall_extracted_texts[image_path] = resultall_extracted_imgs[image_path] = result_image_pathexcept ValueError as ve:166 unmodified lines# Email regexemail_regex = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b')# URL and links regex, updated to avoid conflicts with email domainslink_regex = re.compile(r'\b(?:https?:\/\/)?(?:www\.)[a-zA-Z0-9-]+\.(?:com|co\.in|co|io|org|net|edu|gov|mil|int|uk|us|in|de|au|app|tech|xyz|info|biz|fr|dev)\b')# Find all matches in the textphone_numbers = [num for num in combined_phone_regex.findall(text) if len(num) >= 5]emails = email_regex.findall(text)links_RE = [link for link in link_regex.findall(text) if len(link)>=11]# Remove profile links that might conflict with emails53 unmodified linesprocessed_data['email'].extend(cont_data.get("emails", []))processed_data['contact_number'].extend(cont_data.get("phone_numbers", []))processed_data['Link'].extend(cont_data.get("links_RE", []))return processed_data
utility/utils.py+30-31