Spaces:
Sleeping
Sleeping
File size: 7,390 Bytes
c07fa76 5b9ca00 95b9827 a38851f 95b9827 c07fa76 9360a18 314c1d9 1e72b5a 12c2b8c c07fa76 2b6c6a2 b7c7987 9360a18 2bc1685 5b9ca00 9360a18 d90ea5d a38851f 7b70c37 a38851f d90ea5d 95b9827 9360a18 95b9827 d90ea5d c07fa76 1e72b5a a38851f 95b9827 d90ea5d 95b9827 d90ea5d 95b9827 d90ea5d 95b9827 d90ea5d 95b9827 39df7b3 95b9827 d90ea5d c07fa76 a38851f 95b9827 a38851f d90ea5d c07fa76 d90ea5d a38851f | 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import os
import tempfile
import requests
import pandas as pd
from PIL import Image
import pytesseract
from pptx import Presentation
import shutil
from langdetect import detect
from fastapi import HTTPException
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
from langchain_google_genai import ChatGoogleGenerativeAI
# directory for saved model for SentenceTransformersTokenTextSplitter
MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
# for storing chunk (saving timeeeeeeeeeeee)
chunk_dict= {}
GOOGLE_API_KEY2 = os.getenv("gemini_api_key2")
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
api_key=GOOGLE_API_KEY2,
)
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
""" I am not using zip further as no questions were asked and also the nested zip has no data .
i did not able to submit this coded approaches for differnet types of files due to sudden change of level.
do not delete it , may be all levels will reopened on last day"""
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 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)
min_len = 100 if len(docs[0].page_content) >= 100 else len(docs[0].page_content)
lang = detect(docs[0].page_content[:min_len])
text = ""
if lang != "en":
for doc in docs:
translated = llm.invoke(f"Translate this into English:\n{doc.page_content}")
text = text + translated.content
else:
full_text = "\n".join([doc.page_content for doc in docs])
splitter = SentenceTransformersTokenTextSplitter(
model_name=MODEL_DIR, tokens_per_chunk=512, chunk_overlap=90
)
if lang !="en":
chunk_dict[url] = splitter.create_documents([text])
else:
chunk_dict[url] = splitter.create_documents([full_text])
return chunk_dict[url]
else:
print("stored chunk")
return chunk_dict[url] |