Spaces:
Sleeping
Sleeping
added pdf
Browse filesFiles changed (3)
- Dockerfile+1-1
- aimakerspace/text_utils.py+19-1
- app.py+25-4
1234FROM python:3.9RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \1234FROM python:3.11RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \
123457 unmodified lines1314151617183 unmodified lines2223242526272 unmodified lines30313233343538 unmodified lines74757677import osfrom typing import Listclass TextFileLoader:7 unmodified linesself.load_directory()elif os.path.isfile(self.path) and self.path.endswith(".txt"):self.load_file()else:raise ValueError("Provided path is neither a valid directory nor a .txt file."3 unmodified lineswith open(self.path, "r", encoding=self.encoding) as f:self.documents.append(f.read())def load_directory(self):for root, _, files in os.walk(self.path):for file in files:2 unmodified linesos.path.join(root, file), "r", encoding=self.encoding) as f:self.documents.append(f.read())def load_documents(self):self.load()38 unmodified linesprint("--------")print(chunks[-2])print("--------")print(chunks[-1])1234567 unmodified lines14151617181920213 unmodified lines252627282930313233343536372 unmodified lines404142434445464748495051525338 unmodified lines92939495import osfrom typing import Listimport fitzclass TextFileLoader:7 unmodified linesself.load_directory()elif os.path.isfile(self.path) and self.path.endswith(".txt"):self.load_file()elif os.path.isfile(self.path) and self.path.endswith(".pdf"):self.load_pdf()else:raise ValueError("Provided path is neither a valid directory nor a .txt file."3 unmodified lineswith open(self.path, "r", encoding=self.encoding) as f:self.documents.append(f.read())def load_pdf(self):doc = fitz.open(self.path)text = ""for page in doc:text += page.get_text()self.documents.append(text)def load_directory(self):for root, _, files in os.walk(self.path):for file in files:2 unmodified linesos.path.join(root, file), "r", encoding=self.encoding) as f:self.documents.append(f.read())if file.endswith(".pdf"):doc = fitz.open(os.path.join(root, file))text = ""for page in doc:text += page.get_text()self.documents.append(text)def load_documents(self):self.load()38 unmodified linesprint("--------")print(chunks[-2])print("--------")print(chunks[-1])
10 unmodified lines11121314151647 unmodified lines64656667686970717273747576777879808182832 unmodified lines8687888990919210 unmodified linesfrom aimakerspace.vectordatabase import VectorDatabasefrom aimakerspace.openai_utils.chatmodel import ChatOpenAIimport chainlit as clsystem_template = """\Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."""47 unmodified linestexts = text_splitter.split_texts(documents)return texts@cl.on_chat_startasync def on_chat_start():files = None# Wait for the user to upload a filewhile files == None:files = await cl.AskFileMessage(content="Please upload a Text File file to begin!",accept=["text/plain"],max_size_mb=2,timeout=180,).send()file = files[0]msg = cl.Message(2 unmodified linesawait msg.send()# load the filetexts = process_text_file(file)print(f"Processing {len(texts)} text chunks")10 unmodified lines1112131415161747 unmodified lines65666768697071727374757677787980818283848586878889909192939495969798991001012 unmodified lines10410510610710810911011111211310 unmodified linesfrom aimakerspace.vectordatabase import VectorDatabasefrom aimakerspace.openai_utils.chatmodel import ChatOpenAIimport chainlit as climport fitzsystem_template = """\Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."""47 unmodified linestexts = text_splitter.split_texts(documents)return textsdef process_pdf_file(file: AskFileResponse):import tempfilewith tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".pdf") as temp_file:temp_file_path = temp_file.namewith open(temp_file_path, "wb") as f:f.write(file.content)text_loader = TextFileLoader(temp_file_path)documents = text_loader.load_documents()texts = text_splitter.split_texts(documents)return texts@cl.on_chat_startasync def on_chat_start():files = None# Wait for the user to upload a filewhile files == None :files = await cl.AskFileMessage(content="Please upload a Text or PDF file to begin!",accept=["text/plain", "application/pdf"],max_size_mb=2,timeout=180,).send()file = files[0]msg = cl.Message(2 unmodified linesawait msg.send()# load the fileif file.name.endswith('.pdf'):texts = process_pdf_file(file)else:texts = process_text_file(file)print(f"Processing {len(texts)} text chunks")
Dockerfile+1-1
aimakerspace/text_utils.py+19-1
app.py+25-4