|
import streamlit as st |
|
import requests |
|
import utils |
|
|
|
|
|
with open('styles.css') as f: |
|
css = f.read() |
|
|
|
st.markdown(f'<style>{css}</style>', unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
col1, col2 = st.columns([1, 4]) |
|
with col1: |
|
st.image("brainbot.png", width=100) |
|
with col2: |
|
st.title("Web-Chat") |
|
|
|
question = None |
|
llm = st.session_state["llm"] |
|
|
|
if "current_website" in st.session_state: |
|
current_website = st.session_state['current_website'] |
|
if st.sidebar.button("Upload New Webpage Link"): |
|
st.switch_page("BrainBot.py") |
|
st.subheader("Your website content has been uploaded successfully. You can now chat with it.") |
|
st.success(current_website) |
|
question = st.chat_input("Type your question here...") |
|
else: |
|
st.warning("Upload a webpage link to begin chat with it.") |
|
if st.button("Upload Webpage Link"): |
|
st.switch_page("BrainBot.py") |
|
|
|
|
|
|
|
if st.session_state['uploaded_link'] == True: |
|
st.session_state['web_chat_history'] = [] |
|
|
|
|
|
for message in st.session_state['web_chat_history']: |
|
with st.chat_message("user"): |
|
st.write(message["Human"]) |
|
with st.chat_message("ai"): |
|
st.markdown(utils.format_response(message["AI"])) |
|
|
|
|
|
|
|
|
|
if question is not None: |
|
|
|
with st.chat_message("user"): |
|
st.write(question) |
|
|
|
resource = "web" |
|
|
|
try: |
|
|
|
data = {"question": question, "resource": resource} |
|
FASTAPI_URL = f"http://localhost:8000/answer_with_chat_history/{llm}" |
|
with st.spinner("Generating response..."): |
|
response = requests.post(FASTAPI_URL, json=data) |
|
|
|
st.session_state['web_chat_history'].append({"Human": question, "AI": response.text}) |
|
st.session_state['uploaded_link'] = False |
|
|
|
with st.chat_message("ai"): |
|
|
|
formatted_response = utils.format_response(response.text) |
|
st.markdown(formatted_response) |
|
except Exception as e: |
|
st.switch_page("error.py") |