rgp230 commited on
Commit
ba78ba8
·
1 Parent(s): 9905f36

fix(retrained_distilbert): Fix for tensor memory leaks

Browse files
requirements.txt CHANGED
@@ -10,7 +10,7 @@ langgraph-prebuilt
10
  langchain-tavily
11
  semanticscholar
12
  streamlit
13
- transformers[torch]
14
  langchain_openai
15
  langchain_google_genai
16
  torch
 
10
  langchain-tavily
11
  semanticscholar
12
  streamlit
13
+ transformers==4.55.3
14
  langchain_openai
15
  langchain_google_genai
16
  torch
src/graph/__pycache__/state_vector_nodes.cpython-312.pyc CHANGED
Binary files a/src/graph/__pycache__/state_vector_nodes.cpython-312.pyc and b/src/graph/__pycache__/state_vector_nodes.cpython-312.pyc differ
 
src/graph/state_vector_nodes.py CHANGED
@@ -12,7 +12,7 @@ import re
12
  from langchain_openai import ChatOpenAI
13
  from langchain_community.tools.semanticscholar.tool import SemanticScholarQueryRun
14
  from langchain_community.utilities.semanticscholar import SemanticScholarAPIWrapper
15
- from langchain_community.tools.tavily_search import TavilySearchResults
16
  import pandas as pd
17
  import torch.nn.functional as F
18
  import os
@@ -34,7 +34,7 @@ class question_model:
34
  ])
35
  for topic, keywords in state['topic_kw'].items():
36
  state['messages'].append(SystemMessage(content=f"For the UN SDG Goal: {topic}\n. \
37
- Use the following keywords : {', '.join(keywords)}. Generate questions related to the topic in the country of {state['country']} using these keywords."))
38
  state['messages'].append(AIMessage(content="Based on the provided information, here is an enhanced list of the question: \n"))
39
 
40
  return state
@@ -54,17 +54,7 @@ class question_model:
54
  #print(predict_input)
55
  with torch.no_grad():
56
  logits = self.distilbert_model(**predict_input).logits
57
- #print(logits)
58
- #output = self.distilbert_model(predict_input.numpy())[0]
59
- #print(output)
60
- #numpy_output=output.numpy()
61
- #torch_output=torch.from_numpy(numpy_output)
62
- #prediction_value = torch.argmax(torch_output, dim=1).numpy() # All answers
63
  prob_value=F.softmax(logits, dim=1).cpu().numpy()[0]
64
-
65
- #prob_value = F.softmax(output, dim=1).cpu().numpy()[0]
66
- #prediction_value = tf.argmax(output, axis=1).numpy()#All answers
67
- #prob_value=tf.nn.softmax(output).numpy()[0]#Probability of TF output
68
  Topic_Bool=prob_value>0.4
69
  Topics=[]
70
  Keywords={}
@@ -124,7 +114,6 @@ class research_model:
124
  # Bind the tool to the LLM
125
  self.llm_with_tools = self.llm.bind_tools(self.tools)
126
  os.environ['TAVILY_API_KEY']=tavily_api_key
127
- #self.tavily_api_key=tavily_api_key
128
 
129
  def direct_semantic_scholar_query(self,query: str):
130
 
@@ -143,13 +132,14 @@ class research_model:
143
  def direct_tavily_search(self,query: str):
144
  """Direct invocation of TavilySearchResults without agent"""
145
  # Create the tool directly
146
- tavily = TavilySearchResults()
147
- result = tavily.invoke(query, max_results=5, include_answer=True, include_snippet=True, include_source=True)
148
- response=""
149
- for r in result:
150
- response +="Found a webpage: %s at %s" %(r['title'], r['url'])
151
- response +="Summary of the page: %s" %r['content']
152
- response +="Relevance score: %s" %r['score']
 
153
  return response
154
  def data_analysis(self,state:StateVector):
155
  df_analyst=pd.read_csv(self.local_analysis_file)
@@ -186,8 +176,7 @@ class research_model:
186
 
187
  #AIMessage(content="Using publications on Semantic Scholar and my own reference data, I will answer the questions related to the Sustainable Development Goal: %s." % topic),
188
  SystemMessage(content=f"Search for recent papers on {kw_string} in {country}."),
189
- SystemMessage(content=f"Search for recent news on {kw_string} in {country}."),
190
- SystemMessage(content=f"Search the internet for webpages on {kw_string} in {country}."),
191
  #HumanMessage(content="Please provide a comprehensive answer to the questions based on the information gathered from the tools.")
192
  ]
193
  state['messages'] = messages
@@ -210,9 +199,9 @@ class research_model:
210
  )
211
  self.tools=[semantic_scholar_tool,self.direct_tavily_search]
212
  # Bind the tool to the LLM
213
- llm_with_tools = self.llm.bind_tools(tools)
214
 
215
- return llm_with_tools,tools
216
 
217
 
218
  def tool_calling_llm(self,state:StateVector):
@@ -250,17 +239,8 @@ class research_model:
250
  initial_system_message.content+="\n Assess if the resources indicate a general positive or negative trend and grade progress\
251
  from 0-10 where 0 is very negative and 10 is very positive.\n"
252
  initial_system_message.content+="\n Provide detailed answers to the questions and a list of references used."
 
253
  state["messages"].append(initial_system_message)
254
- '''
255
- llm = ChatOpenAI(
256
- temperature=0.4,
257
- model_name="gpt-4o",
258
- openai_api_key=openai_api_key
259
- )
260
- '''
261
- #llm=ChatGoogleGenerativeAI(model='gemini-2.5-pro',google_api_key=google_api_key,temperature=0.3)
262
-
263
- #print(state["messages"][-1].content)
264
  airesponse = self.llm.invoke(state["messages"][-1].content)
265
  # For simplicity, we just return the messages as they are
266
  return {"messages": [airesponse]}
 
12
  from langchain_openai import ChatOpenAI
13
  from langchain_community.tools.semanticscholar.tool import SemanticScholarQueryRun
14
  from langchain_community.utilities.semanticscholar import SemanticScholarAPIWrapper
15
+ from langchain_tavily import TavilySearch
16
  import pandas as pd
17
  import torch.nn.functional as F
18
  import os
 
34
  ])
35
  for topic, keywords in state['topic_kw'].items():
36
  state['messages'].append(SystemMessage(content=f"For the UN SDG Goal: {topic}\n. \
37
+ Use the following keywords : {', '.join(keywords)}. Generate questions related to the topic in the country of {state['country']} using these keywords.\n"))
38
  state['messages'].append(AIMessage(content="Based on the provided information, here is an enhanced list of the question: \n"))
39
 
40
  return state
 
54
  #print(predict_input)
55
  with torch.no_grad():
56
  logits = self.distilbert_model(**predict_input).logits
 
 
 
 
 
 
57
  prob_value=F.softmax(logits, dim=1).cpu().numpy()[0]
 
 
 
 
58
  Topic_Bool=prob_value>0.4
59
  Topics=[]
60
  Keywords={}
 
114
  # Bind the tool to the LLM
115
  self.llm_with_tools = self.llm.bind_tools(self.tools)
116
  os.environ['TAVILY_API_KEY']=tavily_api_key
 
117
 
118
  def direct_semantic_scholar_query(self,query: str):
119
 
 
132
  def direct_tavily_search(self,query: str):
133
  """Direct invocation of TavilySearchResults without agent"""
134
  # Create the tool directly
135
+ tavily = TavilySearch(max_results=5, include_answer=True, include_snippet=True, include_source=True)
136
+ result = tavily.invoke(query)
137
+ answer=result['answer']
138
+ response="Summary Answer for all webpages: {answer} \n"
139
+ for r in result['results']:
140
+ response +="Found a webpage: %s at %s \n" %(r['title'], r['url'])
141
+ response +="Summary of the page: %s \n" %r['content']
142
+ response +="Relevance score: %s\n" %r['score']
143
  return response
144
  def data_analysis(self,state:StateVector):
145
  df_analyst=pd.read_csv(self.local_analysis_file)
 
176
 
177
  #AIMessage(content="Using publications on Semantic Scholar and my own reference data, I will answer the questions related to the Sustainable Development Goal: %s." % topic),
178
  SystemMessage(content=f"Search for recent papers on {kw_string} in {country}."),
179
+ SystemMessage(content=f"Search the internet for webpages or news on {kw_string} in {country}."),
 
180
  #HumanMessage(content="Please provide a comprehensive answer to the questions based on the information gathered from the tools.")
181
  ]
182
  state['messages'] = messages
 
199
  )
200
  self.tools=[semantic_scholar_tool,self.direct_tavily_search]
201
  # Bind the tool to the LLM
202
+ llm_with_tools = self.llm.bind_tools(self.tools)
203
 
204
+ return llm_with_tools,self.tools
205
 
206
 
207
  def tool_calling_llm(self,state:StateVector):
 
239
  initial_system_message.content+="\n Assess if the resources indicate a general positive or negative trend and grade progress\
240
  from 0-10 where 0 is very negative and 10 is very positive.\n"
241
  initial_system_message.content+="\n Provide detailed answers to the questions and a list of references used."
242
+ print(initial_system_message.content)
243
  state["messages"].append(initial_system_message)
 
 
 
 
 
 
 
 
 
 
244
  airesponse = self.llm.invoke(state["messages"][-1].content)
245
  # For simplicity, we just return the messages as they are
246
  return {"messages": [airesponse]}
src/streamlit_app.py CHANGED
@@ -12,7 +12,9 @@ from state.state import StateVector
12
  from graph.state_vector_nodes import question_model,research_model
13
  from graph.graph_builder import BuildGraphOptions
14
  import re
15
-
 
 
16
  class StreamlitConfigUI:
17
 
18
  """
@@ -97,7 +99,7 @@ if __name__=='__main__':
97
  LLM_Selection=ModelSelection(user_input)
98
  if user_input["GENAI_API_KEY"]:llm=LLM_Selection.setup_llm_model()
99
  loaded_tokenizer = AutoTokenizer.from_pretrained('src/train_bert/topic_classifier_model')
100
- loaded_model = AutoModelForSequenceClassification.from_pretrained('src/train_bert/topic_classifier_model', device_map='cpu')
101
  df_keys=pd.read_csv('src/train_bert/training_data/Keyword_Patterns.csv')
102
 
103
  if not user_input:
 
12
  from graph.state_vector_nodes import question_model,research_model
13
  from graph.graph_builder import BuildGraphOptions
14
  import re
15
+ import os
16
+ import torch
17
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
18
  class StreamlitConfigUI:
19
 
20
  """
 
99
  LLM_Selection=ModelSelection(user_input)
100
  if user_input["GENAI_API_KEY"]:llm=LLM_Selection.setup_llm_model()
101
  loaded_tokenizer = AutoTokenizer.from_pretrained('src/train_bert/topic_classifier_model')
102
+ loaded_model = AutoModelForSequenceClassification.from_pretrained('src/train_bert/topic_classifier_model',device_map='cpu')
103
  df_keys=pd.read_csv('src/train_bert/training_data/Keyword_Patterns.csv')
104
 
105
  if not user_input:
src/train_bert/topic_classifier_model/model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:130c8e5deecc14277276719c7c0e24f836eb2ef7ac7558d591ebbdca195bf536
3
  size 267878708
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:082360600edda6a2ffcf6bc3d16ff21db50214111947c4b970a8fd2c55c10210
3
  size 267878708
src/train_bert/topic_classifier_model/tokenizer.json CHANGED
@@ -6,14 +6,7 @@
6
  "strategy": "LongestFirst",
7
  "stride": 0
8
  },
9
- "padding": {
10
- "strategy": "BatchLongest",
11
- "direction": "Right",
12
- "pad_to_multiple_of": null,
13
- "pad_id": 0,
14
- "pad_type_id": 0,
15
- "pad_token": "[PAD]"
16
- },
17
  "added_tokens": [
18
  {
19
  "id": 0,
 
6
  "strategy": "LongestFirst",
7
  "stride": 0
8
  },
9
+ "padding": null,
 
 
 
 
 
 
 
10
  "added_tokens": [
11
  {
12
  "id": 0,
src/train_bert/topic_classifier_model/training_args.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:065a3a69c45d460583f6e974780e25e39e359f94094ab3d0d2b20048ff15fb8a
3
  size 5777
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78224673c2ba1baa79f14e017c2b6ebe9556c46b018f6441021d05569da25691
3
  size 5777
src/train_bert/train_classifier.py CHANGED
@@ -57,6 +57,7 @@ def buildtraining(train_df, test_df,save_directory='topic_classifier_model'):
57
  num_labels=len(labels),label2id=label2id,id2label=id2label)
58
  trainer = Trainer(
59
  model=model,
 
60
  args=training_args,
61
  train_dataset=tokenized_train,
62
  eval_dataset=tokenized_test,
@@ -109,4 +110,4 @@ if __name__ == '__main__':
109
  buildtraining(train_df, test_df)
110
  prediction_metrics(test_df)
111
 
112
-
 
57
  num_labels=len(labels),label2id=label2id,id2label=id2label)
58
  trainer = Trainer(
59
  model=model,
60
+ device_map='cpu',
61
  args=training_args,
62
  train_dataset=tokenized_train,
63
  eval_dataset=tokenized_test,
 
110
  buildtraining(train_df, test_df)
111
  prediction_metrics(test_df)
112
 
113
+