ShivanshMathur007 commited on
Commit
7724e60
1 Parent(s): 0dab347

create app.py

Browse files
Files changed (1) hide show
  1. app.py +238 -0
app.py ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.vectorstores import FAISS
2
+ from langchain.chains import RetrievalQA
3
+ import gradio as gr
4
+ import os
5
+ from pandasai import Agent
6
+ import io
7
+ import contextlib
8
+ import re
9
+ import pandas as pd
10
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
11
+ from langchain_google_genai import ChatGoogleGenerativeAI
12
+
13
+
14
+ gemini_embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
15
+
16
+ vector_store = FAISS.load_local("jio_tickets_faiss/", embeddings=gemini_embeddings,allow_dangerous_deserialization=True)
17
+
18
+ llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro-001",
19
+ temperature=0.01)
20
+
21
+
22
+ retriever = vector_store.as_retriever(
23
+ search_type="similarity",
24
+ search_kwargs={"k":10}
25
+ )
26
+
27
+ df=pd.read_csv('data/Gretel_Data.csv')
28
+ averages = df.mean(numeric_only=True).to_dict()
29
+
30
+ agent = Agent([df], config={"llm": llm, 'verbose':True})
31
+
32
+ global unique_columns
33
+ unique_columns = [
34
+ 'Avg_Connected_UEs',
35
+ 'PRB Util%',
36
+ 'CA Activation Rate',
37
+ 'DLRLCLayerDataVolume MB',
38
+ 'DRB UL Data Volume MB',
39
+ 'UPTP_Mbps',
40
+ 'UPTP Mbps Num',
41
+ 'UPTP Mbps Den',
42
+ 'UL MAC Vol Scell Pct',
43
+ 'DL MAC Vol Scell Pct',
44
+ 'DL MAC Vol Scell MB',
45
+ 'DL Volume',
46
+ 'DL Data Vol MAC in MB',
47
+ 'UL Throughput',
48
+ 'MB_per_connected_UE'
49
+ ]
50
+
51
+ global target_words
52
+ target_words = ["Bandwidth", "Interference", "Call Quality", "Network", "Handover"]
53
+
54
+ columns = []
55
+
56
+ column_avgs = {}
57
+
58
+ global network_features
59
+ network_features = {
60
+ 'Bandwidth': [
61
+ 'Avg_Connected_UEs',
62
+ 'PRB Util%',
63
+ 'CA Activation Rate',
64
+ 'DLRLCLayerDataVolume MB',
65
+ 'DRB UL Data Volume MB',
66
+ 'UPTP_Mbps',
67
+ 'UPTP Mbps Num',
68
+ 'UPTP Mbps Den',
69
+ 'UL MAC Vol Scell Pct',
70
+ 'DL MAC Vol Scell Pct',
71
+ 'DL MAC Vol Scell MB',
72
+ 'DL Volume',
73
+ 'DL Data Vol MAC in MB',
74
+ 'UL Throughput',
75
+ 'MB_per_connected_UE'
76
+ ],
77
+ 'Handover': [
78
+ 'Avg_Connected_UEs',
79
+ 'PRB Util%',
80
+ 'CA Activation Rate',
81
+ 'HO Failures',
82
+ 'HO_fail_InterFreq',
83
+ 'HO_fail_PCT_InterFreq',
84
+ 'HO Failure%',
85
+ 'HO Attempts',
86
+ 'HO_att_InterFreq'
87
+ ],
88
+ 'Network': [
89
+ 'Avg_Connected_UEs',
90
+ 'PRB Util%',
91
+ 'CA Activation Rate',
92
+ 'SIP DC%',
93
+ 'RRC Setup Attempts',
94
+ 'RRC Setup Failures',
95
+ 'RRC Setup Failure% 5G',
96
+ 'Combined RACH Failure%',
97
+ 'Combined RACH Preambles',
98
+ 'Combined RACH Failures',
99
+ 'Interference Pwr',
100
+ ],
101
+ 'Call Quality': [
102
+ 'Avg_Connected_UEs',
103
+ 'PRB Util%',
104
+ 'CA Activation Rate',
105
+ 'Avg_PUCCH_SINR',
106
+ 'Avg CQI',
107
+ 'SIP Calls with a Leg',
108
+ 'SIP_SC_Total_MOU',
109
+ 'SIP Dropped Calls',
110
+ 'VoLTE_MOU',
111
+ 'QCI 1 Bearer Drops',
112
+ 'QCI 1 Bearer Releases',
113
+ 'QCI 1 Bearer Drop%',
114
+ 'Peak UE',
115
+ 'DL Packet Loss Pct',
116
+ 'UL Resid BLER PCT',
117
+ 'Bearer Drops Voice',
118
+ 'Bearer Releases Voice',
119
+ 'Bearer Drop%',
120
+ 'Call_Drops_Credit'
121
+ ],
122
+ 'Interference': [
123
+ 'Avg_Connected_UEs',
124
+ 'PRB Util%',
125
+ 'CA Activation Rate',
126
+ 'Combined RACH Failure%',
127
+ 'Interference Pwr'
128
+ ]
129
+ }
130
+
131
+ def echo(message, history):
132
+ try:
133
+ qa=RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
134
+ message= " <s> [INST] You are a senior telecom network engineer having access to troubleshooting tickets data and other technical and product documentation. Stick to the knowledge provided. Search through the product documentation pdfs first before scanning the tickets to generate the answer. Return only the helpful answer. Question:" + message + '[/INST]'
135
+ result= qa({"query":message})
136
+ answer= result['result']
137
+ for word in target_words:
138
+ if re.search(r'\b' + re.escape(word) + r'\b', answer, flags=re.IGNORECASE):
139
+ columns.extend(network_features.get(word, []))
140
+ unique_columns = list(set(columns))
141
+
142
+ for column in unique_columns:
143
+ column_avgs.update({column:averages.get(column, [])})
144
+
145
+ result_df = df[unique_columns].iloc[:25]
146
+
147
+ def highlight_rows(val, threshold):
148
+ if val > threshold:
149
+ return 'color: red; font-weight: bold'
150
+ elif val < threshold:
151
+ return 'color: green'
152
+ else:
153
+ return ''
154
+
155
+ styled_df = result_df.style
156
+
157
+ for key in column_avgs:
158
+ styled_df = styled_df.applymap(lambda x, k=key: highlight_rows(x, column_avgs[k]), subset=[f'{key}'])
159
+
160
+ gr.Dataframe(styled_df)
161
+ return (
162
+ "Answer: \n"
163
+ + '\n' + answer.strip() + '\n'
164
+ + '\n' + "Sources: \n"
165
+ + '\n' + '1. ' + result['source_documents'][0].metadata['source'] + '\n' + result['source_documents'][0].page_content + "\n"
166
+ + '\n' + '2. ' + result['source_documents'][1].metadata['source'] + '\n' + result['source_documents'][1].page_content + "\n"
167
+ + '\n' + "3. " + result['source_documents'][2].metadata['source'] + '\n' + result['source_documents'][2].page_content + "\n"
168
+ + '\n' + "4. " + result['source_documents'][3].metadata['source'] + '\n' + result['source_documents'][3].page_content + "\n"
169
+ + '\n' + "5. " + result['source_documents'][4].metadata['source'] + '\n' + result['source_documents'][4].page_content + "\n",
170
+ styled_df
171
+ )
172
+ except Exception as e:
173
+ error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
174
+ return error_message, error_message
175
+
176
+ def echo_agent(message, history):
177
+ try:
178
+ response = agent.chat(message, output_type= 'text')
179
+ # explanation = agent.explain()
180
+
181
+ # result = "Answer: \n" + '\n' + response.str() + '\n' + '\n' + "Explanation: \n" + '\n' + explanation
182
+
183
+ return response
184
+ except Exception as e:
185
+ error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
186
+ return error_message
187
+
188
+ demo_agent = gr.Blocks(
189
+ title="Network Ticket Knowledge Management",
190
+ theme=gr.themes.Soft(),
191
+ )
192
+
193
+ with demo_agent:
194
+
195
+ gr.Markdown(
196
+ '''
197
+ # <p style="text-align: center;">Network Ticket Knowledge Management</p>
198
+ Welcome to VeriTel's Network Operations Center. I am here to help the Field Operations team with technical queries & escalation.
199
+ '''
200
+ )
201
+
202
+ with gr.Tab('Clara'):
203
+ with gr.Row():
204
+ message = gr.Text(label="Input Query")
205
+
206
+ btn = gr.Button("Submit")
207
+
208
+ with gr.Row():
209
+ reply = gr.Text(label="RCA and MoP", autoscroll=False)
210
+
211
+ with gr.Accordion(label = "Metrics", open=False):
212
+ table = gr.Dataframe()
213
+
214
+ btn.click(echo, inputs=[message], outputs=[reply, table])
215
+
216
+ gr.Examples([
217
+ "Wi-Fi connected but no internet showing",
218
+ 'What are the possible cause of router overheating ?',
219
+ "What are the possible causes of RAN getting disconnected frequently?",
220
+ "For the past week, are there any specific cell towers in Texas experiencing unusually high call failure rates or data latency?",
221
+ "What are the network problems faced by people living in the state of California?",
222
+ "I have an FWA connection and all devices except my iPhone have internet access via this FWA device. Can you suggest steps for resolution?",
223
+ "We're receiving reports of congested cell towers in Cleveland. Can you identify the specific cell towers experiencing overload and suggest any temporary network adjustments to alleviate the congestion?"
224
+ ],
225
+ inputs=[message]
226
+ )
227
+
228
+ with gr.Tab('Sam'):
229
+ with gr.Row():
230
+ message_agent = gr.Text(label="Input Query")
231
+ with gr.Row():
232
+ reply_agent = gr.Text(label="Answer")
233
+
234
+ btn2 = gr.Button("Submit")
235
+ btn2.click(echo_agent, inputs=[message_agent], outputs=[reply_agent])
236
+
237
+
238
+ demo_agent.launch(share=True,debug=True,auth=("admin", "Sam&Clara"))