Skip to content

Commit 853a388

Browse files
committed
Fix Issue#2: Reduce the memory
* Add the `st.cache_resource` avoid memory abnormal increase. * Change the HuggingFaceEmbedding model from default to `multi-qa-MiniLM-L6-cos-v1`. (Lower memory usage!) (see commit: 8fd68f8 ) init the app: 15w add pdf(8.1m): 47w remove pdf: 47w add same pdf(8.1m): 57w remove pdf: 47w add new pdf(8.8m): 58w add original pdf(8.1m): 59w init the app: 15w add pdf(8.1m): 47w remove pdf: 47w add same pdf(8.1m): 47w remove pdf: 47w add new pdf(8.8m): 57w add original pdf(8.1m): 57w --- Reference: [Stackoverflow](https://stackoverflow.com/questions/77013746/how-to-release-memory-correctly-in-streamlit-app/77016325?noredirect=1#comment135777962_7701632501)
1 parent ca02992 commit 853a388

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

app.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,13 @@ def get_response(query: str) -> str:
151151

152152
with doc_container:
153153
docs = upload_and_process_pdf()
154-
model = create_doc_gpt(docs)
155-
del docs
154+
155+
if docs:
156+
model = create_doc_gpt(
157+
docs,
158+
{k: v for k, v in docs[0].metadata.items() if k not in ['source', 'file_path']}
159+
)
160+
del docs
156161
st.write('---')
157162

158163
if 'response' not in st.session_state:

docGPT/__init__.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
module_logger = logger.get_logger(__name__)
1515

1616

17-
def create_doc_gpt(docs):
18-
if not docs:
19-
return
20-
21-
docGPT = DocGPT(docs=docs)
17+
@st.cache_resource(ttl=1800, max_entries=10)
18+
def create_doc_gpt(_docs: list, doc_metadata: str) -> DocGPT:
19+
docGPT = DocGPT(docs=_docs)
2220

2321
try:
2422
if OpenAiAPI.is_valid():
@@ -33,10 +31,8 @@ def create_doc_gpt(docs):
3331
)
3432
docGPT.llm = llm_model
3533
agent_.llm = llm_model
36-
with st.spinner('Running...'):
37-
docGPT.create_qa_chain(
38-
chain_type='refine',
39-
)
34+
35+
docGPT.create_qa_chain(chain_type='refine')
4036
docGPT_tool = agent_.create_doc_chat(docGPT)
4137
calculate_tool = agent_.get_calculate_chain
4238
llm_tool = agent_.create_llm_chain()
@@ -62,10 +58,7 @@ def create_doc_gpt(docs):
6258
]
6359
)
6460
docGPT.llm = llm_model
65-
with st.spinner('Running...(free model will take more time)'):
66-
docGPT.create_qa_chain(
67-
chain_type='refine',
68-
)
61+
docGPT.create_qa_chain(chain_type='refine')
6962
return docGPT
7063
except Exception as e:
7164
module_logger.info(f'{__file__}: {e}')

docGPT/docGPT.py

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def _embeddings(self):
159159
documents=self.docs,
160160
embedding=embeddings
161161
)
162+
print('embedded...')
162163
return db
163164

164165
def create_qa_chain(

0 commit comments

Comments
 (0)