vedsadani commited on
Commit
a3b1b36
·
verified ·
1 Parent(s): 402ebf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -28
app.py CHANGED
@@ -21,30 +21,41 @@ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-
21
  vector_store= FAISS.load_local("vector_db/", embeddings)
22
 
23
  repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1"
24
- llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature": 0.01, "max_new_tokens": 2048})
25
 
26
  retriever = vector_store.as_retriever(
27
  search_type="similarity",
28
- search_kwargs={"k":3, "include_metadata": True})
29
 
30
  df=pd.read_csv('data/Gretel_Data.csv')
31
  averages = df.mean().to_dict()
32
  agent = Agent([df], config={"llm": llm, 'raw_response':True, 'verbose':True})
33
 
34
  global unique_columns
35
- unique_columns = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  global target_words
38
  target_words = ["Bandwidth", "Interference", "Call Quality", "Network", "Handover"]
39
 
40
- global result_dict
41
- result_dict = {}
42
-
43
- global columns
44
- columns=[]
45
 
46
- global column_avgs
47
- column_avgs={}
48
 
49
  global network_features
50
  network_features = {
@@ -98,7 +109,7 @@ network_features = {
98
  'SIP Calls with a Leg',
99
  'SIP_SC_Total_MOU',
100
  'SIP Dropped Calls',
101
- 'Volte_MOU',
102
  'QCI 1 Bearer Drops',
103
  'QCI 1 Bearer Releases',
104
  'QCI 1 Bearer Drop%',
@@ -120,14 +131,15 @@ network_features = {
120
  }
121
 
122
 
 
123
  def echo(message, history):
124
  try:
125
  qa=RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
126
- message= "Your name is Clara. You are a senior telecom network engineer having access to troubleshooting tickets data and other technical and product documentation.Stick to the knowledge from these tickets. Ask clarification questions if needed. "+message
127
- result=qa({"query":message})
128
- bold_answer= "<b>" + result['result'] + "</b>"
129
  for word in target_words:
130
- if re.search(r'\b' + re.escape(word) + r'\b', bold_answer, flags=re.IGNORECASE):
131
  columns.extend(network_features.get(word, []))
132
  unique_columns = list(set(columns))
133
 
@@ -149,10 +161,18 @@ def echo(message, history):
149
  for key in column_avgs:
150
  styled_df = styled_df.applymap(lambda x, k=key: highlight_rows(x, column_avgs[k]), subset=[f'{key}'])
151
 
152
- return "<br></br>"+ bold_answer + "<br></br>" +'1. ' + "<br>" + str(result["source_documents"][0]) +"<br>" + '2. ' + str(result["source_documents"][1]) + "<br>" + "3. " + str(result["source_documents"][2]), styled_df
 
 
 
 
 
 
 
 
153
  except Exception as e:
154
  error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
155
- return error_message
156
 
157
  def echo_agent(message, history):
158
  message="There are 2 df's. If you find a KeyError check for the same in the other df." + "<br>" + message
@@ -160,12 +180,6 @@ def echo_agent(message, history):
160
  with io.StringIO() as buffer:
161
  with contextlib.redirect_stdout(buffer):
162
  result= agent.chat(message)
163
- verbose_output = buffer.getvalue()
164
- verbose_output = verbose_output.replace("\x1b[36;1m\x1b[1;3m", "")
165
- verbose_output = verbose_output.replace("> ", "")
166
- verbose_output = verbose_output.replace("", "")
167
- verbose_output = verbose_output.replace("", "")
168
- result= "<b>" + verbose_output + "<br>" + result + "</b>"
169
  return result
170
  except Exception as e:
171
  error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
@@ -190,17 +204,25 @@ with demo_agent:
190
  with gr.Tab('Clara'):
191
  with gr.Row():
192
  message = gr.Text(label="Input Query")
193
-
194
  btn = gr.Button("Submit")
195
 
196
  with gr.Row():
197
- reply = gr.Text(label="RCA and MoP")
198
-
199
  with gr.Accordion(label = "Metrics", open=False):
200
  table = gr.Dataframe()
201
 
202
  btn.click(echo, inputs=[message], outputs=[reply, table])
203
- gr.Examples(["Wi-Fi connected but no internet showing", "internet stopped working after primary link down", "internet stopped working link not shifted to secondary after primary link down"], inputs=[message])
 
 
 
 
 
 
 
 
204
 
205
  with gr.Tab('Sam'):
206
  with gr.Row():
@@ -211,6 +233,5 @@ with demo_agent:
211
  btn2 = gr.Button("Submit")
212
  btn2.click(echo_agent, inputs=[message_agent], outputs=[reply_agent])
213
 
214
-
215
  if __name__ == "__main__":
216
  demo_agent.launch(share=True,debug=True,auth=("admin", "Sam&Clara"))
 
21
  vector_store= FAISS.load_local("vector_db/", embeddings)
22
 
23
  repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1"
24
+ llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature": 0.01, "max_new_tokens": 4096})
25
 
26
  retriever = vector_store.as_retriever(
27
  search_type="similarity",
28
+ search_kwargs={"k":5, "include_metadata": True})
29
 
30
  df=pd.read_csv('data/Gretel_Data.csv')
31
  averages = df.mean().to_dict()
32
  agent = Agent([df], config={"llm": llm, 'raw_response':True, 'verbose':True})
33
 
34
  global unique_columns
35
+ unique_columns = [
36
+ 'Avg_Connected_UEs',
37
+ 'PRB Util%',
38
+ 'CA Activation Rate',
39
+ 'DLRLCLayerDataVolume MB',
40
+ 'DRB UL Data Volume MB',
41
+ 'UPTP_Mbps',
42
+ 'UPTP Mbps Num',
43
+ 'UPTP Mbps Den',
44
+ 'UL MAC Vol Scell Pct',
45
+ 'DL MAC Vol Scell Pct',
46
+ 'DL MAC Vol Scell MB',
47
+ 'DL Volume',
48
+ 'DL Data Vol MAC in MB',
49
+ 'UL Throughput',
50
+ 'MB_per_connected_UE'
51
+ ]
52
 
53
  global target_words
54
  target_words = ["Bandwidth", "Interference", "Call Quality", "Network", "Handover"]
55
 
56
+ columns = []
 
 
 
 
57
 
58
+ column_avgs = {}
 
59
 
60
  global network_features
61
  network_features = {
 
109
  'SIP Calls with a Leg',
110
  'SIP_SC_Total_MOU',
111
  'SIP Dropped Calls',
112
+ 'VoLTE_MOU',
113
  'QCI 1 Bearer Drops',
114
  'QCI 1 Bearer Releases',
115
  'QCI 1 Bearer Drop%',
 
131
  }
132
 
133
 
134
+
135
  def echo(message, history):
136
  try:
137
  qa=RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
138
+ message= "You are a senior telecom network engineer having access to troubleshooting tickets data and other technical and product documentation. Stick to the knowledge provided. Ask clarification questions if needed. Return only the helpful answer" + message
139
+ result= qa({"query":message})
140
+ answer= result['result'].split('Helpful Answer: ')[1]
141
  for word in target_words:
142
+ if re.search(r'\b' + re.escape(word) + r'\b', answer, flags=re.IGNORECASE):
143
  columns.extend(network_features.get(word, []))
144
  unique_columns = list(set(columns))
145
 
 
161
  for key in column_avgs:
162
  styled_df = styled_df.applymap(lambda x, k=key: highlight_rows(x, column_avgs[k]), subset=[f'{key}'])
163
 
164
+ gr.Dataframe(styled_df)
165
+ return (
166
+ answer + '\n'
167
+ + '\n' + "Sources: \n"
168
+ + '\n' + '1. ' + result['source_documents'][0].metadata['source'] + ' Row: ' +str(result['source_documents'][0].metadata['row']) + '\n' + result['source_documents'][0].page_content + "\n"
169
+ + '\n' + '2. ' + result['source_documents'][1].metadata['source'] + ' Row: ' +str(result['source_documents'][1].metadata['row']) + '\n' + result['source_documents'][1].page_content + "\n"
170
+ + '\n' + "3. " + result['source_documents'][2].metadata['source'] + ' Row: ' +str(result['source_documents'][2].metadata['row']) + '\n' + result['source_documents'][2].page_content,
171
+ styled_df
172
+ )
173
  except Exception as e:
174
  error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
175
+ return error_message, error_message
176
 
177
  def echo_agent(message, history):
178
  message="There are 2 df's. If you find a KeyError check for the same in the other df." + "<br>" + message
 
180
  with io.StringIO() as buffer:
181
  with contextlib.redirect_stdout(buffer):
182
  result= agent.chat(message)
 
 
 
 
 
 
183
  return result
184
  except Exception as e:
185
  error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
 
204
  with gr.Tab('Clara'):
205
  with gr.Row():
206
  message = gr.Text(label="Input Query")
207
+
208
  btn = gr.Button("Submit")
209
 
210
  with gr.Row():
211
+ reply = gr.Text(label="RCA and MoP", autoscroll=False)
212
+
213
  with gr.Accordion(label = "Metrics", open=False):
214
  table = gr.Dataframe()
215
 
216
  btn.click(echo, inputs=[message], outputs=[reply, table])
217
+
218
+ gr.Examples([
219
+ "Wi-Fi connected but no internet showing",
220
+ "internet stopped working after primary link down",
221
+ "internet stopped working link not shifted to secondary after primary link down",
222
+ "What are the network problems faced by people living in the state of California?"
223
+ ],
224
+ inputs=[message]
225
+ )
226
 
227
  with gr.Tab('Sam'):
228
  with gr.Row():
 
233
  btn2 = gr.Button("Submit")
234
  btn2.click(echo_agent, inputs=[message_agent], outputs=[reply_agent])
235
 
 
236
  if __name__ == "__main__":
237
  demo_agent.launch(share=True,debug=True,auth=("admin", "Sam&Clara"))