Skip to content

Commit f8fc839

Browse files
committed
Update TempFile
1 parent 9746aa2 commit f8fc839

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed
-628 KB
Binary file not shown.

app.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import os
2+
23
os.chdir(os.path.dirname(os.path.abspath(__file__)))
34
os.environ['SERPAPI_API_KEY'] = ''
45

6+
import tempfile
7+
58
import langchain
9+
import streamlit as st
10+
from langchain.cache import InMemoryCache
11+
612
from agent import AgentHelper
713
from docGPT import DocGPT
8-
from langchain.cache import InMemoryCache
914
from model import PDFLoader
1015

11-
import streamlit as st
12-
1316

1417
langchain.llm_cache = InMemoryCache()
1518

@@ -18,7 +21,11 @@
1821
agent_ = None
1922

2023
st.set_page_config(page_title="DocGPT")
21-
st.title('PDF Chatbot')
24+
icon, title = st.columns([3, 20])
25+
with icon:
26+
st.image('./img/chatbot.png')
27+
with title:
28+
st.title('PDF Chatbot')
2229
st.session_state.openai_api_key = None
2330
st.session_state.serpapi_api_key = None
2431

@@ -60,12 +67,19 @@ def load_api_key() -> None:
6067

6168

6269
with st.container():
63-
upload_file = st.file_uploader('#### Choose a PDF file:', type='pdf')
70+
upload_file = st.file_uploader('#### Upload a PDF file:', type='pdf')
6471
if upload_file:
65-
path = os.path.join('uploaded', upload_file.name)
72+
temp_file = tempfile.NamedTemporaryFile(delete=False)
73+
temp_file.write(upload_file.read())
74+
temp_file_path = temp_file.name
6675

67-
docs = PDFLoader.load_documents(path)
76+
docs = PDFLoader.load_documents(temp_file_path)
6877
docs = PDFLoader.split_documents(docs, chunk_size=2500, chunk_overlap=200)
78+
79+
temp_file.close()
80+
if temp_file_path:
81+
os.remove(temp_file_path)
82+
6983
docGPT, docGPT_spec, calculate_tool, search_tool = None, None, None, None
7084

7185
try:
@@ -82,13 +96,12 @@ def load_api_key() -> None:
8296
)
8397
docGPT_spec_tool = agent_.create_doc_chat(docGPT_spec)
8498
except Exception:
85-
st.caption('#### ⚠️ :red[You have not pass OpenAPI key. (Or your api key cannot use.)]')
99+
st.error('#### ⚠️ :red[You have not pass OpenAPI key. (Or your api key cannot use.)]')
86100

87101
try:
88102
search_tool = agent_.get_searp_chain
89103
except Exception as e:
90-
st.write(e)
91-
st.caption('⚠️ You have not pass SEARPAPI key. (Or your api key cannot use.) Try Refresh')
104+
st.warning('⚠️ You have not pass SEARPAPI key. (Or your api key cannot use.) Try Refresh')
92105

93106
try:
94107
calculate_tool = agent_.get_calculate_chain
@@ -99,7 +112,6 @@ def load_api_key() -> None:
99112
]
100113
agent_.initialize(tools)
101114
except Exception:
102-
st.write(e)
103115
pass
104116

105117
st.write('---')
@@ -109,6 +121,7 @@ def load_api_key() -> None:
109121
response = None
110122

111123
if agent_ and query and query != '':
124+
response = 'loading...'
112125
response = agent_.query(query)
113126

114127
st.write('### :blue[Response]:')

img/chatbot.png

120 KB
Loading

model/data_connection.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def get_pdf_files(path: str) -> list:
2020
@staticmethod
2121
def load_documents(pdf_file: str) -> PyMuPDFLoader:
2222
"""Loading and separating each page of a PDF"""
23-
loader = PyMuPDFLoader(f'./PDF/{pdf_file}')
23+
loader = PyMuPDFLoader(f'{pdf_file}')
2424
return loader.load()
2525

2626
@staticmethod
@@ -35,3 +35,28 @@ def split_documents(
3535
)
3636

3737
return splitter.split_documents(document)
38+
39+
40+
class JSONWriter:
41+
def __init__(self, file_name: str) -> None:
42+
self.file_name = file_name
43+
44+
def writer(self, data: list) -> None:
45+
if os.path.exists(self.file_name):
46+
with open(self.file_name) as file:
47+
listObj = json.load(file)
48+
49+
listObj.extend(data)
50+
data = listObj
51+
52+
with open(self.file_name, 'w') as file:
53+
json.dump(
54+
data,
55+
file,
56+
indent=4
57+
)
58+
59+
60+
class LoggerHandle:
61+
def qa_chain(self, chain_type: str, retriever, llm):
62+
pass

0 commit comments

Comments
 (0)