MM_Math_Helper / app.py
Nathan Butters
Fixed Image to Model!!!
2960307
"""UI for the Math Helper prototype for exploring safety engineering concepts and LLMs.
Using Streamlit to provide a python-based front end.
"""
#Import the libraries for human interaction and visualization.
import streamlit as st
import logging
from helpers.constant import *
from helpers.chat import basicChat, guidedMM, mmChat
import base64
logger = logging.getLogger(__name__)
logging.basicConfig(filename='app.log', level=logging.INFO)
def update_model(name):
if name == "Llama":
st.session_state.model = LLAMA
else:
st.session_state.model = QWEN
if "model" not in st.session_state:
st.session_state.model = LLAMA
if "systemPrompt" not in st.session_state:
st.session_state.systemPrompt = "Model"
st.set_page_config(page_title="IMSA Math Helper v0.1")
st.title("IMSA Math Helper v0.1")
with st.sidebar:
st.info("""This prototype is designed to help students explore the potential and risks of using LLMs to help learn math.
This version (v.01) exists to demonstrate an end-to-end proof of concept for the students to engage with to identify
hazards within the system design. Future iterations will include the constraints they devise to improve the system.""")
# User selects a model
model_choice = st.radio("Please select the model:", options=["Llama","QWEN"])
update_model(model_choice)
logger.info(f"Model changed to {model_choice}.")
systemPrompt = st.radio("Designate a control persona:",options=["Model","Tutor"])
st.session_state.systemPrompt = systemPrompt
liveExperience = st.checkbox("Check to enable the live experience.")
st.subheader(f"This experience is currently running on {st.session_state.model}.")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if liveExperience:
enable = st.checkbox("Enable camera to upload a picture of your math problem.")
picture = st.camera_input("Take a picture", disabled=not enable)
if picture is not None:
st.image(picture)
picBytes= picture.getvalue()
b64IMG = base64.b64encode(picBytes).decode('utf-8')
img_url = f"data:image/jpeg;base64,{b64IMG}"
guidedMM(st.session_state.systemPrompt, img_url)
else:
st.info("Please select an example image to understand the basic potential of the app before using the live app. You can ask the model a question about these images to see how it responds.")
example = st.selectbox("Select an example image", options=["Example 1", "Example 2"])
if example == "Example 1":
img_url = "https://huggingface.co/spaces/butterswords/MM_Math_Helper/resolve/main/tempDir/example1.png"
elif example == "Example 2":
img_url = "https://huggingface.co/spaces/butterswords/MM_Math_Helper/resolve/main/tempDir/example2.png"
st.image(img_url)
guidedMM(st.session_state.systemPrompt, img_url)