robertselvam commited on
Commit
1ee06ff
·
verified ·
1 Parent(s): 37bde1e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tempfile
3
+ from dspy_qa import DocQA
4
+
5
+ st.set_page_config(page_title="DoC QA", layout="wide")
6
+ # st.title("📄 Chat over PDF using DSPy 🚀")
7
+ st.markdown("""
8
+ <div class="st-emotion-cache-12fmjuu">
9
+ <h1>Document QA DSPY</h1>
10
+ </div>
11
+ """, unsafe_allow_html=True)
12
+
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
+ .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{
28
+ color:black;
29
+ }
30
+ </style>
31
+ """, unsafe_allow_html=True)
32
+
33
+ # Initialize chat history
34
+ if "knowledge_base" not in st.session_state:
35
+ st.session_state.knowledge_base = None
36
+
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
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
41
+ temp_file.write(pdf_file.getbuffer())
42
+ temp_file_path = temp_file.name
43
+ docqa = DocQA(temp_file_path)
44
+ if docqa:
45
+ st.success("PDF file uploaded successfully!")
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 = []
53
+
54
+ # Display chat messages from history on app rerun
55
+ for message in st.session_state.messages:
56
+ with st.chat_message(message["role"]):
57
+ st.markdown(message["content"])
58
+
59
+
60
+ if st.button("Reset"):
61
+ st.session_state.knowledge_base = None
62
+ st.session_state.messages = []
63
+ st.rerun() # Rerun the script
64
+
65
+
66
+ # Accept user input
67
+ if question := st.chat_input("What is up?"):
68
+ # Add user message to chat history
69
+ st.session_state.messages.append({"role": "user", "content": question})
70
+ # Display user message in chat message container
71
+ with st.chat_message("user"):
72
+ st.markdown(question)
73
+
74
+ # Display assistant response in chat message container
75
+ with st.chat_message("assistant"):
76
+ response = docqa.run(question)
77
+ st.markdown(response)
78
+ st.session_state.messages.append({"role": "assistant", "content": response})