Spaces:
Runtime error
Runtime error
HemanthSai7
commited on
Commit
β’
15d650d
1
Parent(s):
2a7e4ad
frontend
Browse files- frontend/components/__init__.py +1 -1
- frontend/{pages β components}/file_streaming.py +18 -0
- frontend/components/toaster.py +0 -22
- frontend/pages/2_π€_bot.py +38 -93
- test.py +87 -26
frontend/components/__init__.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
from .authors import *
|
2 |
from .user_greetings import *
|
3 |
from .logo import add_logo
|
4 |
-
from .
|
|
|
1 |
from .authors import *
|
2 |
from .user_greetings import *
|
3 |
from .logo import add_logo
|
4 |
+
from .file_streaming import *
|
frontend/{pages β components}/file_streaming.py
RENAMED
@@ -1,10 +1,28 @@
|
|
1 |
import os
|
|
|
2 |
|
3 |
import streamlit as st
|
4 |
|
5 |
from langchain.callbacks.base import BaseCallbackHandler
|
6 |
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
class StreamHandler(BaseCallbackHandler):
|
9 |
def __init__(
|
10 |
self, container: st.delta_generator.DeltaGenerator, initial_text: str = ""
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
|
4 |
import streamlit as st
|
5 |
|
6 |
from langchain.callbacks.base import BaseCallbackHandler
|
7 |
|
8 |
|
9 |
+
@st.cache_resource(ttl="1h")
|
10 |
+
def upload_data(uploaded_files):
|
11 |
+
files = {"file": uploaded_files}
|
12 |
+
with st.spinner("Uploading PDF..."):
|
13 |
+
response = requests.post(
|
14 |
+
"http://127.0.0.1:8000/api/upload", files=files
|
15 |
+
)
|
16 |
+
|
17 |
+
if response.status_code == 200:
|
18 |
+
st.success(
|
19 |
+
f'{response.json()["message"][0]} Vector Store created successfully!'
|
20 |
+
)
|
21 |
+
st.session_state.uploaded_pdf = True
|
22 |
+
else:
|
23 |
+
st.error("Failed to upload PDF!")
|
24 |
+
|
25 |
+
|
26 |
class StreamHandler(BaseCallbackHandler):
|
27 |
def __init__(
|
28 |
self, container: st.delta_generator.DeltaGenerator, initial_text: str = ""
|
frontend/components/toaster.py
DELETED
@@ -1,22 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
|
3 |
-
import time
|
4 |
-
|
5 |
-
|
6 |
-
def toaster_messages(func: callable):
|
7 |
-
def wrapper():
|
8 |
-
msg = st.toast("Uploading PDF...")
|
9 |
-
time.sleep(8)
|
10 |
-
msg.toast("Converting PDF into small chunks...")
|
11 |
-
time.sleep(8)
|
12 |
-
msg.toast("Breaking down chunks into tokens...")
|
13 |
-
time.sleep(8)
|
14 |
-
msg.toast("Creating embeddging vectors...")
|
15 |
-
time.sleep(8)
|
16 |
-
msg.toast("Creating vector store...")
|
17 |
-
time.sleep(8)
|
18 |
-
msg.toast("Vector store created successfully!")
|
19 |
-
|
20 |
-
func()
|
21 |
-
|
22 |
-
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/pages/2_π€_bot.py
CHANGED
@@ -3,113 +3,58 @@ import requests
|
|
3 |
import streamlit as st
|
4 |
|
5 |
from layouts.mainlayout import mainlayout
|
|
|
6 |
|
7 |
-
|
8 |
-
def upload_data():
|
9 |
-
# upload pdf
|
10 |
-
upload_pdf = st.file_uploader("Upload PDF", type="pdf")
|
11 |
-
if upload_pdf is not None:
|
12 |
-
files = {"file": upload_pdf}
|
13 |
-
with st.spinner("Uploading PDF..."):
|
14 |
-
response = requests.post(
|
15 |
-
"https://hemanthsai7-studybotapi.hf.space/api/upload", files=files
|
16 |
-
)
|
17 |
-
|
18 |
-
if response.status_code == 200:
|
19 |
-
st.success(
|
20 |
-
f'{response.json()["message"][0]}. Vector Store created successfully!'
|
21 |
-
)
|
22 |
-
st.session_state.uploaded_pdf=True
|
23 |
-
else:
|
24 |
-
st.error("Failed to upload PDF!")
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
upload_data()
|
29 |
|
30 |
-
with st.expander("What happens when I upload a PDF? π", expanded=True):
|
31 |
-
st.info(
|
32 |
-
"""
|
33 |
-
- The PDF is uploaded to the backend server. βοΈ
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
)
|
45 |
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
|
|
52 |
|
53 |
-
if "messages" not in st.session_state.keys():
|
54 |
-
st.session_state.messages = [
|
55 |
-
{
|
56 |
-
"role": "assistant",
|
57 |
-
"content": "What's troubling you? Ask me a question right away!",
|
58 |
-
}
|
59 |
-
]
|
60 |
|
61 |
-
|
62 |
-
for message in st.session_state.messages:
|
63 |
-
with st.chat_message(message["role"]):
|
64 |
-
st.write(message["content"])
|
65 |
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
"content": "What's troubling you? Ask me a question right away!",
|
72 |
-
}
|
73 |
-
]
|
74 |
|
|
|
75 |
|
76 |
-
|
|
|
|
|
77 |
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
if dict_message["role"] == "user":
|
82 |
-
question = dict_message["content"]
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
87 |
).json()
|
88 |
-
|
89 |
-
return answer
|
90 |
-
|
91 |
-
|
92 |
-
# User-provided prompt
|
93 |
-
if prompt := st.chat_input(
|
94 |
-
disabled=not st.session_state.messages[-1]["role"] == "assistant",
|
95 |
-
placeholder="Hello, please ask me a question! π€"):
|
96 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
97 |
-
with st.chat_message("user"):
|
98 |
-
st.write(prompt)
|
99 |
-
|
100 |
-
# ask question
|
101 |
-
st.write(st.session_state)
|
102 |
-
|
103 |
-
# Generate a new response if last message is not from assistant
|
104 |
-
if st.session_state.messages[-1]["role"] != "assistant":
|
105 |
-
with st.chat_message("assistant"):
|
106 |
-
with st.spinner("Thinking..."):
|
107 |
-
response = generate_mistral_response(prompt)
|
108 |
-
placeholder = st.empty()
|
109 |
-
full_response = ""
|
110 |
-
for item in response:
|
111 |
-
full_response += item
|
112 |
-
placeholder.markdown(full_response)
|
113 |
-
placeholder.markdown(full_response)
|
114 |
-
message = {"role": "assistant", "content": full_response}
|
115 |
-
st.session_state.messages.append(message)
|
|
|
3 |
import streamlit as st
|
4 |
|
5 |
from layouts.mainlayout import mainlayout
|
6 |
+
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
7 |
|
8 |
+
from components.file_streaming import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
@mainlayout
|
12 |
+
def display():
|
13 |
+
with st.expander("What happens when I upload a PDF? π", expanded=True):
|
14 |
+
st.info(
|
15 |
+
"""
|
16 |
+
- The PDF is uploaded to the backend server. βοΈ
|
17 |
|
18 |
+
- The PDF is converted into small chunks for faster processing. π
|
19 |
+
|
20 |
+
- The chunks are broken down into tokens. A token is a single word or a group of words. π
|
|
|
21 |
|
22 |
+
- The tokens are converted into embedding vectors. π
|
23 |
|
24 |
+
- The embedding vectors are stored in a vector store. ποΈ
|
25 |
+
""",
|
26 |
+
icon="βΉοΈ",
|
27 |
+
)
|
28 |
|
29 |
+
st.divider()
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
display()
|
|
|
|
|
|
|
33 |
|
34 |
+
uploaded_files = st.sidebar.file_uploader(label="Upload PDF files", type=["pdf"])
|
35 |
|
36 |
+
if not uploaded_files:
|
37 |
+
st.info("Please upload PDF documents to continue.")
|
38 |
+
st.stop()
|
39 |
+
upload_data(uploaded_files)
|
|
|
|
|
|
|
40 |
|
41 |
+
msgs = StreamlitChatMessageHistory()
|
42 |
|
43 |
+
if len(msgs.messages) == 0 or st.sidebar.button("Clear message history"):
|
44 |
+
msgs.clear()
|
45 |
+
msgs.add_ai_message("How can I help you?")
|
46 |
|
47 |
+
avatars = {"human": "user", "ai": "assistant"}
|
48 |
+
for msg in msgs.messages:
|
49 |
+
st.chat_message(avatars[msg.type]).write(msg.content)
|
50 |
|
51 |
+
if user_query := st.chat_input(placeholder="Ask me anything!"):
|
52 |
+
st.chat_message("user").write(user_query)
|
|
|
|
|
53 |
|
54 |
+
with st.chat_message("assistant"):
|
55 |
+
retrieval_handler = PrintRetrievalHandler(st.container())
|
56 |
+
stream_handler = StreamHandler(st.empty())
|
57 |
+
response = requests.post(
|
58 |
+
"http://127.0.0.1:8000/api/inference",
|
59 |
+
json={"promptMessage": user_query},
|
60 |
).json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.py
CHANGED
@@ -1,26 +1,87 @@
|
|
1 |
-
class A:
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
class B(A):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# b = B()
|
20 |
-
# print(b.bill)
|
21 |
-
a=A()
|
22 |
-
a.bill=3
|
23 |
-
print(a.bill)
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# class A:
|
2 |
+
# def __init__(self):
|
3 |
+
# self._bill = 1
|
4 |
+
|
5 |
+
# @property
|
6 |
+
# def bill(self):
|
7 |
+
# return self._bill
|
8 |
+
|
9 |
+
# @bill.setter
|
10 |
+
# def bill(self,value):
|
11 |
+
# self._bill = value
|
12 |
+
# # raise PermissionError("You can't change the bill")
|
13 |
+
|
14 |
+
# class B(A):
|
15 |
+
# def __init__(self):
|
16 |
+
# super().__init__()
|
17 |
+
# self._bill = 2
|
18 |
+
|
19 |
+
# # b = B()
|
20 |
+
# # print(b.bill)
|
21 |
+
# a=A()
|
22 |
+
# a.bill=3
|
23 |
+
# print(a.bill)
|
24 |
+
|
25 |
+
# if "uploaded_pdf" in st.session_state.keys():
|
26 |
+
# # chatbot
|
27 |
+
# st.subheader("Ask Studybot a question! π€")
|
28 |
+
|
29 |
+
# if "messages" not in st.session_state.keys():
|
30 |
+
# st.session_state.messages = [
|
31 |
+
# {
|
32 |
+
# "role": "assistant",
|
33 |
+
# "content": "What's troubling you? Ask me a question right away!",
|
34 |
+
# }
|
35 |
+
# ]
|
36 |
+
|
37 |
+
# # Display or clear chat messages
|
38 |
+
# for message in st.session_state.messages:
|
39 |
+
# with st.chat_message(message["role"]):
|
40 |
+
# st.write(message["content"])
|
41 |
+
|
42 |
+
# def clear_chat_history():
|
43 |
+
# st.session_state.messages = [
|
44 |
+
# {
|
45 |
+
# "role": "assistant",
|
46 |
+
# "content": "What's troubling you? Ask me a question right away!",
|
47 |
+
# }
|
48 |
+
# ]
|
49 |
+
|
50 |
+
# st.sidebar.button("Clear Chat History", on_click=clear_chat_history)
|
51 |
+
|
52 |
+
# def generate_mistral_response(question: str):
|
53 |
+
# for dict_message in st.session_state.messages:
|
54 |
+
# if dict_message["role"] == "user":
|
55 |
+
# question = dict_message["content"]
|
56 |
+
|
57 |
+
# answer = requests.post(
|
58 |
+
# "https://hemanthsai7-studybotapi.hf.space/api/inference",
|
59 |
+
# json={"promptMessage": question},
|
60 |
+
# ).json()
|
61 |
+
|
62 |
+
# return answer
|
63 |
+
|
64 |
+
# User-provided prompt
|
65 |
+
# if prompt := st.chat_input(
|
66 |
+
# disabled=not st.session_state.messages[-1]["role"] == "assistant",
|
67 |
+
# placeholder="Hello, please ask me a question! π€"):
|
68 |
+
# st.session_state.messages.append({"role": "user", "content": prompt})
|
69 |
+
# with st.chat_message("user"):
|
70 |
+
# st.write(prompt)
|
71 |
+
|
72 |
+
# # ask question
|
73 |
+
# st.write(st.session_state)
|
74 |
+
|
75 |
+
# # Generate a new response if last message is not from assistant
|
76 |
+
# if st.session_state.messages[-1]["role"] != "assistant":
|
77 |
+
# with st.chat_message("assistant"):
|
78 |
+
# with st.spinner("Thinking..."):
|
79 |
+
# response = generate_mistral_response(prompt)
|
80 |
+
# placeholder = st.empty()
|
81 |
+
# full_response = ""
|
82 |
+
# for item in response:
|
83 |
+
# full_response += item
|
84 |
+
# placeholder.markdown(full_response)
|
85 |
+
# placeholder.markdown(full_response)
|
86 |
+
# message = {"role": "assistant", "content": full_response}
|
87 |
+
# st.session_state.messages.append(message)
|