Spaces:
Sleeping
Sleeping
import random | |
import time | |
import requests | |
import streamlit as st | |
random.seed(42) | |
backend_url = "https://sebastianschramm-qa-api.hf.space/answer/" | |
assistant_avatar = "bot_avatar.jpeg" | |
user_avatar = "user_avatar.jpeg" | |
st.set_page_config(page_title="Olympics 2024 Paris", page_icon=assistant_avatar) | |
def stream_bot_message(msg): | |
message_placeholder = st.empty() | |
displayed_message = "" | |
for char in msg: | |
displayed_message += char | |
message_placeholder.chat_message("assistant", avatar=assistant_avatar).write( | |
displayed_message | |
) | |
delay = random.randint(1, 8) | |
time.sleep(0.01 * delay) | |
svg_url = "https://eu.ftp.opendatasoft.com/paris2024/emblem-white.svg" | |
st.markdown( | |
f""" | |
<div style="display: flex; justify-content: center;"> | |
<img src="{svg_url}" alt="SVG Image" style="width:20%; height:auto;"> | |
</div> | |
""", | |
unsafe_allow_html=True, | |
) | |
st.markdown( | |
""" | |
<h1 style='text-align: center;'>FAQs Olympics 2024 Paris</h1> | |
""", | |
unsafe_allow_html=True | |
) | |
st.markdown( | |
""" | |
<div style='text-align: center;'> | |
A chatbot for the Olympics 2024 in Paris. The tool uses <a href='https://data.paris2024.org/pages/accueil/'>paris open data</a> | |
</div> | |
""", | |
unsafe_allow_html=True | |
) | |
st.empty() | |
st.empty() | |
if "messages" not in st.session_state: | |
st.session_state["messages"] = [ | |
{"role": "assistant", "content": "How can I help you? Ask me in English!"} | |
] | |
for msg in st.session_state.messages: | |
st.chat_message(msg["role"], avatar=assistant_avatar).write(msg["content"]) | |
if question := st.chat_input(): | |
with st.spinner('Processing...'): | |
print(f"User question: {question}") | |
st.session_state.messages.append({"role": "user", "content": question}) | |
st.chat_message("user", avatar=user_avatar).write(question) | |
response = requests.post(backend_url, json={"question": question}) | |
print(response) | |
if response.status_code == 200: | |
msg = response.json()["answer"] | |
print(f"Bot answer: {msg}| Question was: {question}") | |
st.session_state.messages.append({"role": "agent", "content": msg}) | |
stream_bot_message(msg) | |
st.success("Done!") |