Spaces:
Sleeping
Sleeping
Update src/interface/ui.py
Browse files- src/interface/ui.py +76 -1
src/interface/ui.py
CHANGED
|
@@ -1 +1,76 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 5 |
+
|
| 6 |
+
def get_base64_image(image_path):
|
| 7 |
+
with open(image_path, 'rb') as img_file:
|
| 8 |
+
return base64.b64encode(img_file.read()).decode()
|
| 9 |
+
|
| 10 |
+
def create_interface(call_model):
|
| 11 |
+
if 'chat_history' not in st.session_state:
|
| 12 |
+
st.session_state.chat_history = []
|
| 13 |
+
|
| 14 |
+
# Background
|
| 15 |
+
background_image_path = os.path.abspath("assets/uwf-page-edited.jpg")
|
| 16 |
+
if os.path.exists(background_image_path):
|
| 17 |
+
base64_background_image = get_base64_image(background_image_path)
|
| 18 |
+
st.markdown(
|
| 19 |
+
f"""
|
| 20 |
+
<style>
|
| 21 |
+
.stApp {{
|
| 22 |
+
background-image: url("data:image/jpeg;base64,{base64_background_image}");
|
| 23 |
+
background-size: cover;
|
| 24 |
+
background-position: center;
|
| 25 |
+
background-repeat: no-repeat;
|
| 26 |
+
background-attachment: fixed;
|
| 27 |
+
}}
|
| 28 |
+
</style>
|
| 29 |
+
""",
|
| 30 |
+
unsafe_allow_html=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Header
|
| 34 |
+
col1, col2 = st.columns([1, 3])
|
| 35 |
+
with col1:
|
| 36 |
+
image_path = os.path.abspath("assets/argie-argonaut-gif-edited.png")
|
| 37 |
+
if os.path.exists(image_path):
|
| 38 |
+
st.image(image_path, width=200)
|
| 39 |
+
with col2:
|
| 40 |
+
st.markdown("<h1 style='color:black;'>ARGObot: UWF's Custom Q&A Chatbot</h1>", unsafe_allow_html=True)
|
| 41 |
+
|
| 42 |
+
# Sample questions
|
| 43 |
+
st.markdown("### Example questions:")
|
| 44 |
+
st.markdown("- UWF's values")
|
| 45 |
+
st.markdown("- Student Rights and Responsibilities")
|
| 46 |
+
st.markdown("- Student Resources")
|
| 47 |
+
|
| 48 |
+
# Display conversation
|
| 49 |
+
for message in st.session_state.chat_history:
|
| 50 |
+
if isinstance(message, HumanMessage):
|
| 51 |
+
st.markdown(f"<div style='background-color:#8DC8E8; padding:10px; border-radius:10px;'><strong>You:</strong> {message.content}</div>", unsafe_allow_html=True)
|
| 52 |
+
elif isinstance(message, AIMessage):
|
| 53 |
+
st.markdown(f"<div style='background-color:#E7E7E7; padding:10px; border-radius:10px;'><strong>Chatbot:</strong> {message.content}</div>", unsafe_allow_html=True)
|
| 54 |
+
|
| 55 |
+
# Input form
|
| 56 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
| 57 |
+
user_input = st.text_input("Enter your question here:")
|
| 58 |
+
submitted = st.form_submit_button("Submit")
|
| 59 |
+
|
| 60 |
+
if submitted:
|
| 61 |
+
if user_input.strip():
|
| 62 |
+
state = {
|
| 63 |
+
"input": user_input,
|
| 64 |
+
"chat_history": st.session_state.chat_history,
|
| 65 |
+
"context": "",
|
| 66 |
+
"answer": ""
|
| 67 |
+
}
|
| 68 |
+
result = call_model(state)
|
| 69 |
+
if not result.get("answer"):
|
| 70 |
+
result["answer"] = "Sorry, I couldn't generate an answer."
|
| 71 |
+
|
| 72 |
+
st.session_state.chat_history.append(HumanMessage(user_input))
|
| 73 |
+
st.session_state.chat_history.append(AIMessage(result["answer"]))
|
| 74 |
+
st.rerun()
|
| 75 |
+
else:
|
| 76 |
+
st.warning("Please enter a question.")
|