File size: 7,917 Bytes
b4cf5e3 10dac13 d8b0266 0642723 023bbb4 e82ceb3 7ce3cce cdb473b 4b0e752 c1f5e87 7ce3cce f18f917 ef19f5d 449c64b 51d34e2 d8b0266 449c64b cf77265 d8b0266 449c64b 10dac13 ef19f5d 7ce3cce 10dac13 c1f5e87 51d34e2 c1f5e87 10dac13 c1f5e87 dfdc78c c1f5e87 dfdc78c c1f5e87 dfdc78c c1f5e87 dfdc78c c1f5e87 d8b0266 ff6a260 10dac13 ff6a260 10dac13 ff6a260 10dac13 ff6a260 10dac13 ff6a260 10dac13 ff6a260 10dac13 ff6a260 10dac13 ff6a260 6d20b45 10dac13 023bbb4 6d20b45 10dac13 6d20b45 ff6a260 10dac13 023bbb4 10dac13 ff6a260 10dac13 023bbb4 10dac13 ff6a260 10dac13 ff6a260 5085ed3 d8b0266 c12d9f6 10dac13 ff6a260 10dac13 ff6a260 10dac13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import os
os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit"
from langchain_huggingface import HuggingFaceEndpoint
import streamlit as st
from langchain_core.messages import HumanMessage
# constants
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"
# Check if HF token is set
if not hf_token:
st.error("HF_TOKEN is not set. Please add it to your HuggingFace secrets.")
st.stop()
# remembers session
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") # Optional but ensures it's set
return HuggingFaceEndpoint(
repo_id=model_id,
max_new_tokens=max_new_tokens,
temperature=temperature,
)
# create llm
llm = get_llm()
# prompts
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.
"""
# count the number of times "I don't know is clicked"
if "retry_count" not in st.session_state:
st.session_state.retry_count = 0
# Initialize session state
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
)
# Outer container for neat padding
with st.container():
# Question area
st.text_area(
label="Type your response here.",
value="",
height=100,
key="question_input",
)
st.markdown("")
# Help Button Logic
def toggle_help():
st.session_state.help_clicks += 1
st.session_state.button_clicked = None # Reset help text on new toggle
# Help Button (main toggle)
col1, col2, col3 = st.columns([1, 3, 1])
with col2:
st.button("Help", on_click=toggle_help)
# Show 3 sub-buttons if Help clicked an odd number of times
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("---") # Divider for clarity
col1, col2, col3 = st.columns(3)
with col1:
if st.button("π Explain the question"):
if st.session_state.button_clicked != "Explain the question":
# First time clicked
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 # Save prompt for retry
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":
# First time clicked
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 # Save prompt for retry
st.session_state.button_clicked = "Give an example"
with col3:
if st.button("π€ Who cares?"):
if st.session_state.button_clicked != "Who cares?":
# First time clicked
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 # Save prompt for retry
st.session_state.button_clicked = "Who cares?"
st.markdown("---")
# Display response text if a sub-button is clicked
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)
# Optional: Add footer or spacing
st.markdown("<br><br>", unsafe_allow_html=True)
# css
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,
) |