Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,21 @@
|
|
1 |
from langchain.vectorstores import FAISS
|
2 |
-
from langchain.chains import
|
3 |
from langchain.llms import HuggingFaceHub
|
|
|
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
-
from
|
|
|
8 |
from langchain.document_loaders import PyPDFDirectoryLoader
|
9 |
from langchain.document_loaders.csv_loader import CSVLoader
|
10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
11 |
import io
|
12 |
import contextlib
|
|
|
|
|
13 |
|
14 |
|
15 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
@@ -22,15 +28,128 @@ retriever = vector_store.as_retriever(
|
|
22 |
search_type="similarity",
|
23 |
search_kwargs={"k":3, "include_metadata": True})
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
|
|
28 |
try:
|
29 |
-
qa=RetrievalQA.from_chain_type(llm=llm, retriever=retriever,return_source_documents=True)
|
30 |
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
|
31 |
result=qa({"query":message})
|
32 |
bold_answer= "<b>" + result['result'] + "</b>"
|
33 |
-
return bold_answer + "<br></br>" +'1. ' + str(result["source_documents"][0]) +"<br>" + '2. ' + str(result["source_documents"][1]) + "<br>" + "3. " + str(result["source_documents"][2])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
except Exception as e:
|
35 |
error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
|
36 |
|
@@ -39,7 +158,7 @@ def echo_agent(message, history):
|
|
39 |
try:
|
40 |
with io.StringIO() as buffer:
|
41 |
with contextlib.redirect_stdout(buffer):
|
42 |
-
result= agent.
|
43 |
verbose_output = buffer.getvalue()
|
44 |
verbose_output = verbose_output.replace("\x1b[36;1m\x1b[1;3m", "")
|
45 |
verbose_output = verbose_output.replace("[1m> ", "")
|
@@ -51,34 +170,46 @@ def echo_agent(message, history):
|
|
51 |
error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
|
52 |
return error_message
|
53 |
|
54 |
-
|
55 |
-
fn=echo,
|
56 |
-
chatbot=gr.Chatbot(height=300, label="Hi I am Clara!", show_label=True),
|
57 |
-
textbox=gr.Textbox(placeholder="Ask me a question", container=True, autofocus=True, scale=7),
|
58 |
title="Network Ticket Knowledge Management",
|
59 |
-
description="<span style='font-size: 16x;'>Welcome to Verizon Network Operations Center!! I am here to help the Verizon Field Operations team with technical queries & escalation. I am trained on 1000s of RAN, Backhaul, Core network & End user equipment trouble tickets. Ask me!!! ☺</span>",
|
60 |
-
theme=gr.themes.Soft(),
|
61 |
-
examples=["wifi connected but no internet showing", "internet stopped working after primary link down", "internet stopped working link not shifted to secondary after primary link down"],
|
62 |
-
cache_examples=False,
|
63 |
-
retry_btn=None,
|
64 |
-
undo_btn="Delete Previous",
|
65 |
-
clear_btn="Clear",
|
66 |
-
stop_btn="Stop",
|
67 |
-
)
|
68 |
-
|
69 |
-
|
70 |
-
demo1=gr.ChatInterface(
|
71 |
-
fn=echo_agent,
|
72 |
-
chatbot=gr.Chatbot(height=300, label="Hi I am Sam!", show_label=True),
|
73 |
-
textbox=gr.Textbox(placeholder="Ask me a question", container=True, autofocus=True, scale=7),
|
74 |
-
title="LLM Powered Agent",
|
75 |
-
description="<span style='font-size: 16x;'>Welcome to Verizon RAN Visualization & Analytics powered by GEN AI. I have access 100 of metrices generated by a RAN base station and can help in visualizing, correlating and generating insights, using power of Conversational AI ☺</span>",
|
76 |
theme=gr.themes.Soft(),
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
|
|
|
|
|
1 |
from langchain.vectorstores import FAISS
|
2 |
+
from langchain.chains import ConversationalRetrievalChain
|
3 |
from langchain.llms import HuggingFaceHub
|
4 |
+
from langchain.chains import RetrievalQA
|
5 |
+
from getpass import getpass
|
6 |
import gradio as gr
|
7 |
import os
|
8 |
from langchain.embeddings import HuggingFaceEmbeddings
|
9 |
+
from pandasai import Agent
|
10 |
+
from google.colab import drive
|
11 |
from langchain.document_loaders import PyPDFDirectoryLoader
|
12 |
from langchain.document_loaders.csv_loader import CSVLoader
|
13 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
14 |
+
from langchain.memory import ConversationSummaryBufferMemory
|
15 |
import io
|
16 |
import contextlib
|
17 |
+
import pandas as pd
|
18 |
+
import re
|
19 |
|
20 |
|
21 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
|
|
28 |
search_type="similarity",
|
29 |
search_kwargs={"k":3, "include_metadata": True})
|
30 |
|
31 |
+
df=pd.read_csv('data/Gretel_Data.csv')
|
32 |
+
averages = df.mean().to_dict()
|
33 |
+
agent = Agent([df], config={"llm": llm, 'raw_response':True, 'verbose':True})
|
34 |
+
|
35 |
+
global unique_columns
|
36 |
+
unique_columns = []
|
37 |
+
|
38 |
+
global target_words
|
39 |
+
target_words = ["Bandwidth", "Interference", "Call Quality", "Network", "Handover"]
|
40 |
+
|
41 |
+
global result_dict
|
42 |
+
result_dict = {}
|
43 |
+
|
44 |
+
global network_features
|
45 |
+
network_features = {
|
46 |
+
'Bandwidth': [
|
47 |
+
'Avg_Connected_UEs',
|
48 |
+
'PRB Util%',
|
49 |
+
'CA Activation Rate',
|
50 |
+
'DLRLCLayerDataVolume MB',
|
51 |
+
'DRB UL Data Volume MB',
|
52 |
+
'UPTP_Mbps',
|
53 |
+
'UPTP Mbps Num',
|
54 |
+
'UPTP Mbps Den',
|
55 |
+
'UL MAC Vol Scell Pct',
|
56 |
+
'DL MAC Vol Scell Pct',
|
57 |
+
'DL MAC Vol Scell MB',
|
58 |
+
'DL Volume',
|
59 |
+
'DL Data Vol MAC in MB',
|
60 |
+
'UL Throughput',
|
61 |
+
'MB_per_connected_UE'
|
62 |
+
],
|
63 |
+
'Handover': [
|
64 |
+
'Avg_Connected_UEs',
|
65 |
+
'PRB Util%',
|
66 |
+
'CA Activation Rate',
|
67 |
+
'HO Failures',
|
68 |
+
'HO_fail_InterFreq',
|
69 |
+
'HO_fail_PCT_InterFreq',
|
70 |
+
'HO Failure%',
|
71 |
+
'HO Attempts',
|
72 |
+
'HO_att_InterFreq'
|
73 |
+
],
|
74 |
+
'Network': [
|
75 |
+
'Avg_Connected_UEs',
|
76 |
+
'PRB Util%',
|
77 |
+
'CA Activation Rate',
|
78 |
+
'SIP DC%',
|
79 |
+
'RRC Setup Attempts',
|
80 |
+
'RRC Setup Failures',
|
81 |
+
'RRC Setup Failure% 5G',
|
82 |
+
'Combined RACH Failure%',
|
83 |
+
'Combined RACH Preambles',
|
84 |
+
'Combined RACH Failures',
|
85 |
+
'Interference Pwr',
|
86 |
+
],
|
87 |
+
'Call Quality': [
|
88 |
+
'Avg_Connected_UEs',
|
89 |
+
'PRB Util%',
|
90 |
+
'CA Activation Rate',
|
91 |
+
'Avg_PUCCH_SINR',
|
92 |
+
'Avg CQI',
|
93 |
+
'SIP Calls with a Leg',
|
94 |
+
'SIP_SC_Total_MOU',
|
95 |
+
'SIP Dropped Calls',
|
96 |
+
'Volte_MOU',
|
97 |
+
'QCI 1 Bearer Drops',
|
98 |
+
'QCI 1 Bearer Releases',
|
99 |
+
'QCI 1 Bearer Drop%',
|
100 |
+
'Peak UE',
|
101 |
+
'DL Packet Loss Pct',
|
102 |
+
'UL Resid BLER PCT',
|
103 |
+
'Bearer Drops Voice',
|
104 |
+
'Bearer Releases Voice',
|
105 |
+
'Bearer Drop%',
|
106 |
+
'Call_Drops_Credit'
|
107 |
+
],
|
108 |
+
'Interference': [
|
109 |
+
'Avg_Connected_UEs',
|
110 |
+
'PRB Util%',
|
111 |
+
'CA Activation Rate',
|
112 |
+
'Combined RACH Failure%',
|
113 |
+
'Interference Pwr'
|
114 |
+
]
|
115 |
+
}
|
116 |
|
117 |
+
|
118 |
+
def echo(message):
|
119 |
try:
|
120 |
+
qa=RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
|
121 |
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
|
122 |
result=qa({"query":message})
|
123 |
bold_answer= "<b>" + result['result'] + "</b>"
|
124 |
+
# return bold_answer + "<br></br>" +'1. ' + str(result["source_documents"][0]) +"<br>" + '2. ' + str(result["source_documents"][1]) + "<br>" + "3. " + str(result["source_documents"][2])
|
125 |
+
for word in target_words:
|
126 |
+
if re.search(r'\b' + re.escape(word) + r'\b', bold_answer, flags=re.IGNORECASE):
|
127 |
+
columns.extend(network_features.get(word, []))
|
128 |
+
unique_columns = list(set(columns))
|
129 |
+
|
130 |
+
for column in unique_columns:
|
131 |
+
column_avgs.update({column:averages.get(column, [])})
|
132 |
+
|
133 |
+
result_df = df[unique_columns].iloc[:25]
|
134 |
+
|
135 |
+
def highlight_rows(val, threshold):
|
136 |
+
if val > threshold:
|
137 |
+
return 'color: red; font-weight: bold'
|
138 |
+
elif val < threshold:
|
139 |
+
return 'color: green'
|
140 |
+
else:
|
141 |
+
return ''
|
142 |
+
|
143 |
+
# Initialize styled_df outside the loop
|
144 |
+
styled_df = result_df.style
|
145 |
+
|
146 |
+
# Iterate through keys in result_dict
|
147 |
+
for key in column_avgs:
|
148 |
+
# Apply styling for the current key
|
149 |
+
styled_df = styled_df.applymap(lambda x, k=key: highlight_rows(x, column_avgs[k]), subset=[f'{key}'])
|
150 |
+
|
151 |
+
gr.Dataframe(styled_df)
|
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 |
|
|
|
158 |
try:
|
159 |
with io.StringIO() as buffer:
|
160 |
with contextlib.redirect_stdout(buffer):
|
161 |
+
result= agent.chat(message)
|
162 |
verbose_output = buffer.getvalue()
|
163 |
verbose_output = verbose_output.replace("\x1b[36;1m\x1b[1;3m", "")
|
164 |
verbose_output = verbose_output.replace("[1m> ", "")
|
|
|
170 |
error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
|
171 |
return error_message
|
172 |
|
173 |
+
demo_agent = gr.Blocks(
|
|
|
|
|
|
|
174 |
title="Network Ticket Knowledge Management",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
theme=gr.themes.Soft(),
|
176 |
+
)
|
177 |
+
|
178 |
+
with demo_agent:
|
179 |
+
|
180 |
+
gr.Markdown(
|
181 |
+
'''
|
182 |
+
|
183 |
+
# <p style="text-align: center;">Network Ticket Knowledge Management</p>
|
184 |
+
|
185 |
+
Welcome to Verizon Network Operations Center!! I am here to help the Verizon Field Operations team with technical queries & escalation. I am trained on 1000s of RAN, Backhaul, Core network & End user equipment trouble tickets. Ask me!!! ☺
|
186 |
+
'''
|
187 |
+
)
|
188 |
+
|
189 |
+
with gr.Tab('Clara'):
|
190 |
+
with gr.Row():
|
191 |
+
message = gr.Text(label="Input Query")
|
192 |
+
|
193 |
+
btn = gr.Button("Submit")
|
194 |
+
|
195 |
+
with gr.Row():
|
196 |
+
reply = gr.Text(label="RCA and MoP")
|
197 |
+
|
198 |
+
with gr.Accordion(label = "Metrics", open=False):
|
199 |
+
table = gr.Dataframe()
|
200 |
+
|
201 |
+
btn.click(echo, inputs=[message], outputs=[reply, table])
|
202 |
+
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])
|
203 |
+
|
204 |
+
with gr.Tab('Sam'):
|
205 |
+
with gr.Row():
|
206 |
+
message_agent = gr.Text(label="Input Query")
|
207 |
+
with gr.Row():
|
208 |
+
reply_agent = gr.Text(label="Answer")
|
209 |
+
|
210 |
+
btn2 = gr.Button("Submit")
|
211 |
+
btn2.click(echo_agent, inputs=[message_agent], outputs=[reply_agent])
|
212 |
+
|
213 |
|
214 |
+
if __name__ == "__main__":
|
215 |
+
demo_agent.launch(share=True,debug=True,auth=("admin", "Sam&Clara"))
|