Spaces:
Runtime error
Runtime error
Download utilities/step1.py from BFManza/FastAPITest1: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/spaces/BFManza/FastAPITest1/resolve/main/utilities/step1.py
- Command line
-
hf download hf://spaces/BFManza/FastAPITest1/utilities/step1.py
-
curl -L -o step1.py https://huggingface.co/spaces/BFManza/FastAPITest1/resolve/main/utilities/step1.py
2.56 kB
| 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") | |