1
1
import os
2
+
2
3
os .chdir (os .path .dirname (os .path .abspath (__file__ )))
3
4
os .environ ['SERPAPI_API_KEY' ] = ''
4
5
6
+ import tempfile
7
+
5
8
import langchain
9
+ import streamlit as st
10
+ from langchain .cache import InMemoryCache
11
+
6
12
from agent import AgentHelper
7
13
from docGPT import DocGPT
8
- from langchain .cache import InMemoryCache
9
14
from model import PDFLoader
10
15
11
- import streamlit as st
12
-
13
16
14
17
langchain .llm_cache = InMemoryCache ()
15
18
18
21
agent_ = None
19
22
20
23
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' )
22
29
st .session_state .openai_api_key = None
23
30
st .session_state .serpapi_api_key = None
24
31
@@ -60,12 +67,19 @@ def load_api_key() -> None:
60
67
61
68
62
69
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' )
64
71
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
66
75
67
- docs = PDFLoader .load_documents (path )
76
+ docs = PDFLoader .load_documents (temp_file_path )
68
77
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
+
69
83
docGPT , docGPT_spec , calculate_tool , search_tool = None , None , None , None
70
84
71
85
try :
@@ -82,13 +96,12 @@ def load_api_key() -> None:
82
96
)
83
97
docGPT_spec_tool = agent_ .create_doc_chat (docGPT_spec )
84
98
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.)]' )
86
100
87
101
try :
88
102
search_tool = agent_ .get_searp_chain
89
103
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' )
92
105
93
106
try :
94
107
calculate_tool = agent_ .get_calculate_chain
@@ -99,7 +112,6 @@ def load_api_key() -> None:
99
112
]
100
113
agent_ .initialize (tools )
101
114
except Exception :
102
- st .write (e )
103
115
pass
104
116
105
117
st .write ('---' )
@@ -109,6 +121,7 @@ def load_api_key() -> None:
109
121
response = None
110
122
111
123
if agent_ and query and query != '' :
124
+ response = 'loading...'
112
125
response = agent_ .query (query )
113
126
114
127
st .write ('### :blue[Response]:' )
0 commit comments