ForcePilot/src/utils/__init__.py

41 lines
1.3 KiB
Python
Raw Normal View History

import time
2024-09-09 17:07:03 +08:00
import random
2025-03-04 13:49:00 +08:00
import os
2025-02-27 19:35:25 +08:00
from src.utils.logging_config import logger
2024-07-14 18:31:23 +08:00
def is_text_pdf(pdf_path):
2024-07-16 18:14:27 +08:00
import fitz
2024-07-14 18:31:23 +08:00
doc = fitz.open(pdf_path)
2025-03-11 18:10:31 +08:00
total_pages = len(doc)
if total_pages == 0:
return False
text_pages = 0
for page_num in range(total_pages):
2024-07-14 18:31:23 +08:00
page = doc.load_page(page_num)
text = page.get_text()
if text.strip(): # 检查是否有文本内容
2025-03-11 18:10:31 +08:00
text_pages += 1
# 计算有文本内容的页面比例
text_ratio = text_pages / total_pages
# 如果超过50%的页面有文本内容则认为是文本PDF
return text_ratio > 0.5
def hashstr(input_string, length=8, with_salt=False):
import hashlib
# 添加时间戳作为干扰
if with_salt:
2024-09-09 17:07:03 +08:00
input_string += str(time.time() + random.random())
hash = hashlib.md5(str(input_string).encode()).hexdigest()
2025-03-04 13:49:00 +08:00
return hash[:length]
def get_docker_safe_url(base_url):
if os.getenv("RUNNING_IN_DOCKER") == "true":
# 替换所有可能的本地地址形式
base_url = base_url.replace("http://localhost", "http://host.docker.internal")
base_url = base_url.replace("http://127.0.0.1", "http://host.docker.internal")
logger.info(f"Running in docker, using {base_url} as base url")
return base_url