Skip to content

Commit

Permalink
Merge branch 'main' into Document_Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lmitlaender authored Nov 9, 2023
2 parents 9946ccf + 88790ef commit dd5fa6c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHAT_URL=host.docker.internal
CHAT_PORT=8001
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.10

WORKDIR /app

COPY . /app
COPY ./requirements.txt /app

RUN apt-get update && apt-get install -y \
build-essential \
Expand All @@ -13,6 +13,8 @@ RUN apt-get update && apt-get install -y \

RUN pip3 install -r requirements.txt

COPY . /app

# Expose the ports that your app uses
EXPOSE 8501

Expand Down
58 changes: 52 additions & 6 deletions frontend/streamlit_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

dotenv.load_dotenv()

CHAT_URL = os.getenv("CHAT_URL")
CHAT_PORT = os.getenv("CHAT_PORT")

# Constants
PDF_FILE_TYPE = "pdf"
META_DATA_HEIGHT = 500
Expand Down Expand Up @@ -41,8 +44,48 @@ def upload_files(uploaded_files):
logger.error(e)
return e

def get_conversation_id(conv_type = "Q_AND_A", return_sys_message = False):
if "conversation_id" in st.session_state:
return st.session_state.conversation_id

body = {"conversation_type": conv_type}

response = requests.post(url="http://" + CHAT_URL + ":" + CHAT_PORT + "/startConversation", json=body)
response.raise_for_status()
response_json = response.json()
st.session_state.conversation_id = response_json["conversationId"]
logger.info(f"Started conversation: {st.session_state.conversation_id}")

if return_sys_message:
return st.session_state.conversation_id, response_json["history"][0]["message"]
return st.session_state.conversation_id

def send_chat(message):
try:
conversation_id = get_conversation_id()

body = {
"conversationId": conversation_id,
"correlationId": "StreamlitUI",
"message": message,
"type": "user",
"creationDate": 0
}

response = requests.post(url="http://" + CHAT_URL + ":" + CHAT_PORT + "/chat", json=body)
response.raise_for_status()
response_json = response.json()

return True, response_json["message"]
except Exception as e:
return False, e

if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "Wie kann ich dir helfen?"}]
try:
conversation_id, first_message = get_conversation_id(return_sys_message=True)
st.session_state["messages"] = [{"role": "assistant", "content": first_message}]
except:
st.session_state["messages"] = [{"role": "assistant", "content": "Wie kann ich dir helfen?"}]

for msg in st.session_state.messages:
#if len(st.session_state.messages) == 1:
Expand All @@ -52,11 +95,14 @@ def upload_files(uploaded_files):
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
#send request
#
success, response = send_chat(prompt)

#check response
msg = "BOT RESPONSE"
st.session_state.messages.append({"role": "assistant", "content": msg})
st.chat_message("assistant").write(msg)
st.session_state.messages.append({"role": "assistant", "content": response})
if success:
st.chat_message("assistant").write(response)
else:
st.chat_message("assistant").error(response)

with st.sidebar:
st.subheader("Expend the bots knowledge with your pdfs:")
Expand All @@ -69,4 +115,4 @@ def upload_files(uploaded_files):
submitted = st.form_submit_button("Upload")
if submitted:
logger.info("Started Upload")
st.toast(upload_files(uploaded_files=uploaded_files), icon="🚨")
st.toast(upload_files(uploaded_files=uploaded_files), icon="🚨")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
loguru==0.7.0
streamlit==1.26.0
python-dotenv==1.0.0

0 comments on commit dd5fa6c

Please sign in to comment.