Skip to content

Commit f244fb1

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 ) * Using Windows task manager track the memory usages (unit: KB): --- 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 f244fb1

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

app.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,14 @@ 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+
st.session_state.g4f_provider
160+
)
161+
del docs
156162
st.write('---')
157163

158164
if 'response' not in st.session_state:

docGPT/__init__.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
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(
19+
_docs: list,
20+
doc_metadata: str,
21+
g4f_provider: str
22+
) -> DocGPT:
23+
docGPT = DocGPT(docs=_docs)
2224

2325
try:
2426
if OpenAiAPI.is_valid():
@@ -33,10 +35,8 @@ def create_doc_gpt(docs):
3335
)
3436
docGPT.llm = llm_model
3537
agent_.llm = llm_model
36-
with st.spinner('Running...'):
37-
docGPT.create_qa_chain(
38-
chain_type='refine',
39-
)
38+
39+
docGPT.create_qa_chain(chain_type='refine')
4040
docGPT_tool = agent_.create_doc_chat(docGPT)
4141
calculate_tool = agent_.get_calculate_chain
4242
llm_tool = agent_.create_llm_chain()
@@ -58,14 +58,12 @@ def create_doc_gpt(docs):
5858
# Use gpt4free llm model without agent
5959
llm_model = GPT4Free(
6060
provider=GPT4Free().PROVIDER_MAPPING[
61-
st.session_state.g4f_provider
61+
g4f_provider
6262
]
6363
)
64+
print(GPT4Free().PROVIDER_MAPPING[g4f_provider])
6465
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-
)
66+
docGPT.create_qa_chain(chain_type='refine')
6967
return docGPT
7068
except Exception as e:
7169
module_logger.info(f'{__file__}: {e}')

docGPT/docGPT.py

Lines changed: 1 addition & 0 deletions
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)