pratham0011 commited on
Commit
039e67a
·
verified ·
1 Parent(s): 0c1f388

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +50 -0
streamlit_app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+
5
+ # Streamlit UI
6
+ def streamlit_ui():
7
+ st.title("Chat with your Document 📄")
8
+ st.markdown("Chat here👇")
9
+
10
+ icons = {"assistant": "🤖", "user": "👤"}
11
+
12
+ if 'messages' not in st.session_state:
13
+ st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF, DOCX, or TXT file and ask me anything about its content.'}]
14
+
15
+ for message in st.session_state.messages:
16
+ with st.chat_message(message['role'], avatar=icons[message['role']]):
17
+ st.write(message['content'])
18
+
19
+ with st.sidebar:
20
+ st.title("Menu:")
21
+ uploaded_file = st.file_uploader("Upload your document (PDF, DOCX, TXT)", type=["pdf", "docx", "txt"])
22
+ if st.button("Submit & Process") and uploaded_file:
23
+ with st.spinner("Processing..."):
24
+ files = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
25
+ response = requests.post("http://localhost:8000/upload", files=files)
26
+ if response.status_code == 200:
27
+ st.success("File uploaded and processed successfully")
28
+ else:
29
+ st.error("Error uploading file")
30
+
31
+ user_prompt = st.chat_input("Ask me anything about the content of the document:")
32
+
33
+ if user_prompt:
34
+ st.session_state.messages.append({'role': 'user', "content": user_prompt})
35
+ with st.chat_message("user", avatar=icons["user"]):
36
+ st.write(user_prompt)
37
+
38
+ # Trigger assistant's response retrieval and update UI
39
+ with st.spinner("Thinking..."):
40
+ response = requests.post("http://localhost:8000/query", json={"question": user_prompt})
41
+ if response.status_code == 200:
42
+ assistant_response = response.json()["response"]
43
+ with st.chat_message("assistant", avatar=icons["assistant"]):
44
+ st.write(assistant_response)
45
+ st.session_state.messages.append({'role': 'assistant', "content": assistant_response})
46
+ else:
47
+ st.error("Error querying document")
48
+
49
+ if __name__ == "__main__":
50
+ streamlit_ui()