from __future__ import annotations
from clickhouse_connect.driver import create_client
from daft import col, lit
from daft.las.functions.ark_llm import DoubaoEmbeddingVision
from daft.las.functions.doc import PDFParse
from daft.las.functions.text import PreSignUrlForTos
from daft.las.functions.text.chunk_text_sentence_splitter import ChunkTextSentenceSplitter
from daft.las.functions.udf import las_udf
from daft.las.io.tos import TOSConfig
daft.context.set_runner_ray("ray://127.0.0.1:10001")
@daft.udf(return_dtype=str)
self.bh_client = create_client(
host=os.getenv("bh_host", "host"),
port=os.getenv("bh_port", "8123"),
user=os.getenv("bh_user", "user"),
password=os.getenv("bh_pass", "pass"),
database=os.getenv("bh_database", "database"),
self.table_name = os.getenv("bh_table", "bh_table")
def __call__(self, *columns):
data_to_insert = [col.to_pylist() for col in columns]
rows = list(zip(*data_to_insert))
return ["success"] * len(rows)
def run_pipeline(input_tos_dir, output_tos_path):
input_s3_dir = input_tos_dir.replace("tos://", "s3://", 1)
tos_config = TOSConfig.from_env()
IO_CONFIG = daft.io.IOConfig(s3=tos_config.to_s3_config())
df = daft.from_glob_path(
f"{input_s3_dir}/*.pdf",
lambda p: p.replace("s3://", "tos://", 1),
return_dtype=daft.DataType.string(),
"filename", col("pdf_tos_path").apply(lambda p: p.split("/")[-1], return_dtype=daft.DataType.string())
"output_tos_path": output_tos_path,
)(col("input_pdf_url"), col("filename")),
df = df.with_column("plain_text", col("result").struct.get("parsed_plain_text"))
df = df.with_column("image_filenames", col("result").struct.get("parsed_image_filenames"))
df = df.with_column("parsed_markdown_tos_path", col("result").struct.get("parsed_file_path"))
df_image = df.select("pdf_tos_path", "image_filenames", "parsed_markdown_tos_path").explode("image_filenames")
df_image = df_image.with_column("image_tos_path", output_tos_path + "/images/" + col("image_filenames"))
df_image = df_image.with_column(
)(col("image_tos_path")),
df_image = df_image.with_column(
)(col("image_http_path")),
df_image.with_column("modality", lit("image"))
.with_column("content", lit(""))
.select("pdf_tos_path", "modality", "content", "image_tos_path", "parsed_markdown_tos_path", "embedding")
df_text = df.with_column(
ChunkTextSentenceSplitter,
df_text = df_text.select("pdf_tos_path", "chunks", "parsed_markdown_tos_path").explode("chunks")
df_text = df_text.with_column(
"multimodal_type": "text",
df_text.select("pdf_tos_path", "chunks", "embedding", "parsed_markdown_tos_path")
.with_column("modality", lit("text"))
.with_columns_renamed({"chunks": "content"})
.with_column("image_tos_path", lit(""))
.select("pdf_tos_path", "modality", "content", "image_tos_path", "parsed_markdown_tos_path", "embedding")
df_merged = df_text.concat(df_image)
df_merged = df_merged.filter(col("embedding").list.length() > 0)
df_merged = df_merged.with_column(
ClickhouseWriter(*df_merged.columns),
print(df_merged.filter(col("write_to_bytehouse") == "error").count_rows())
if __name__ == "__main__":
input_tos_dir = os.getenv("input_tos_dir", "input_tos_dir")
output_tos_path = os.getenv("output_tos_path", "output_tos_path")
run_pipeline(input_tos_dir, output_tos_path)