Spaces:
Sleeping
Sleeping
Baskar2005
commited on
Commit
•
0e21b1c
1
Parent(s):
77117d5
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import tempfile
|
3 |
from dspy_qa import DocQA
|
@@ -13,20 +14,31 @@ st.markdown("""
|
|
13 |
st.markdown("""
|
14 |
<style>
|
15 |
.st-emotion-cache-12fmjuu {
|
16 |
-
|
17 |
-
|
18 |
text-align: center;
|
19 |
}
|
20 |
|
|
|
21 |
margin: 0;
|
22 |
font-size: 24px;
|
23 |
justify-content: center;
|
|
|
24 |
|
|
|
|
|
25 |
}
|
26 |
|
27 |
-
.
|
28 |
-
|
|
|
|
|
|
|
29 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
</style>
|
31 |
""", unsafe_allow_html=True)
|
32 |
|
@@ -37,16 +49,16 @@ if "knowledge_base" not in st.session_state:
|
|
37 |
if st.session_state.knowledge_base is None:
|
38 |
pdf_file = st.file_uploader("Upload a PDF file", "pdf")
|
39 |
if pdf_file:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
st.session_state.knowledge_base = docqa
|
48 |
|
49 |
docqa = st.session_state.knowledge_base
|
|
|
50 |
# Initialize chat history
|
51 |
if "messages" not in st.session_state:
|
52 |
st.session_state.messages = []
|
@@ -56,22 +68,29 @@ for message in st.session_state.messages:
|
|
56 |
with st.chat_message(message["role"]):
|
57 |
st.markdown(message["content"])
|
58 |
|
|
|
|
|
|
|
59 |
# Accept user input
|
60 |
if question := st.chat_input("What is up?"):
|
61 |
# Add user message to chat history
|
62 |
st.session_state.messages.append({"role": "user", "content": question})
|
63 |
# Display user message in chat message container
|
64 |
with st.chat_message("user"):
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
# Display assistant response in chat message container
|
68 |
with st.chat_message("assistant"):
|
69 |
response = docqa.run(question)
|
70 |
st.markdown(response)
|
71 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
72 |
|
73 |
-
|
74 |
if st.button("Reset"):
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
1 |
+
%%writefile app.py
|
2 |
import streamlit as st
|
3 |
import tempfile
|
4 |
from dspy_qa import DocQA
|
|
|
14 |
st.markdown("""
|
15 |
<style>
|
16 |
.st-emotion-cache-12fmjuu {
|
|
|
|
|
17 |
text-align: center;
|
18 |
}
|
19 |
|
20 |
+
.margin-0 {
|
21 |
margin: 0;
|
22 |
font-size: 24px;
|
23 |
justify-content: center;
|
24 |
+
}
|
25 |
|
26 |
+
.st-af.st-b6.st-b7.st-ar.st-as.st-b8.st-b9.st-ba.st-bb.st-bc.st-bd.st-be.st-b1{
|
27 |
+
color: black;
|
28 |
}
|
29 |
|
30 |
+
.reset-container {
|
31 |
+
display: flex;
|
32 |
+
justify-content: space-between;
|
33 |
+
align-items: center;
|
34 |
+
margin-bottom: 10px;
|
35 |
}
|
36 |
+
|
37 |
+
.stButton>button {
|
38 |
+
margin-left: auto;
|
39 |
+
order: 1;
|
40 |
+
}
|
41 |
+
|
42 |
</style>
|
43 |
""", unsafe_allow_html=True)
|
44 |
|
|
|
49 |
if st.session_state.knowledge_base is None:
|
50 |
pdf_file = st.file_uploader("Upload a PDF file", "pdf")
|
51 |
if pdf_file:
|
52 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
53 |
+
temp_file.write(pdf_file.getbuffer())
|
54 |
+
temp_file_path = temp_file.name
|
55 |
+
docqa = DocQA(temp_file_path)
|
56 |
+
if docqa:
|
57 |
+
st.success("PDF file uploaded successfully!")
|
58 |
+
st.session_state.knowledge_base = docqa
|
|
|
59 |
|
60 |
docqa = st.session_state.knowledge_base
|
61 |
+
|
62 |
# Initialize chat history
|
63 |
if "messages" not in st.session_state:
|
64 |
st.session_state.messages = []
|
|
|
68 |
with st.chat_message(message["role"]):
|
69 |
st.markdown(message["content"])
|
70 |
|
71 |
+
# Container for input and reset button
|
72 |
+
|
73 |
+
|
74 |
# Accept user input
|
75 |
if question := st.chat_input("What is up?"):
|
76 |
# Add user message to chat history
|
77 |
st.session_state.messages.append({"role": "user", "content": question})
|
78 |
# Display user message in chat message container
|
79 |
with st.chat_message("user"):
|
80 |
+
st.markdown(question)
|
81 |
+
|
82 |
+
if docqa is None:
|
83 |
+
with st.chat_message("assistant"):
|
84 |
+
st.markdown("Please upload a PDF file first.")
|
85 |
+
st.session_state.messages.append({"role": "assistant", "content": "Please upload a PDF file first."})
|
86 |
+
st.stop()
|
87 |
# Display assistant response in chat message container
|
88 |
with st.chat_message("assistant"):
|
89 |
response = docqa.run(question)
|
90 |
st.markdown(response)
|
91 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
92 |
|
|
|
93 |
if st.button("Reset"):
|
94 |
+
st.session_state.knowledge_base = None
|
95 |
+
st.session_state.messages = []
|
96 |
+
st.rerun() # Rerun the script
|