Spaces:
Sleeping
Sleeping
Update utils/DocsLoader.py
Browse files- utils/DocsLoader.py +29 -14
utils/DocsLoader.py
CHANGED
|
@@ -7,6 +7,7 @@ from PIL import Image
|
|
| 7 |
import pytesseract
|
| 8 |
from pptx import Presentation
|
| 9 |
import shutil
|
|
|
|
| 10 |
|
| 11 |
from fastapi import HTTPException
|
| 12 |
|
|
@@ -20,6 +21,12 @@ MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
|
|
| 20 |
|
| 21 |
# for storing chunk (saving timeeeeeeeeeeee)
|
| 22 |
chunk_dict= {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def load_excel(path: str) -> list[Document]:
|
| 25 |
dfs = pd.read_excel(path, sheet_name=None)
|
|
@@ -92,16 +99,7 @@ def load_pptx(path: str) -> list[Document]:
|
|
| 92 |
|
| 93 |
def load_and_chunk(url: str) -> list[Document]:
|
| 94 |
print(url)
|
| 95 |
-
|
| 96 |
-
#for malayam data , i can make the translation by llm in code also but why to call a api if we can saved the translated text for now!!!
|
| 97 |
-
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":
|
| 98 |
-
text = "On August 6, 2025, US President Donald Trump announced that he would impose a 100 percent tariff on imports of computer chips and semiconductors made abroad. However, the tariff would not apply to companies that have committed to manufacturing in the US. The aim of this measure is to boost domestic manufacturing in the US and reduce foreign dependence. While Apple announced a $600 billion investment, the continuation of this windfall opens the door to price increases and anti-trade reactions."
|
| 99 |
-
docs = [Document(page_content=text)]
|
| 100 |
-
full_text = "\n".join([doc.page_content for doc in docs])
|
| 101 |
-
splitter = SentenceTransformersTokenTextSplitter(
|
| 102 |
-
model_name=MODEL_DIR, tokens_per_chunk=512, chunk_overlap=90
|
| 103 |
-
)
|
| 104 |
-
chunk_dict[url] = splitter.create_documents([full_text])
|
| 105 |
if url not in chunk_dict:
|
| 106 |
print("processing new url")
|
| 107 |
resp = requests.get(url)
|
|
@@ -162,13 +160,30 @@ def load_and_chunk(url: str) -> list[Document]:
|
|
| 162 |
if 'tmp_path' in locals() and os.path.exists(tmp_path):
|
| 163 |
os.remove(tmp_path)
|
| 164 |
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
splitter = SentenceTransformersTokenTextSplitter(
|
| 167 |
model_name=MODEL_DIR, tokens_per_chunk=512, chunk_overlap=90
|
| 168 |
)
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
return chunk_dict[url]
|
| 171 |
else:
|
| 172 |
print("stored chunk")
|
| 173 |
-
return chunk_dict[url]
|
| 174 |
-
|
|
|
|
| 7 |
import pytesseract
|
| 8 |
from pptx import Presentation
|
| 9 |
import shutil
|
| 10 |
+
from langdetect import detect
|
| 11 |
|
| 12 |
from fastapi import HTTPException
|
| 13 |
|
|
|
|
| 21 |
|
| 22 |
# for storing chunk (saving timeeeeeeeeeeee)
|
| 23 |
chunk_dict= {}
|
| 24 |
+
|
| 25 |
+
GOOGLE_API_KEY2 = os.getenv("gemini_api_key2")
|
| 26 |
+
llm = ChatGoogleGenerativeAI(
|
| 27 |
+
model="gemini-1.5-flash",
|
| 28 |
+
api_key=GOOGLE_API_KEY2,
|
| 29 |
+
)
|
| 30 |
|
| 31 |
def load_excel(path: str) -> list[Document]:
|
| 32 |
dfs = pd.read_excel(path, sheet_name=None)
|
|
|
|
| 99 |
|
| 100 |
def load_and_chunk(url: str) -> list[Document]:
|
| 101 |
print(url)
|
| 102 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if url not in chunk_dict:
|
| 104 |
print("processing new url")
|
| 105 |
resp = requests.get(url)
|
|
|
|
| 160 |
if 'tmp_path' in locals() and os.path.exists(tmp_path):
|
| 161 |
os.remove(tmp_path)
|
| 162 |
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
min_len = 100 if len(docs[0].page_content) >= 100 else len(docs[0].page_content)
|
| 166 |
+
lang = detect(docs[0].page_content[:min_len])
|
| 167 |
+
|
| 168 |
+
text = ""
|
| 169 |
+
if lang != "en":
|
| 170 |
+
for doc in docs:
|
| 171 |
+
translated = llm.invoke(f"Translate this into English:\n{doc.page_content}")
|
| 172 |
+
text = text + translated.content
|
| 173 |
+
|
| 174 |
+
else:
|
| 175 |
+
full_text = "\n".join([doc.page_content for doc in docs])
|
| 176 |
+
|
| 177 |
splitter = SentenceTransformersTokenTextSplitter(
|
| 178 |
model_name=MODEL_DIR, tokens_per_chunk=512, chunk_overlap=90
|
| 179 |
)
|
| 180 |
+
|
| 181 |
+
if lang !="en":
|
| 182 |
+
chunk_dict[url] = splitter.create_documents([text])
|
| 183 |
+
else:
|
| 184 |
+
chunk_dict[url] = splitter.create_documents([full_text])
|
| 185 |
+
|
| 186 |
return chunk_dict[url]
|
| 187 |
else:
|
| 188 |
print("stored chunk")
|
| 189 |
+
return chunk_dict[url]
|
|
|