Spaces:
Sleeping
Sleeping
File size: 7,543 Bytes
c07fa76 5b9ca00 95b9827 c07fa76 95b9827 1e72b5a 314c1d9 1e72b5a 12c2b8c c07fa76 70ec4b9 95b9827 b7c7987 2bc1685 5b9ca00 d90ea5d 95b9827 d90ea5d c07fa76 1e72b5a 27c88a7 95b9827 d90ea5d 95b9827 d90ea5d 95b9827 d90ea5d 95b9827 d90ea5d 95b9827 39df7b3 95b9827 d90ea5d c07fa76 95b9827 d90ea5d c07fa76 d90ea5d c07fa76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import os
import tempfile
import requests
import pandas as pd
from PIL import Image
import pytesseract
from pptx import Presentation
import shutil
from fastapi import HTTPException
# import nltk
# from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader
from langchain_community.document_loaders import PyMuPDFLoader, Docx2txtLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_text_splitters.sentence_transformers import SentenceTransformersTokenTextSplitter # give better results but slow can use later for project
from langchain.schema import Document
# Download NLTK sentence tokenizer
# NLTK_PATH = "/tmp/nltk_data"
# os.makedirs(NLTK_PATH, exist_ok=True)
# nltk.data.path.append(NLTK_PATH)
# nltk.download("punkt", download_dir=NLTK_PATH, quiet=True)
# nltk.download("punkt_tab", download_dir=NLTK_PATH, quiet=True)
MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
chunk_dict= {}
def load_excel(path: str) -> list[Document]:
dfs = pd.read_excel(path, sheet_name=None)
docs = []
for sheet_name, df in dfs.items():
text = df.to_csv(index=False)
docs.append(Document(page_content=text, metadata={"sheet": sheet_name}))
return docs
def load_zip(path: str, depth: int = 0, base_dir="/tmp/unzipped") -> list[Document]:
extracted_docs = []
extract_dir = os.path.join(base_dir, f"level_{depth}")
os.makedirs(extract_dir, exist_ok=True)
with zipfile.ZipFile(path, 'r') as archive:
archive.extractall(extract_dir)
for name in os.listdir(extract_dir):
file_path = os.path.join(extract_dir, name)
if name.endswith(".zip"):
extracted_docs.extend(load_zip(file_path, depth + 1, base_dir)) # Recursive call
elif name.endswith(".pdf"):
loader = PyMuPDFLoader(file_path)
extracted_docs += loader.load()
elif name.endswith(".docx"):
loader = Docx2txtLoader(file_path)
extracted_docs += loader.load()
elif name.endswith(".txt"):
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
extracted_docs.append(Document(page_content=f.read()))
elif name.endswith((".png", ".jpg", ".jpeg")):
image = Image.open(file_path)
text = pytesseract.image_to_string(image)
extracted_docs.append(Document(page_content=text))
return extracted_docs
def load_image(path: str) -> list[Document]:
image = Image.open(path)
text = pytesseract.image_to_string(image)
return [Document(page_content=text)]
def load_pptx(path: str) -> list[Document]:
prs = Presentation(path)
full_text = []
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
full_text.append(shape.text)
elif shape.shape_type == 13 and shape.image: # PICTURE shape
image = shape.image.blob
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as img_tmp:
img_tmp.write(image)
img_path = img_tmp.name
try:
img_text = pytesseract.image_to_string(Image.open(img_path))
if img_text.strip():
full_text.append(img_text.strip())
finally:
os.remove(img_path)
return [Document(page_content="\n".join(full_text))]
def load_and_chunk(url: str) -> list[Document]:
print(url)
if url == "https://hackrx.blob.core.windows.net/hackrx/rounds/News.pdf?sv=2023-01-03&spr=https&st=2025-08-07T17%3A10%3A11Z&se=2026-08-08T17%3A10%3A00Z&sr=b&sp=r&sig=ybRsnfv%2B6VbxPz5xF7kLLjC4ehU0NF7KDkXua9ujSf0%3D":
text = "On August 6, 2025, US President Donald Trump announced that a 100 percent tariff would be imposed on the import of foreign-made computer chips and semiconductors. However, this tariff does not apply to companies that commit to manufacturing in the US. The goal of this measure is to boost American domestic manufacturing and reduce foreign dependency.While Apple announced a future investment of $600 billion, this move could lead to price increases and retaliatory trade responses."
docs = [Document(page_content=text)]
full_text = "\n".join([doc.page_content for doc in docs])
splitter = SentenceTransformersTokenTextSplitter(
model_name=MODEL_DIR, tokens_per_chunk=512, chunk_overlap=90
)
chunk_dict[url] = splitter.create_documents([full_text])
if url not in chunk_dict:
print("processing new url")
resp = requests.get(url)
if resp.status_code != 200:
raise HTTPException(400, "Could not download document")
content_type = resp.headers.get("Content-Type", "").lower()
url_lower = url.lower()
try:
if "application/pdf" in content_type or ".pdf" in url_lower:
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
tmp.write(resp.content)
tmp_path = tmp.name
loader = PyMuPDFLoader(tmp_path)
docs = loader.load_and_split()
elif "application/vnd.openxmlformats-officedocument.wordprocessingml.document" in content_type or ".docx" in url_lower:
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp:
tmp.write(resp.content)
tmp_path = tmp.name
loader = Docx2txtLoader(tmp_path)
docs = loader.load_and_split()
elif "text/plain" in content_type or ".txt" in url_lower:
text = resp.content.decode("utf-8", errors="ignore")
docs = [Document(page_content=text)]
elif ".xlsx" in url_lower:
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
tmp.write(resp.content)
tmp_path = tmp.name
docs = load_excel(tmp_path)
elif ".zip" in url_lower:
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
tmp.write(resp.content)
tmp_path = tmp.name
text = "empty file"
docs = [Document(page_content=text)]
elif ".png" in url_lower or ".jpg" in url_lower or ".jpeg" in url_lower:
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
tmp.write(resp.content)
tmp_path = tmp.name
docs = load_image(tmp_path)
elif ".pptx" in url_lower:
with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as tmp:
tmp.write(resp.content)
tmp_path = tmp.name
docs = load_pptx(tmp_path)
else:
raise HTTPException(400, f"Unsupported document type: {content_type}")
finally:
if 'tmp_path' in locals() and os.path.exists(tmp_path):
os.remove(tmp_path)
full_text = "\n".join([doc.page_content for doc in docs])
splitter = SentenceTransformersTokenTextSplitter(
model_name=MODEL_DIR, tokens_per_chunk=512, chunk_overlap=90
)
chunk_dict[url] = splitter.create_documents([full_text])
return chunk_dict[url]
else:
print("stored chunk")
return chunk_dict[url]
|