ShivanshMathur007 commited on
Commit
f1ee750
1 Parent(s): 1dabeab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.vectorstores import FAISS
2
+ from langchain.chains import RetrievalQA
3
+ from langchain.llms import HuggingFaceHub
4
+ import gradio as gr
5
+ import os
6
+ from langchain.embeddings import HuggingFaceEmbeddings
7
+ from langchain_experimental.agents.agent_toolkits.csv.base import create_csv_agent
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")
16
+ vector_store= FAISS.load_local("vector_db/", embeddings)
17
+
18
+ repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1"
19
+ llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature": 0.01, "max_new_tokens": 2048})
20
+
21
+ retriever = vector_store.as_retriever(
22
+ search_type="similarity",
23
+ search_kwargs={"k":3, "include_metadata": True})
24
+
25
+ agent=create_csv_agent(llm,['data/Gretel_Data.csv','data/RAN_Data _T.csv'],verbose=True)
26
+
27
+ def echo(message, history):
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
+
37
+ def echo_agent(message, history):
38
+ message="There are 2 df's. If you find a KeyError check for the same in the other df." + "<br>" + message
39
+ try:
40
+ with io.StringIO() as buffer:
41
+ with contextlib.redirect_stdout(buffer):
42
+ result= agent.run(message)
43
+ verbose_output = buffer.getvalue()
44
+ verbose_output = verbose_output.replace("\x1b[36;1m\x1b[1;3m", "")
45
+ verbose_output = verbose_output.replace("> ", "")
46
+ verbose_output = verbose_output.replace("", "")
47
+ verbose_output = verbose_output.replace("", "")
48
+ result= "<b>" + verbose_output + "<br>" + result + "</b>"
49
+ return result
50
+ except Exception as e:
51
+ error_message = f"An error occurred: {e}"+str(e.with_traceback) + str(e.args)
52
+ return error_message
53
+
54
+ demo=gr.ChatInterface(
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!!!&nbsp;☺</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 &nbsp;☺</span>",
76
+ theme=gr.themes.Soft(),
77
+ retry_btn=None,
78
+ undo_btn="Delete Previous",
79
+ clear_btn="Clear",
80
+ stop_btn="Stop",
81
+ )
82
+ demo2=gr.TabbedInterface([demo,demo1],["RAG","AGENT"], title='INCEDO', theme=gr.themes.Soft())
83
+ demo2.launch(share=True,debug=True,auth=("admin", "Sam&Clara"))