pattonma commited on
Commit
16d1a5a
1 Parent(s): b745365

file loading testing

Browse files
Files changed (2) hide show
  1. helper_functions.py +23 -9
  2. requirements.txt +1 -0
helper_functions.py CHANGED
@@ -1,6 +1,6 @@
1
  from typing import List
2
  from langchain.agents import AgentExecutor, create_openai_functions_agent
3
- from langchain_community.document_loaders import PyMuPDFLoader, TextLoader, UnstructuredURLLoader, WebBaseLoader
4
  from langchain_community.vectorstores import Qdrant
5
  from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
6
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
@@ -8,20 +8,34 @@ from langchain_core.language_models import BaseLanguageModel
8
  import os
9
  import functools
10
  import requests
 
11
  from chainlit.types import AskFileResponse
12
 
13
  def store_uploaded_file(uploaded_file: AskFileResponse):
14
- file_path = f"./tmp/{uploaded_file.name}"
15
- open(file_path, "wb").write(uploaded_file.content)
16
- return file_path
17
 
18
- def process_file(file_path):
19
- if file_path.endswith(".pdf"):
 
 
 
 
 
 
 
 
 
20
  # Load PDF with PyMuPDFLoader
21
- loader = PyMuPDFLoader(file_path)
22
- elif file_path.endswith(".txt"):
 
 
 
 
 
23
  # Load text file with TextLoader
24
- loader = TextLoader(file_path)
25
  else:
26
  raise ValueError("Unsupported file format. Only PDF and TXT are supported.")
27
 
 
1
  from typing import List
2
  from langchain.agents import AgentExecutor, create_openai_functions_agent
3
+ from langchain_community.document_loaders import PyMuPDFLoader, TextFileLoader, UnstructuredURLLoader, WebBaseLoader
4
  from langchain_community.vectorstores import Qdrant
5
  from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
6
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
 
8
  import os
9
  import functools
10
  import requests
11
+ import tempfile
12
  from chainlit.types import AskFileResponse
13
 
14
  def store_uploaded_file(uploaded_file: AskFileResponse):
15
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".pdf") as temp_file:
16
+ temp_file_path = temp_file.name
 
17
 
18
+ with open(temp_file_path, "wb") as f:
19
+ f.write(uploaded_file.content)
20
+ return temp_file_path
21
+
22
+ def process_file(uploaded_file: AskFileResponse):
23
+ if uploaded_file.name.endswith(".pdf"):
24
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".pdf") as temp_file:
25
+ temp_file_path = temp_file.name
26
+
27
+ with open(temp_file_path, "wb") as f:
28
+ f.write(uploaded_file.content)
29
  # Load PDF with PyMuPDFLoader
30
+ loader = PyMuPDFLoader(temp_file_path)
31
+ elif uploaded_file.name.endswith(".txt"):
32
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as temp_file:
33
+ temp_file_path = temp_file.name
34
+
35
+ with open(temp_file_path, "wb") as f:
36
+ f.write(uploaded_file.content)
37
  # Load text file with TextLoader
38
+ loader = TextFileLoader(temp_file_path)
39
  else:
40
  raise ValueError("Unsupported file format. Only PDF and TXT are supported.")
41
 
requirements.txt CHANGED
@@ -642,6 +642,7 @@ zipp==3.20.2
642
  # importlib-metadata
643
  unstructured
644
  asyncio
 
645
 
646
  # The following packages are considered to be unsafe in a requirements file:
647
  # setuptools
 
642
  # importlib-metadata
643
  unstructured
644
  asyncio
645
+ tempfile
646
 
647
  # The following packages are considered to be unsafe in a requirements file:
648
  # setuptools