Update embedding.py
Browse files- embedding.py +378 -369
embedding.py
CHANGED
|
@@ -1,370 +1,379 @@
|
|
| 1 |
-
from PyPDF2 import PdfReader
|
| 2 |
-
import requests
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
import concurrent.futures
|
| 6 |
-
import random
|
| 7 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
-
from langchain_community.document_loaders import WebBaseLoader
|
| 9 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 10 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
-
import google.generativeai as genai
|
| 12 |
-
from langchain_core.messages import HumanMessage
|
| 13 |
-
from io import BytesIO
|
| 14 |
-
import numpy as np
|
| 15 |
-
import re
|
| 16 |
-
import torch
|
| 17 |
-
from transformers import AutoTokenizer, AutoModel
|
| 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 |
-
text
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
Tag_History
|
| 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 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
"
|
| 195 |
-
"
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
"
|
| 201 |
-
"
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
"
|
| 206 |
-
"
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
"
|
| 236 |
-
"
|
| 237 |
-
"
|
| 238 |
-
"
|
| 239 |
-
"
|
| 240 |
-
"
|
| 241 |
-
"
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
# print(get_image_embeddings(Product='Samsung Galaxy S24'))
|
|
|
|
| 1 |
+
from PyPDF2 import PdfReader
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import concurrent.futures
|
| 6 |
+
import random
|
| 7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 9 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 10 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
+
import google.generativeai as genai
|
| 12 |
+
from langchain_core.messages import HumanMessage
|
| 13 |
+
from io import BytesIO
|
| 14 |
+
import numpy as np
|
| 15 |
+
import re
|
| 16 |
+
import torch
|
| 17 |
+
from transformers import AutoTokenizer, AutoModel
|
| 18 |
+
# import torch._dynamo
|
| 19 |
+
import time
|
| 20 |
+
# torch._dynamo.config.suppress_errors = True
|
| 21 |
+
|
| 22 |
+
from search import search_images
|
| 23 |
+
|
| 24 |
+
gemini = ChatGoogleGenerativeAI(model="gemini-1.0-pro-001",google_api_key='AIzaSyCo-TeDp0Ou--UwhlTgMwCoTEZxg6-v7wA',temperature = 0.1)
|
| 25 |
+
gemini1 = ChatGoogleGenerativeAI(model="gemini-1.0-pro-001",google_api_key='AIzaSyAtnUk8QKSUoJd3uOBpmeBNN-t8WXBt0zI',temperature = 0.1)
|
| 26 |
+
gemini2 = ChatGoogleGenerativeAI(model="gemini-1.0-pro-001",google_api_key='AIzaSyBzbZQBffHFK3N-gWnhDDNbQ9yZnZtaS2E',temperature = 0.1)
|
| 27 |
+
gemini3 = ChatGoogleGenerativeAI(model="gemini-1.0-pro-001",google_api_key='AIzaSyBNN4VDMAOB2gSZha6HjsTuH71PVV69FLM',temperature = 0.1)
|
| 28 |
+
|
| 29 |
+
vision = ChatGoogleGenerativeAI(model="gemini-1.5-flash",google_api_key='AIzaSyCo-TeDp0Ou--UwhlTgMwCoTEZxg6-v7wA',temperature = 0.1)
|
| 30 |
+
vision1 = ChatGoogleGenerativeAI(model="gemini-1.5-flash",google_api_key='AIzaSyAtnUk8QKSUoJd3uOBpmeBNN-t8WXBt0zI',temperature = 0.1)
|
| 31 |
+
vision2 = ChatGoogleGenerativeAI(model="gemini-1.5-flash",google_api_key='AIzaSyBzbZQBffHFK3N-gWnhDDNbQ9yZnZtaS2E',temperature = 0.1)
|
| 32 |
+
vision3 = ChatGoogleGenerativeAI(model="gemini-1.5-flash",google_api_key='AIzaSyBNN4VDMAOB2gSZha6HjsTuH71PVV69FLM',temperature = 0.1)
|
| 33 |
+
|
| 34 |
+
tokenizer = AutoTokenizer.from_pretrained('dwzhu/e5-base-4k',trust_remote_code = True)
|
| 35 |
+
model = AutoModel.from_pretrained('dwzhu/e5-base-4k',trust_remote_code = True)
|
| 36 |
+
# model = torch.compile(model)
|
| 37 |
+
model.to('cpu') # Ensure the model is on the CPU
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
genai.configure(api_key="AIzaSyAtnUk8QKSUoJd3uOBpmeBNN-t8WXBt0zI")
|
| 41 |
+
|
| 42 |
+
def pdf_extractor(link):
|
| 43 |
+
text = ''
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# Fetch the PDF file from the URL
|
| 47 |
+
response = requests.get(link)
|
| 48 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 49 |
+
|
| 50 |
+
# Use BytesIO to handle the PDF content in memory
|
| 51 |
+
pdf_file = BytesIO(response.content)
|
| 52 |
+
|
| 53 |
+
# Load the PDF file
|
| 54 |
+
reader = PdfReader(pdf_file)
|
| 55 |
+
for page in reader.pages:
|
| 56 |
+
text += page.extract_text() # Extract text from each page
|
| 57 |
+
|
| 58 |
+
except requests.exceptions.HTTPError as e:
|
| 59 |
+
print(f'HTTP error occurred: {e}')
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f'An error occurred: {e}')
|
| 62 |
+
|
| 63 |
+
return text
|
| 64 |
+
|
| 65 |
+
def web_extractor(link):
|
| 66 |
+
text = ''
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
loader = WebBaseLoader(link)
|
| 70 |
+
pages = loader.load_and_split()
|
| 71 |
+
|
| 72 |
+
for page in pages:
|
| 73 |
+
text+=page.page_content
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
|
| 77 |
+
return text
|
| 78 |
+
|
| 79 |
+
def imporve_text(text):
|
| 80 |
+
|
| 81 |
+
prompt = f'''
|
| 82 |
+
Please rewrite the following text to make it short, descriptive, concise, and of high quality.
|
| 83 |
+
Ensure that all essential information is retained.
|
| 84 |
+
Focus on improving clarity, coherence, and word choice without altering the original meaning.
|
| 85 |
+
|
| 86 |
+
text = {text}
|
| 87 |
+
'''
|
| 88 |
+
|
| 89 |
+
model = random.choice([gemini,gemini1,gemini2,gemini3])
|
| 90 |
+
result = model.invoke(prompt)
|
| 91 |
+
|
| 92 |
+
return result.content
|
| 93 |
+
|
| 94 |
+
def feature_extraction(tag, history , context):
|
| 95 |
+
|
| 96 |
+
prompt = f'''
|
| 97 |
+
You are an intelligent assistant tasked with updating product information. You have two data sources:
|
| 98 |
+
1. Tag_History: Previously gathered information about the product.
|
| 99 |
+
2. Tag_Context: New data that might contain additional details.
|
| 100 |
+
Your job is to read the Tag_Context and update the relevant field in the Tag_History with any new details found. The field to be updated is the {tag} FIELD.
|
| 101 |
+
Guidelines:
|
| 102 |
+
- Only add new details that are relevant to the {tag} FIELD.
|
| 103 |
+
- Do not add or modify any other fields in the Tag_History.
|
| 104 |
+
- Ensure your response is in coherent sentences, integrating the new details seamlessly into the existing information.
|
| 105 |
+
Here is the data:
|
| 106 |
+
Tag_Context: {str(context)}
|
| 107 |
+
Tag_History: {history}
|
| 108 |
+
Respond with the updated Tag_History.
|
| 109 |
+
'''
|
| 110 |
+
|
| 111 |
+
model = random.choice([gemini,gemini1,gemini2,gemini3])
|
| 112 |
+
result = model.invoke(prompt)
|
| 113 |
+
|
| 114 |
+
return result.content
|
| 115 |
+
|
| 116 |
+
def feature_extraction_image(url):
|
| 117 |
+
text = ' '
|
| 118 |
+
model = genai.GenerativeModel('gemini-1.5-flash-001')
|
| 119 |
+
try:
|
| 120 |
+
res = model.generate_content(['Describe this image to me',url])
|
| 121 |
+
text = res.text
|
| 122 |
+
|
| 123 |
+
except:
|
| 124 |
+
pass
|
| 125 |
+
return text
|
| 126 |
+
|
| 127 |
+
def detailed_feature_extraction(find, context):
|
| 128 |
+
|
| 129 |
+
prompt = f'''
|
| 130 |
+
You are an intelligent assistant tasked with finding product information. You have one data source and one output format:
|
| 131 |
+
1. Context: The gathered information about the product.
|
| 132 |
+
2. Format: Details which need to be filled based on Context.
|
| 133 |
+
Your job is to read the Context and update the relevant field in Format using Context.
|
| 134 |
+
Guidelines:
|
| 135 |
+
- Only add details that are relevant to the individual FIELD.
|
| 136 |
+
- Do not add or modify any other fields in the Format.
|
| 137 |
+
- If nothing found return None.
|
| 138 |
+
Here is the data:
|
| 139 |
+
The Context is {str(context)}
|
| 140 |
+
The Format is {str(find)}
|
| 141 |
+
'''
|
| 142 |
+
|
| 143 |
+
model = random.choice([gemini,gemini1,gemini2,gemini3])
|
| 144 |
+
result = model.invoke(prompt)
|
| 145 |
+
|
| 146 |
+
return result.content
|
| 147 |
+
|
| 148 |
+
def detailed_history(history):
|
| 149 |
+
|
| 150 |
+
details = {
|
| 151 |
+
"Introduction": {
|
| 152 |
+
"Product Name": None,
|
| 153 |
+
"Overview of the product": None,
|
| 154 |
+
"Purpose of the manual": None,
|
| 155 |
+
"Audience": None,
|
| 156 |
+
"Additional Details": None
|
| 157 |
+
},
|
| 158 |
+
"Specifications": {
|
| 159 |
+
"Technical specifications": None,
|
| 160 |
+
"Performance metrics": None,
|
| 161 |
+
"Additional Details": None
|
| 162 |
+
},
|
| 163 |
+
"Product Overview": {
|
| 164 |
+
"Product features": None,
|
| 165 |
+
"Key components and parts": None,
|
| 166 |
+
"Additional Details": None
|
| 167 |
+
},
|
| 168 |
+
"Safety Information": {
|
| 169 |
+
"Safety warnings and precautions": None,
|
| 170 |
+
"Compliance and certification information": None,
|
| 171 |
+
"Additional Details": None
|
| 172 |
+
},
|
| 173 |
+
"Installation Instructions": {
|
| 174 |
+
"Unboxing and inventory checklist": None,
|
| 175 |
+
"Step-by-step installation guide": None,
|
| 176 |
+
"Required tools and materials": None,
|
| 177 |
+
"Additional Details": None
|
| 178 |
+
},
|
| 179 |
+
"Setup and Configuration": {
|
| 180 |
+
"Initial setup procedures": None,
|
| 181 |
+
"Configuration settings": None,
|
| 182 |
+
"Troubleshooting setup issues": None,
|
| 183 |
+
"Additional Details": None
|
| 184 |
+
},
|
| 185 |
+
"Operation Instructions": {
|
| 186 |
+
"How to use the product": None,
|
| 187 |
+
"Detailed instructions for different functionalities": None,
|
| 188 |
+
"User interface guide": None,
|
| 189 |
+
"Additional Details": None
|
| 190 |
+
},
|
| 191 |
+
"Maintenance and Care": {
|
| 192 |
+
"Cleaning instructions": None,
|
| 193 |
+
"Maintenance schedule": None,
|
| 194 |
+
"Replacement parts and accessories": None,
|
| 195 |
+
"Additional Details": None
|
| 196 |
+
},
|
| 197 |
+
"Troubleshooting": {
|
| 198 |
+
"Common issues and solutions": None,
|
| 199 |
+
"Error messages and their meanings": None,
|
| 200 |
+
"Support Information": None,
|
| 201 |
+
"Additional Details": None
|
| 202 |
+
},
|
| 203 |
+
"Warranty Information": {
|
| 204 |
+
"Terms and Conditions": None,
|
| 205 |
+
"Service and repair information": None,
|
| 206 |
+
"Additional Details": None
|
| 207 |
+
},
|
| 208 |
+
"Legal Information": {
|
| 209 |
+
"Copyright information": None,
|
| 210 |
+
"Trademarks and patents": None,
|
| 211 |
+
"Disclaimers": None,
|
| 212 |
+
"Additional Details": None
|
| 213 |
+
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
for key,val in history.items():
|
| 218 |
+
|
| 219 |
+
find = details[key]
|
| 220 |
+
|
| 221 |
+
details[key] = str(detailed_feature_extraction(find,val))
|
| 222 |
+
|
| 223 |
+
return details
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def get_embeddings(link,tag_option):
|
| 227 |
+
|
| 228 |
+
print(f"\n--> Creating Embeddings - {link}")
|
| 229 |
+
|
| 230 |
+
if tag_option=='Complete Document Similarity':
|
| 231 |
+
history = { "Details": "" }
|
| 232 |
+
|
| 233 |
+
else:
|
| 234 |
+
history = {
|
| 235 |
+
"Introduction": "",
|
| 236 |
+
"Specifications": "",
|
| 237 |
+
"Product Overview": "",
|
| 238 |
+
"Safety Information": "",
|
| 239 |
+
"Installation Instructions": "",
|
| 240 |
+
"Setup and Configuration": "",
|
| 241 |
+
"Operation Instructions": "",
|
| 242 |
+
"Maintenance and Care": "",
|
| 243 |
+
"Troubleshooting": "",
|
| 244 |
+
"Warranty Information": "",
|
| 245 |
+
"Legal Information": ""
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
# Extract Text -----------------------------
|
| 249 |
+
print("Extracting Text")
|
| 250 |
+
if link[-3:] == '.md' or link[8:11] == 'en.':
|
| 251 |
+
text = web_extractor(link)
|
| 252 |
+
else:
|
| 253 |
+
text = pdf_extractor(link)
|
| 254 |
+
|
| 255 |
+
# Create Chunks ----------------------------
|
| 256 |
+
print("Writing Tag Data")
|
| 257 |
+
|
| 258 |
+
if tag_option=="Complete Document Similarity":
|
| 259 |
+
history["Details"] = feature_extraction("Details", history["Details"], text[0][:50000])
|
| 260 |
+
|
| 261 |
+
else:
|
| 262 |
+
chunks = text_splitter.create_documents(text)
|
| 263 |
+
|
| 264 |
+
for chunk in chunks:
|
| 265 |
+
|
| 266 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 267 |
+
future_to_key = {
|
| 268 |
+
executor.submit(
|
| 269 |
+
feature_extraction, f"Product {key}", history[key], chunk.page_content
|
| 270 |
+
): key for key in history
|
| 271 |
+
}
|
| 272 |
+
for future in concurrent.futures.as_completed(future_to_key):
|
| 273 |
+
key = future_to_key[future]
|
| 274 |
+
try:
|
| 275 |
+
response = future.result()
|
| 276 |
+
history[key] = response
|
| 277 |
+
except Exception as e:
|
| 278 |
+
print(f"Error processing {key}: {e}")
|
| 279 |
+
|
| 280 |
+
print("Creating Vectors")
|
| 281 |
+
genai_embeddings=[]
|
| 282 |
+
|
| 283 |
+
for tag in history:
|
| 284 |
+
result = genai.embed_content(
|
| 285 |
+
model="models/embedding-001",
|
| 286 |
+
content=history[tag],
|
| 287 |
+
task_type="retrieval_document")
|
| 288 |
+
genai_embeddings.append(result['embedding'])
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
return history,genai_embeddings
|
| 292 |
+
|
| 293 |
+
def get_embed_chroma(link):
|
| 294 |
+
|
| 295 |
+
print(f"\n--> Creating Embeddings - {link}")
|
| 296 |
+
|
| 297 |
+
# Extract Text -----------------------------
|
| 298 |
+
if link[-3:] == '.md' or link[8:11] == 'en.':
|
| 299 |
+
text = web_extractor(link)
|
| 300 |
+
else:
|
| 301 |
+
text = pdf_extractor(link)
|
| 302 |
+
print("\u2713 Extracting Text")
|
| 303 |
+
|
| 304 |
+
# Create Chunks ----------------------------
|
| 305 |
+
|
| 306 |
+
text = re.sub(r'\.{2,}', '.', text)
|
| 307 |
+
text = re.sub(r'\s{2,}', ' ', text)
|
| 308 |
+
text = [re.sub(r'\n{2,}', '\n', text)]
|
| 309 |
+
|
| 310 |
+
chunks = text_splitter_small.create_documents(text)
|
| 311 |
+
print("\u2713 Writing Tag Data")
|
| 312 |
+
|
| 313 |
+
# Creating Vector
|
| 314 |
+
embedding_vectors=[]
|
| 315 |
+
textual_data = []
|
| 316 |
+
print("\u2713 Creating Vectors")
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
batch_size = 1
|
| 320 |
+
# Process chunks in batches
|
| 321 |
+
for i in range(0, len(chunks), batch_size):
|
| 322 |
+
batch = chunks[i:i + batch_size]
|
| 323 |
+
|
| 324 |
+
texts = [text.page_content for text in batch]
|
| 325 |
+
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
| 326 |
+
inputs = {k: v.to('cpu') for k, v in inputs.items()}
|
| 327 |
+
|
| 328 |
+
# Get the model's outputs
|
| 329 |
+
with torch.no_grad():
|
| 330 |
+
outputs = model(**inputs)
|
| 331 |
+
|
| 332 |
+
embeddings = outputs.last_hidden_state.mean(dim=1).cpu().numpy().tolist()
|
| 333 |
+
embedding_vectors.extend(embeddings)
|
| 334 |
+
textual_data.extend(texts)
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
return textual_data , embedding_vectors
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def get_image_embeddings(Product):
|
| 342 |
+
image_embeddings = []
|
| 343 |
+
|
| 344 |
+
links = search_images(Product)
|
| 345 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 346 |
+
descriptions = list(executor.map(feature_extraction_image, links))
|
| 347 |
+
|
| 348 |
+
for description in descriptions:
|
| 349 |
+
result = genai.embed_content(
|
| 350 |
+
model="models/embedding-001",
|
| 351 |
+
content=description,
|
| 352 |
+
task_type="retrieval_document")
|
| 353 |
+
|
| 354 |
+
image_embeddings.append(result['embedding'])
|
| 355 |
+
# print(image_embeddings)
|
| 356 |
+
return image_embeddings , links
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
global text_splitter
|
| 361 |
+
global data
|
| 362 |
+
global history
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 366 |
+
chunk_size = 10000,
|
| 367 |
+
chunk_overlap = 100,
|
| 368 |
+
separators = ["",''," "]
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
+
text_splitter_small = RecursiveCharacterTextSplitter(
|
| 372 |
+
chunk_size = 2000,
|
| 373 |
+
chunk_overlap = 100,
|
| 374 |
+
separators = ["",''," "]
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
if __name__ == '__main__':
|
| 378 |
+
print(get_embed_chroma('https://www.galaxys24manual.com/wp-content/uploads/pdf/galaxy-s24-manual-SAM-S921-S926-S928-OS14-011824-FINAL-US-English.pdf'))
|
| 379 |
# print(get_image_embeddings(Product='Samsung Galaxy S24'))
|