-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
87 lines (69 loc) · 3.08 KB
/
app.py
File metadata and controls
87 lines (69 loc) · 3.08 KB
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
import streamlit as st
from ingestion import PDFIngestor
from app_graph import PdfChat
st.set_page_config(page_title="Chat with your PDF", page_icon="🤖")
st.image("./media/cover.jpg", use_column_width=True)
button_html = f'''
<div style="display: flex; justify-content: center;">
<a href="https://buymeacoffee.com/mesutduman" target="_blank">
<button style="
background-color: #FFDD00;
border: none;
color: black;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 10px;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
">
☕ Buy Me a Coffee
</button>
</a>
</div>
'''
if "messages" not in st.session_state:
st.session_state.messages = [{"role": "assistant", "content": "Time to talk about PDFs!"}]
st.session_state.app = None
with st.sidebar:
st.info("🤖 This app uses the OpenAI API to generate text, please provide your API key."
"\n If you don't have an API key, you can get one [here](https://platform.openai.com/signup)."
"\n You can also find the source code for this app [here](https://github.com/mesutdmn/Chat-With-Your-PDF)"
"\n App keys are not stored or saved in any way.")
openai_key_input = st.text_input("OpenAI API Key", type="password")
developer_mode = st.checkbox("I don't have key, use developer's money 😓", value=False)
openai_key = openai_key_input or (st.secrets["OpenAI_API_KEY"] if developer_mode else "")
if len(openai_key) < 1:
st.error("Please enter your OpenAI API key")
chat_active = True
else:
chat_active = False
st.divider()
st.markdown(button_html, unsafe_allow_html=True)
st.divider()
pdf_files = st.file_uploader("Upload PDFs", type=["pdf"], accept_multiple_files=True)
def initialize_ingestor(pdf_files):
retriever = PDFIngestor(pdfs=pdf_files, api_key=openai_key).get_retriever()
st.success("PDFs successfully uploaded")
app = PdfChat(openai_key, retriever).graph
st.success("ChatBot successfully initialized")
return app
with st.sidebar:
if st.button("Initialize ChatBot", type="primary"):
st.session_state.app = initialize_ingestor(pdf_files)
app = st.session_state.app
def generate_response(question):
return app.invoke(input={"question": question})["response"]
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if question:= st.chat_input(placeholder="Ask a question", disabled=chat_active):
st.chat_message("user").markdown(question)
st.session_state.messages.append({"role": "user", "content": question})
response = generate_response(question)
with st.chat_message("assistant"):
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})