Update src/app.py
Browse files- src/app.py +128 -129
src/app.py
CHANGED
|
@@ -1,130 +1,129 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from run_model import generate_response, generate_RAG_response
|
| 3 |
-
from web_search import search_web
|
| 4 |
-
from deep_research import perform_deep_research
|
| 5 |
-
import tempfile
|
| 6 |
-
|
| 7 |
-
st.set_page_config(layout="wide")
|
| 8 |
-
|
| 9 |
-
def main():
|
| 10 |
-
st.title("
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
with st.sidebar:
|
| 14 |
-
st.title("Tools")
|
| 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 |
-
st.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
st.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
tmp_file.
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
#
|
| 123 |
-
#
|
| 124 |
-
#
|
| 125 |
-
#
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
if __name__ == "__main__":
|
| 130 |
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from run_model import generate_response, generate_RAG_response
|
| 3 |
+
from web_search import search_web
|
| 4 |
+
from deep_research import perform_deep_research
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
st.set_page_config(layout="wide")
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
st.title("Chat with AI")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
with st.sidebar:
|
| 14 |
+
st.title("Tools")
|
| 15 |
+
|
| 16 |
+
option = st.selectbox(
|
| 17 |
+
"Choose tools",
|
| 18 |
+
("Simple Chat", "Web Search", "Upload PDF", "Deep Web Search"),
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
temperature = st.slider(
|
| 22 |
+
label="Temperature (controls randomness)",
|
| 23 |
+
min_value=0.0,
|
| 24 |
+
max_value=2.0,
|
| 25 |
+
value=1.0,
|
| 26 |
+
step=0.01,
|
| 27 |
+
help="Lower = more deterministic, Higher = more random"
|
| 28 |
+
)
|
| 29 |
+
# Top-k sampling
|
| 30 |
+
top_k = st.slider(
|
| 31 |
+
label="Top-k (limits to top K tokens by probability)",
|
| 32 |
+
min_value=0,
|
| 33 |
+
max_value=100,
|
| 34 |
+
value=50,
|
| 35 |
+
step=1,
|
| 36 |
+
help="0 = disable top-k filtering"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Top-p (nucleus sampling)
|
| 40 |
+
top_p = st.slider(
|
| 41 |
+
label="Top-p (nucleus sampling cutoff)",
|
| 42 |
+
min_value=0.0,
|
| 43 |
+
max_value=1.0,
|
| 44 |
+
value=0.9,
|
| 45 |
+
step=0.01,
|
| 46 |
+
help="0.0 = conservative, 1.0 = more random"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if option == "Upload PDF":
|
| 50 |
+
file = st.file_uploader(label="Uploaded file will provide context to LLM", type="pdf")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if option == "Web Search":
|
| 54 |
+
st.write("Web Search Enabled for next query")
|
| 55 |
+
# WB_SEARCH = True
|
| 56 |
+
|
| 57 |
+
if option == "None":
|
| 58 |
+
st.warning("You are not using any tool")
|
| 59 |
+
|
| 60 |
+
if option == "Deep Web Search":
|
| 61 |
+
st.write("Deep Web Research Enabled for next query")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# col1, col2 = st.columns([6, 1], gap="small")
|
| 66 |
+
|
| 67 |
+
# column 1
|
| 68 |
+
# with col1:
|
| 69 |
+
if "messages" not in st.session_state:
|
| 70 |
+
st.session_state.messages = []
|
| 71 |
+
|
| 72 |
+
for msg in st.session_state.messages:
|
| 73 |
+
with st.chat_message(msg["role"]):
|
| 74 |
+
st.markdown(msg["content"])
|
| 75 |
+
|
| 76 |
+
# prompt = st.chat_input("Say something...")
|
| 77 |
+
|
| 78 |
+
if prompt:= st.chat_input("Say something..."):
|
| 79 |
+
# Display user message
|
| 80 |
+
st.chat_message("user").markdown(prompt)
|
| 81 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 82 |
+
if option == "Simple Chat":
|
| 83 |
+
|
| 84 |
+
response = generate_response(history=st.session_state.messages, query=prompt, temperature=temperature, top_k=top_k, top_p=top_p)
|
| 85 |
+
st.chat_message("assistant").markdown(response)
|
| 86 |
+
# st.session_state.messages.append({"role": "assistant", "content": response})
|
| 87 |
+
|
| 88 |
+
if option == "Web Search":
|
| 89 |
+
st.session_state.messages.append([{"role": "user", "content" : prompt}])
|
| 90 |
+
|
| 91 |
+
response, sources = search_web(prompt)
|
| 92 |
+
# asnswer = response
|
| 93 |
+
# st.chat_message("assistant").markdown(f"{response}\n\n###Sources\n{'\n'.join([source for source in sources])}")
|
| 94 |
+
with st.chat_message("assistant"):
|
| 95 |
+
st.markdown(f"{response}\n\n### Sources\n" + "\n".join(sources))
|
| 96 |
+
|
| 97 |
+
if option == "Deep Web Search":
|
| 98 |
+
st.session_state.messages.append([{"role": "user", "content" : prompt}])
|
| 99 |
+
|
| 100 |
+
response = perform_deep_research(prompt)
|
| 101 |
+
# asnswer = response
|
| 102 |
+
# st.chat_message("assistant").markdown(f"{response}\n\n###Sources\n{'\n'.join([source for source in sources])}")
|
| 103 |
+
with st.chat_message("assistant"):
|
| 104 |
+
st.markdown(response)
|
| 105 |
+
|
| 106 |
+
if option == "Upload PDF":
|
| 107 |
+
st.session_state.messages.append([{"role": "user", "content" : prompt}])
|
| 108 |
+
|
| 109 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 110 |
+
tmp_file.write(file.read())
|
| 111 |
+
tmp_path = tmp_file.name
|
| 112 |
+
print(tmp_path)
|
| 113 |
+
response = generate_RAG_response(prompt, tmp_path, st.session_state.messages)
|
| 114 |
+
# print(file)
|
| 115 |
+
with st.chat_message("assistant"):
|
| 116 |
+
st.markdown(response)
|
| 117 |
+
|
| 118 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# # Slider in column 2
|
| 122 |
+
# with col2:
|
| 123 |
+
# temperature = st.slider("Temperature", 0.0, 1.0, 0.0)
|
| 124 |
+
# top_k = st.slider("Top k", 0.0, 100.0, 40.0)
|
| 125 |
+
# top_p = st.slider("Top p", 0.0, 1.0, 0.95)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
|
|
|
| 129 |
main()
|