Spaces:
Runtime error
Runtime error
Commit
·
e91ad46
1
Parent(s):
d28ed88
Upload app.py
Browse files
app.py
CHANGED
@@ -9,13 +9,24 @@ from langchain.memory import ConversationBufferMemory
|
|
9 |
from langchain.chains import ConversationalRetrievalChain
|
10 |
from langchain.llms import HuggingFaceHub
|
11 |
from htmlTemplates import css, bot_template, user_template
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def
|
14 |
text = ""
|
15 |
-
for
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
return text
|
20 |
|
21 |
def get_text_chunks(text):
|
@@ -35,7 +46,7 @@ def get_vectorstore(text_chunks):
|
|
35 |
return vectorstore
|
36 |
|
37 |
def get_conversation_chain(vectorstore):
|
38 |
-
llm = ChatOpenAI()
|
39 |
# llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
|
40 |
|
41 |
memory = ConversationBufferMemory(
|
@@ -50,47 +61,107 @@ def get_conversation_chain(vectorstore):
|
|
50 |
def handle_userinput(user_question, myslot):
|
51 |
response = st.session_state.conversation({'question': user_question})
|
52 |
st.session_state.chat_history = response['chat_history']
|
|
|
|
|
|
|
|
|
|
|
53 |
with myslot.container():
|
54 |
-
for i,
|
55 |
if i % 2 == 0:
|
56 |
-
|
57 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
58 |
else:
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
def main():
|
63 |
load_dotenv()
|
64 |
-
st.set_page_config(page_title="
|
65 |
-
page_icon=":books:")
|
66 |
|
67 |
if "conversation" not in st.session_state:
|
68 |
st.session_state.conversation = None
|
69 |
if "chat_history" not in st.session_state:
|
70 |
st.session_state.chat_history = None
|
|
|
|
|
71 |
|
72 |
-
st.header("
|
73 |
|
74 |
myslot = st.empty()
|
75 |
-
|
|
|
76 |
if user_question:
|
77 |
handle_userinput(user_question, myslot)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
with st.sidebar:
|
80 |
st.subheader("Your support Documents")
|
81 |
pdf_docs = st.file_uploader(
|
82 |
"Upload your Documents here and click on 'Process'", accept_multiple_files=True)
|
83 |
if st.button("Process"):
|
84 |
with st.spinner("Uploading the docs"):
|
85 |
-
|
|
|
86 |
|
87 |
text_chunks = get_text_chunks(raw_text)
|
88 |
|
89 |
vectorstore = get_vectorstore(text_chunks)
|
90 |
|
91 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
92 |
-
|
93 |
-
|
94 |
|
95 |
|
96 |
if __name__ == '__main__':
|
|
|
9 |
from langchain.chains import ConversationalRetrievalChain
|
10 |
from langchain.llms import HuggingFaceHub
|
11 |
from htmlTemplates import css, bot_template, user_template
|
12 |
+
from streamlit_chat import message
|
13 |
+
import os
|
14 |
+
from docx import Document
|
15 |
+
import requests
|
16 |
+
from requests.auth import HTTPBasicAuth
|
17 |
|
18 |
+
def get_uploaded_text(uploadedFiles):
|
19 |
text = ""
|
20 |
+
for uploadedFile in uploadedFiles:
|
21 |
+
file_extension = os.path.splitext(uploadedFile.name)[1]
|
22 |
+
if(file_extension == '.pdf'):
|
23 |
+
pdf_reader = PdfReader(uploadedFile)
|
24 |
+
for page in pdf_reader.pages:
|
25 |
+
text += page.extract_text()
|
26 |
+
elif(file_extension == '.docx'):
|
27 |
+
doc = Document(uploadedFile)
|
28 |
+
for para in doc.paragraphs:
|
29 |
+
text += para.text
|
30 |
return text
|
31 |
|
32 |
def get_text_chunks(text):
|
|
|
46 |
return vectorstore
|
47 |
|
48 |
def get_conversation_chain(vectorstore):
|
49 |
+
llm = ChatOpenAI(temperature=0.3)
|
50 |
# llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
|
51 |
|
52 |
memory = ConversationBufferMemory(
|
|
|
61 |
def handle_userinput(user_question, myslot):
|
62 |
response = st.session_state.conversation({'question': user_question})
|
63 |
st.session_state.chat_history = response['chat_history']
|
64 |
+
indexed = response['answer'].find("don't have") != -1 or response['answer'].find("don't know") != -1
|
65 |
+
if response and response['answer'] and indexed:
|
66 |
+
st.session_state.sr = 0
|
67 |
+
else:
|
68 |
+
st.session_state.sr = 1
|
69 |
with myslot.container():
|
70 |
+
for i, msg in enumerate(st.session_state.chat_history):
|
71 |
if i % 2 == 0:
|
72 |
+
message(msg.content, is_user=True)
|
|
|
73 |
else:
|
74 |
+
message(msg.content)
|
75 |
+
|
76 |
+
def create_jira_ticket(summary, description, project_key, issuetype_name):
|
77 |
+
url = "https://tnq.atlassian.net/rest/api/3/issue"
|
78 |
+
token = "ATATT3xFfGF0EqZ--az1c1buBTXEebQfuxMIBv0jByz6tNHcXXrEV61esZS_ZIwOY15OJN7jHn23IBG1Fzw3lm7GKhtGqpetDZCaiis2BbGefP3dvS5aOfPubz1ZWlEjAbQr4s87J8hr7xMceFTbTMfXk4kk8TRN3jEes9SyMLyhnuux1ugykck=13D01E13"
|
79 |
+
auth = HTTPBasicAuth("harinandan.munagala@tnqtech.com", token)
|
80 |
+
|
81 |
+
headers = {
|
82 |
+
"Accept": "application/json",
|
83 |
+
"Content-Type": "application/json"
|
84 |
+
}
|
85 |
+
|
86 |
+
payload = {
|
87 |
+
"fields": {
|
88 |
+
"project":
|
89 |
+
{
|
90 |
+
"key": project_key
|
91 |
+
},
|
92 |
+
"summary": summary,
|
93 |
+
"customfield_10044": [{"value": "Edit Central All"}],
|
94 |
+
"description": {
|
95 |
+
"type": "doc",
|
96 |
+
"version": 1,
|
97 |
+
"content": [
|
98 |
+
{
|
99 |
+
"type": "paragraph",
|
100 |
+
"content": [
|
101 |
+
{
|
102 |
+
"type": "text",
|
103 |
+
"text": "Creating of an issue using project keys and issue type names using the REST API"
|
104 |
+
}
|
105 |
+
]
|
106 |
+
}
|
107 |
+
]
|
108 |
+
},
|
109 |
+
"issuetype": {
|
110 |
+
"name": issuetype_name
|
111 |
+
}
|
112 |
+
}
|
113 |
+
}
|
114 |
+
|
115 |
+
response = requests.post(
|
116 |
+
url, json=payload, headers=headers, auth=auth
|
117 |
+
)
|
118 |
+
|
119 |
+
return response.json()
|
120 |
+
|
121 |
|
122 |
def main():
|
123 |
load_dotenv()
|
124 |
+
st.set_page_config(page_title="AIusBOT", page_icon=":alien:")
|
|
|
125 |
|
126 |
if "conversation" not in st.session_state:
|
127 |
st.session_state.conversation = None
|
128 |
if "chat_history" not in st.session_state:
|
129 |
st.session_state.chat_history = None
|
130 |
+
if "sr" not in st.session_state:
|
131 |
+
st.session_state.sr = 1
|
132 |
|
133 |
+
st.header("AIusBOT :alien:")
|
134 |
|
135 |
myslot = st.empty()
|
136 |
+
|
137 |
+
user_question = st.text_input("Ask a question?")
|
138 |
if user_question:
|
139 |
handle_userinput(user_question, myslot)
|
140 |
|
141 |
+
if st.button("Create SR?", disabled=st.session_state.sr, type="primary"):
|
142 |
+
jira_response = create_jira_ticket(
|
143 |
+
summary=user_question,
|
144 |
+
description=f"User question that did not receive a satisfactory answer: {user_question}",
|
145 |
+
project_key="EC",
|
146 |
+
issuetype_name="Task"
|
147 |
+
)
|
148 |
+
st.write(f"Ticket created: {jira_response.get('key')}")
|
149 |
+
|
150 |
with st.sidebar:
|
151 |
st.subheader("Your support Documents")
|
152 |
pdf_docs = st.file_uploader(
|
153 |
"Upload your Documents here and click on 'Process'", accept_multiple_files=True)
|
154 |
if st.button("Process"):
|
155 |
with st.spinner("Uploading the docs"):
|
156 |
+
|
157 |
+
raw_text = get_uploaded_text(pdf_docs)
|
158 |
|
159 |
text_chunks = get_text_chunks(raw_text)
|
160 |
|
161 |
vectorstore = get_vectorstore(text_chunks)
|
162 |
|
163 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
164 |
+
st.toast("File Process Completed", icon='🎉')
|
|
|
165 |
|
166 |
|
167 |
if __name__ == '__main__':
|