File size: 5,037 Bytes
3ff62e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from pdf_reader import *

# Creating Session State Variable
if 'API_Key' not in st.session_state:
    st.session_state['API_Key'] = ''
if 'Pinecone_API_Key' not in st.session_state:
    st.session_state['Pinecone_API_Key'] =''
if 'summary' not in st.session_state:
    st.session_state.summary = ''
if 'history' not in st.session_state:
    st.session_state.history = {}
if 'chat' not in st.session_state:
    st.session_state.chat = ''
if 'counter' not in st.session_state:
    st.session_state.counter = 1

st.title('PDF Chat Bot') 

#********SIDE BAR Funtionality started*******

# Sidebar to capture the API keys
st.session_state['API_Key'] = st.sidebar.text_input("What's your OPENAI API key?",type="password")
# File uploader widget
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"])

load_button = st.sidebar.button("UPLOAD", key="load_button")

#If the bove button is clicked, pushing the data to Pinecone...
if load_button:
    #Proceed only if API keys are provided
    if st.session_state['API_Key'] != '' and uploaded_file is not None:
        file = save_pdf(uploaded_file)
        file = "uploaded.pdf"
        st.session_state.summary = load_db_sum(file, st.session_state['API_Key'])
        st.session_state.chat = load_db(file, st.session_state['API_Key'])
        st.session_state.history = {}

    elif st.session_state['API_Key'] == '':
        st.sidebar.error("Please enter your OpenAI API key.")
    elif uploaded_file is None:
        st.sidebar.error("Please attach a PDF file.")

#********SIDE BAR Funtionality ended*****
        

if st.session_state['API_Key'] != '' and uploaded_file is not None:
    file = "uploaded.pdf"
    st.markdown('<br>', unsafe_allow_html=True)
    st.markdown("#### **Summary**")
    st.markdown('<hr style="margin: -10px 0; border-top: 1px solid black;">', unsafe_allow_html=True)
    st.write(st.session_state.summary)

    # create a variable for the chat
    conversation = {}

    #Captures User Inputs
    user_input = st.text_input('Ask about the PDF',key="prompt")  # The box for the text prompt
    # document_count = st.slider('No.Of links to return 🔗 - (0 LOW || 5 HIGH)', 0, 5, 2,step=1)

    submit = st.button("SUBMIT") 

    if submit:
        #Proceed only if API keys are provided
        if st.session_state.summary == '':
            st.error("Please upload the PDF file.")
        
        # user_input = request.form['user_input']
        else:
            result = st.session_state.chat({"question": user_input})
            answer_text = str(result['answer'])
            question_text = str(result['question'])
            user = "User"
            chatbot = "Chat Bot"
            conversation.update({user: question_text, chatbot: answer_text})

            user_hist = f"[{st.session_state.counter}] {user}"
            chat_hist = f"[{st.session_state.counter}] {chatbot}"
            st.session_state.history.update({user_hist : question_text})
            st.session_state.history.update({chat_hist : answer_text})
            st.session_state.counter += 1


            st.markdown('<br>', unsafe_allow_html=True)
            st.markdown("#### **Conversation**")
            st.markdown('<hr style="margin: -10px 0; border-top: 1px solid black;">', unsafe_allow_html=True)

            table_data = list(conversation.items())

            # Display the table with keys bolded using HTML
            html_table = """
                <style>
                    table, tr {border:hidden;}
                    table, td {border:hidden;}
                </style>
                <table><tr><th><strong></strong></th><th></th></tr>
                """
            for key, value in table_data:
                html_table += f"<tr><td style='width: 90px;'><strong>{key}:</strong></td><td>{value}</td></tr>"
            html_table += "</table>"
            st.markdown(html_table, unsafe_allow_html=True)


            st.markdown('<br>', unsafe_allow_html=True)
            st.markdown("#### **Chat History**")
            st.markdown('<hr style="margin: -10px 0; border-top: 1px solid black;">', unsafe_allow_html=True)
            
            table_data2 = list(st.session_state.history.items())

            # Display the table with keys bolded using HTML
            html_table = """
                <style>
                    table, tr {border:hidden;}
                    table, td {border:hidden;}
                </style>
                <table><tr><th><strong></strong></th><th></th></tr>
                """
            for key, value in table_data2:
                key = key[4:]
                html_table += f"<tr><td style='width: 90px;'><strong>{key}:</strong></td><td>{value}</td></tr>"
            html_table += "</table>"
            st.markdown(html_table, unsafe_allow_html=True)

elif st.session_state['API_Key'] == '':
    st.error("Please enter your OpenAI API key.")
elif uploaded_file is None:
    st.session_state.summary = ''
    st.error("Please upload the PDF file.")