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