|
import os |
|
os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit" |
|
from langchain_huggingface import HuggingFaceEndpoint |
|
import streamlit as st |
|
from langchain_core.messages import HumanMessage |
|
|
|
|
|
QUESTION = "Compute the integral of f(x) = x^2." |
|
MODEL = "mistralai/Mistral-7B-Instruct-v0.3" |
|
hf_token = os.getenv("HF_TOKEN") |
|
SUBJECT = "Calculus BC" |
|
|
|
|
|
if not hf_token: |
|
st.error("HF_TOKEN is not set. Please add it to your HuggingFace secrets.") |
|
st.stop() |
|
|
|
|
|
if "response" not in st.session_state: |
|
st.session_state.response = "" |
|
|
|
def get_llm(model_id=MODEL, max_new_tokens=300, temperature=0.7): |
|
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN") |
|
|
|
return HuggingFaceEndpoint( |
|
repo_id=model_id, |
|
max_new_tokens=max_new_tokens, |
|
temperature=temperature, |
|
) |
|
|
|
|
|
llm = get_llm() |
|
|
|
|
|
prompt = f""" |
|
You are an AI assistant designed to support high school students in the subject of {SUBJECT}. |
|
Your role is to offer friendly, helpful, concise, in-depth guidance, just like a supportive teacher would. |
|
|
|
Please follow these guidelines: |
|
|
|
1. Maintain a polite, respectful, and professional tone at all times. |
|
2. Adhere to ethical principles β do not promote cheating, harmful behavior, or misinformation. |
|
3. Interact in a warm, encouraging, and student-centered style β use clear explanations, positive reinforcement, and examples when needed. |
|
4. The word limit is 180 words. |
|
""" |
|
|
|
p_explanation = """ |
|
5. Focus on thoroughly explaining the question by breaking down its components. Clarify the key concepts and definitions involved, ensuring that the explanation helps the reader fully understand what the question is asking. Avoid jumping to answers or examples; instead, concentrate on making the meaning and scope of the question clear. |
|
6. Do not include specific examples or real-world applications in your response. |
|
""" |
|
p_example = """ |
|
5. Focus on providing three distinct examples that illustrate different aspects or variations of the question. Each example should highlight a unique approach or scenario related to the topic, helping to clarify the concept from multiple perspectives. |
|
6. Do not include any explanation or real-world applications in your response. |
|
""" |
|
p_application = """ |
|
5. Provide two clear and relevant real-world applications related to the question or topic. Explain how each application connects to the concepts being discussed, demonstrating practical uses or implications. |
|
6. Do not include any explanation or examples in your response. |
|
""" |
|
|
|
|
|
if "retry_count" not in st.session_state: |
|
st.session_state.retry_count = 0 |
|
|
|
|
|
|
|
if "help_clicks" not in st.session_state: |
|
st.session_state.help_clicks = 0 |
|
if "button_clicked" not in st.session_state: |
|
st.session_state.button_clicked = None |
|
|
|
st.set_page_config(page_title="Interactive Help Interface", layout="centered") |
|
|
|
st.markdown("## Sample Question Interface") |
|
st.markdown("") |
|
st.markdown( |
|
f"<p style='font-size:20px;'>{QUESTION}</p>", |
|
unsafe_allow_html=True |
|
) |
|
|
|
with st.container(): |
|
|
|
st.text_area( |
|
label="Type your response here.", |
|
value="", |
|
height=100, |
|
key="question_input", |
|
) |
|
|
|
st.markdown("") |
|
|
|
|
|
def toggle_help(): |
|
st.session_state.help_clicks += 1 |
|
st.session_state.button_clicked = None |
|
|
|
|
|
col1, col2, col3 = st.columns([1, 3, 1]) |
|
with col2: |
|
st.button("Help", on_click=toggle_help) |
|
|
|
|
|
if st.session_state.help_clicks % 2 == 1: |
|
st.markdown("### Need Help?") |
|
st.markdown("Choose an option below to better understand the question.") |
|
with st.container(): |
|
st.markdown("---") |
|
col1, col2, col3 = st.columns(3) |
|
|
|
with col1: |
|
if st.button("π Explain the question"): |
|
if st.session_state.button_clicked != "Explain the question": |
|
|
|
full_prompt = ( |
|
"[INST]<<SYS>>\n" |
|
f"{prompt + p_explanation}\n" |
|
"<</SYS>>\n\n" |
|
f"{QUESTION}\n" |
|
"[/INST]" |
|
) |
|
st.session_state.response = llm.invoke([HumanMessage(content=full_prompt)]) |
|
st.session_state.retry_count = 0 |
|
st.session_state.full_prompt = full_prompt |
|
st.session_state.button_clicked = "Explain the question" |
|
with col2: |
|
if st.button("π‘ Give an example"): |
|
if st.session_state.button_clicked != "Give an example": |
|
|
|
full_prompt = ( |
|
"[INST]<<SYS>>\n" |
|
f"{prompt + p_example}\n" |
|
"<</SYS>>\n\n" |
|
f"{QUESTION}\n" |
|
"[/INST]" |
|
) |
|
st.session_state.response = llm.invoke([HumanMessage(content=full_prompt)]) |
|
st.session_state.retry_count = 0 |
|
st.session_state.full_prompt = full_prompt |
|
st.session_state.button_clicked = "Give an example" |
|
with col3: |
|
if st.button("π€ Who cares?"): |
|
if st.session_state.button_clicked != "Who cares?": |
|
|
|
full_prompt = ( |
|
"[INST]<<SYS>>\n" |
|
f"{prompt + p_application}\n" |
|
"<</SYS>>\n\n" |
|
f"{QUESTION}\n" |
|
"[/INST]" |
|
) |
|
st.session_state.response = llm.invoke([HumanMessage(content=full_prompt)]) |
|
st.session_state.retry_count = 0 |
|
st.session_state.full_prompt = full_prompt |
|
st.session_state.button_clicked = "Who cares?" |
|
st.markdown("---") |
|
|
|
|
|
if st.session_state.button_clicked: |
|
with st.container(): |
|
st.info(st.session_state.response) |
|
|
|
if st.session_state.button_clicked == "Explain the question": |
|
col1, col2, col3 = st.columns([1, 1, 1]) |
|
with col2: |
|
if st.button("I don't understand. Try again.", key="retry_button"): |
|
st.session_state.retry_count += 1 |
|
alt_llm = get_llm(temperature=0.9) |
|
st.session_state.response = alt_llm.invoke([HumanMessage(content=st.session_state.full_prompt)]) |
|
if st.session_state.response: |
|
st.markdown("response") |
|
st.info(st.session_state.response) |
|
|
|
st.markdown("<br><br>", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
div.stButton > button { |
|
width: 200px !important; |
|
height: 3em; |
|
font-size: 1.1rem; |
|
display: block; |
|
margin-left: auto; |
|
margin-right: auto; |
|
} |
|
</style> |
|
""", |
|
unsafe_allow_html=True, |
|
) |