Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix markdown reader image path #334

Open
wants to merge 4 commits into
base: feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/pai_rag/app/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ async def upload_data(
for file in files:
fn = file.filename
data = await file.read()
file_hash = hashlib.md5(data).hexdigest()
tmp_file_dir = os.path.join(
tmpdir, f"{COMMON_FILE_PATH_FODER_NAME}/{file_hash}"
)
tmp_file_dir = os.path.join(tmpdir, f"{COMMON_FILE_PATH_FODER_NAME}")
os.makedirs(tmp_file_dir, exist_ok=True)
save_file = os.path.join(tmp_file_dir, fn)

Expand Down
6 changes: 1 addition & 5 deletions src/pai_rag/app/api/v1/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Any, List
from fastapi import APIRouter, Body, BackgroundTasks, UploadFile, Form
import uuid
import hashlib
import os
import tempfile
import shutil
Expand Down Expand Up @@ -174,10 +173,7 @@ async def upload_data(
for file in files:
fn = file.filename
data = await file.read()
file_hash = hashlib.md5(data).hexdigest()
tmp_file_dir = os.path.join(
tmpdir, f"{COMMON_FILE_PATH_FODER_NAME}/{file_hash}"
)
tmp_file_dir = os.path.join(tmpdir, f"{COMMON_FILE_PATH_FODER_NAME}")
os.makedirs(tmp_file_dir, exist_ok=True)
save_file = os.path.join(tmp_file_dir, fn)

Expand Down
15 changes: 11 additions & 4 deletions src/pai_rag/integrations/readers/pai_markdown_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ def __init__(
f"PaiMarkdownReader created with enable_table_summary : {self.enable_table_summary}"
)

def replace_image_paths(self, markdown_name: str, content: str):
def replace_image_paths(self, markdown_dir: str, markdown_name: str, content: str):
markdown_image_matches = MARKDOWN_IMAGE_PATTERN.finditer(content)
html_image_matches = HTML_IMAGE_PATTERN.finditer(content)
for match in markdown_image_matches:
full_match = match.group(0) # 整个匹配
local_url = match.group(1) # 捕获的URL
image_name = os.path.basename(local_url)

local_path = os.path.join(markdown_dir, image_name)

if self._oss_cache:
oss_url = self._transform_local_to_oss(markdown_name, local_url)
oss_url = self._transform_local_to_oss(markdown_name, local_path)
if oss_url:
content = content.replace(local_url, oss_url)
else:
Expand All @@ -55,9 +58,12 @@ def replace_image_paths(self, markdown_name: str, content: str):
for match in html_image_matches:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

突然想到,html文件里可能也要做这个处理

full_match = match.group(0) # 整个匹配
local_url = match.group(1) # 捕获的URL
image_name = os.path.basename(local_url)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image有可能会有上层目录,比如"figures/docs/1.jpg"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议这样写

def is_url(url):
    """判断是否为 URL"""
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc])
    except ValueError:
        return False

base_dir = os.path.basedir(markdown_path)
if not is_url(image_path):
    image_path = os.path.join(base_dir, image_path) #绝对路径不会被合并

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image在上传的时候,上层目录没有被保留


local_path = os.path.join(markdown_dir, image_name)

if self._oss_cache:
oss_url = self._transform_local_to_oss(markdown_name, local_url)
oss_url = self._transform_local_to_oss(markdown_name, local_path)
if oss_url:
content = content.replace(local_url, oss_url)
else:
Expand All @@ -78,6 +84,7 @@ def _transform_local_to_oss(self, markdown_name: str, local_url: str):
def parse_markdown(self, markdown_path):
markdown_name = os.path.basename(markdown_path).split(".")[0]
markdown_name = markdown_name.replace(" ", "_")
markdown_dir = os.path.dirname(markdown_path)
text = ""
pre_line = ""
with open(markdown_path) as fp:
Expand Down Expand Up @@ -108,7 +115,7 @@ def parse_markdown(self, markdown_path):
line = fp.readline()

text += pre_line
md_content = self.replace_image_paths(markdown_name, text)
md_content = self.replace_image_paths(markdown_dir, markdown_name, text)
return md_content

def load_data(
Expand Down
Loading