Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234513 unmodified lines1920212223242512 unmodified lines383940414243444546474818 unmodified lines67686970717273232 unmodified lines306307308309310311312import langchainfrom langchain.embeddings import SentenceTransformerEmbeddingsfrom langchain_community.document_loaders import UnstructuredPDFLoader,UnstructuredWordDocumentLoaderfrom langchain.indexes import VectorstoreIndexCreatorfrom langchain.vectorstores import FAISS13 unmodified lines#import faiss as FAISSfrom groq import Groqfrom sentence_transformers import SentenceTransformertokenizer = tiktoken.encoding_for_model("gpt-3.5-turbo")12 unmodified linesseparators=["\n\n", "\n", " ", ""])embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")#embeddings = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')foo = Document(page_content='foo is fou!',metadata={"source":'foo source'})def reset_database(ui_session_id):session_id = f"PDFAISS-{ui_session_id}"18 unmodified linesprint(f"DUPLICATE: Treating: {split_docs[0].metadata['source'].split('/')[-1]}")for i in range(min(3,len(split_docs))):query = split_docs[i].page_contentdocs = db.similarity_search_with_score(query,k=1)_ , score = docs[0]epsilon += scoreprint(f"DUPLICATE: epsilon: {epsilon}")232 unmodified linesprint(f"SESSION: {session_id} database does not exist")return f"SESSION: {session_id} database does not exist","",""docs = db.similarity_search(query, k=5)documents = "\n\n*-*-*-*-*-*\n\n".join(f"Content: {doc.page_content}\n" for doc in docs)system = f"# Instructions\nTake a deep breath and resonate step by step.\nYou are a helpful standard assistant. Your have only one mission and that consists in answering to the user input based on the **provided documents**. If the answer to the question that is asked by the user isn't contained in the **provided documents**, say so but **don't make up an answer**. I chose you because you can say 'I don't know' so please don't do like the other LLMs and don't define acronyms that aren\'t present in the following **PROVIDED DOCUMENTS** double check if it is present before answering. If some of the information can be useful for the user you can tell him.\nFinish your response by **ONE** follow up question that the provided documents could answer.\n\nThe documents are separated by the string \'*-*-*-*-*-*\'. Do not provide any explanations or details.\n\n# **Provided documents**: {documents}."12345613 unmodified lines2021222324252612 unmodified lines3940414243444546474849505152535455565758596018 unmodified lines79808182838485232 unmodified lines318319320321322323324import langchain#from langchain.embeddings import SentenceTransformerEmbeddingsfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.document_loaders import UnstructuredPDFLoader,UnstructuredWordDocumentLoaderfrom langchain.indexes import VectorstoreIndexCreatorfrom langchain.vectorstores import FAISS13 unmodified lines#import faiss as FAISSfrom groq import Groq#from sentence_transformers import SentenceTransformertokenizer = tiktoken.encoding_for_model("gpt-3.5-turbo")12 unmodified linesseparators=["\n\n", "\n", " ", ""])#embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")#embeddings = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2",model_kwargs = {'device': 'cuda'},encode_kwargs = {'normalize_embeddings': False})foo = Document(page_content='foo is fou!',metadata={"source":'foo source'})@spaces.GPUdef similarity_search(db,query, k=5):return db.similarity_search(query, k)@spaces.GPUdef similarity_search_with_score(db,query,k=1):return db.similarity_search_with_score(query,k)def reset_database(ui_session_id):session_id = f"PDFAISS-{ui_session_id}"18 unmodified linesprint(f"DUPLICATE: Treating: {split_docs[0].metadata['source'].split('/')[-1]}")for i in range(min(3,len(split_docs))):query = split_docs[i].page_contentdocs = similarity_search_with_score(db,query,k=1)_ , score = docs[0]epsilon += scoreprint(f"DUPLICATE: epsilon: {epsilon}")232 unmodified linesprint(f"SESSION: {session_id} database does not exist")return f"SESSION: {session_id} database does not exist","",""docs = similarity_search(db,query, k=5)documents = "\n\n*-*-*-*-*-*\n\n".join(f"Content: {doc.page_content}\n" for doc in docs)system = f"# Instructions\nTake a deep breath and resonate step by step.\nYou are a helpful standard assistant. Your have only one mission and that consists in answering to the user input based on the **provided documents**. If the answer to the question that is asked by the user isn't contained in the **provided documents**, say so but **don't make up an answer**. I chose you because you can say 'I don't know' so please don't do like the other LLMs and don't define acronyms that aren\'t present in the following **PROVIDED DOCUMENTS** double check if it is present before answering. If some of the information can be useful for the user you can tell him.\nFinish your response by **ONE** follow up question that the provided documents could answer.\n\nThe documents are separated by the string \'*-*-*-*-*-*\'. Do not provide any explanations or details.\n\n# **Provided documents**: {documents}."
app.py+17-5