Spaces:
Sleeping
Sleeping
import streamlit as st | |
from langchain import OpenAI, PromptTemplate, LLMChain | |
from langchain.text_splitter import CharacterTextSplitter | |
from langchain.chains.mapreduce import MapReduceChain | |
from langchain.prompts import PromptTemplate | |
from langchain.chat_models import AzureChatOpenAI | |
from langchain.chains.summarize import load_summarize_chain | |
from langchain.chains import AnalyzeDocumentChain | |
from PyPDF2 import PdfReader | |
from langchain.document_loaders import TextLoader | |
from langchain.indexes import VectorstoreIndexCreator | |
from langchain.document_loaders import PyPDFLoader | |
import os | |
os.environ["OPENAI_API_TYPE"] = "azure" | |
os.environ["OPENAI_API_VERSION"] = "2023-05-15" | |
os.environ["OPENAI_API_BASE"] = "https://cog-mnjbf5r4o6b3e.openai.azure.com/" | |
os.environ["OPENAI_API_KEY"] = "957f7d98b47a467a98a786f7ca903112" | |
llm = AzureChatOpenAI( | |
deployment_name="chat", | |
openai_api_version="2023-03-15-preview") | |
st.title("Contract Management System") | |
# description text | |
st.write("""Hello! I'm an Al chatbot specifically created to assist vou with any procurement-related questions you may have. My range of capabilities includes providing information on contract expiry dates payment terms, and other relevant details. | |
Feel free to ask me anything you need help with! | |
To get started, just provide me with the contract number and let me know what specitic details you're interested in. Additionally, tee tree to ask me any general questions you may have regardinc procurement. I'm here to assist vou in any way can π""") | |
# pdf file upload | |
pdf_file = st.file_uploader("Upload file", type=["pdf"]) | |
def extract_text_from_pdf(): | |
reader = PdfReader(pdf_file) | |
# get all pages text | |
text = [reader.pages[i].extract_text() for i in range(len(reader.pages))] | |
# join all pages text | |
text = " ".join(text) | |
return text | |
if st.button("Summerize Contract"): | |
with st.spinner("Extracting Text..."): | |
summary_chain = load_summarize_chain(llm, chain_type="map_reduce") | |
summarize_document_chain = AnalyzeDocumentChain(combine_docs_chain=summary_chain, verbose=True) | |
text = extract_text_from_pdf() | |
with st.spinner("Summarizing..."): | |
result = summarize_document_chain.run(text) | |
st.write(result) | |