Spaces:
Runtime error
Runtime error
File size: 2,562 Bytes
47e020e afa9e43 8cd81fc afa9e43 f589e2d 8cd81fc afa9e43 8cd81fc a0b526b afa9e43 8cd81fc afa9e43 a0b526b 8cd81fc afa9e43 8cd81fc a0b526b 8cd81fc afa9e43 35c659a 0f48e10 35c659a 0f48e10 35c659a 0f48e10 35c659a 0f48e10 35c659a afa9e43 | 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 | import os
import json
from sentence_transformers import SentenceTransformer
# os.environ['TRANSFORMERS_CACHE'] = '/documents/'
os.environ['TRANSFORMERS_CACHE'] = str(os.getcwd()) + '/transformers'
os.environ['HUGGINGFACE_HUB_CACHE'] = str(os.getcwd()) + '/transformers'
os.environ['HF_HOME'] = str(os.getcwd()) + '/transformers'
os.environ['XDG_CACHE_HOME'] = str(os.getcwd()) + '/huggingface'
def run_step1():
sentences = ["This is an example sentence", "Each sentence is converted"]
print(f"/*/*/*\n there should be something here: \n {os.getenv('TRANSFORMERS_CACHE')} \n ----- ")
try:
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
except Exception as e:
print(json.dumps({"status": "error on model", "message": str(e)}))
return json.dumps({"status": "error on model", "message": str(e)})
else:
try:
embeddings = model.encode(sentences)
except Exception as e:
print(json.dumps({"status": "error on embedding", "message": str(e)}))
return json.dumps({"status": "error on embedding", "message": str(e)})
else:
print(embeddings)
# from langchain.document_loaders import DirectoryLoader, TextLoader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain.embeddings import HuggingFaceEmbeddings
# from langchain.vectorstores import FAISS
# # Define the directory where the documents are located
# directory = "./documents"
# # Load the documents from the directory using LangChain
# loader = DirectoryLoader(directory, glob="*.txt", loader_cls=TextLoader)
# documents = loader.load()
# # Split the text data into chunks for faster processing
# splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
# texts = splitter.split_documents(documents)
# # Create a Hugging Face Embeddings object with the desired model name and device
# model_name = "sentence-transformers/all-MiniLM-L6-v2"
# device = "cpu"
# print(f'\n ********* \n Model: {model_name}, device: {device} \n documents \n{documents} \n ------------- \n texts \n{texts}')
# # I THINK THIS IS NOT REQUIRE WHEN RUNNING FROM SPACE , model_kwargs={"device": device}
# # embeddings = HuggingFaceEmbeddings(model_name, model_kwargs={"device": device})
# embeddings = HuggingFaceEmbeddings(model_name)
# print(f'\n ********* 4 \n {embeddings}')
# # Use the FAISS library to index the vectors generated by the Hugging Face model
# db = FAISS.from_documents(texts, embeddings)
# db.save_local("faiss_company")
|