import subprocess def install_dependencies(): subprocess.check_call(["python", "-m", "pip", "install", "--upgrade", "pip"]) subprocess.check_call(["python", "-m", "pip", "install", "--upgrade", "huggingface_hub"]) subprocess.check_call(["python", "-m", "pip", "install", "--upgrade", "streamlit"]) subprocess.check_call(["python", "-m", "pip", "install", "--upgrade", "requests"]) # other packages here install_dependencies() import streamlit as st # import requests import json import os from huggingface_hub import InferenceClient # ── Page config ────────────────────────────────────────────────────────────── st.set_page_config( page_title="Math Bot", page_icon="➗", layout="wide", ) # ── Custom CSS ──────────────────────────────────────────────────────────────── def local_css(file_name): with open(file_name) as f: st.markdown(f'{f.read()}', unsafe_allow_html=True) # Call the function at the top of your app # local_css("./style.css") local_css("style.css") st.title("Math Bot") # ── Get answers via HuggingFace LLM ─────────────────────────────────────── def get_llm_response(user_prompt): """Use HF Inference API to get help with highschool math from a natural-language prompt.""" client = InferenceClient( # model="mistralai/Mistral-7B-Instruct-v0.3", # model="mistralai/Mistral-7B-Instruct-v0.2", # model="nvidia/Gemma-4-26B-A4B-NVFP4", # model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", model="meta-llama/Llama-3.3-70B-Instruct", token=os.getenv("mathbot110526read"), provider="fireworks-ai" ) system_prompt = ( "You are a helpful homework assistant that helps high school students with homework, especially math. " "Respond ONLY with an explanation of the problem that stduent poses." "Never give a direct answer to the problem." ) messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": f"Provide help with this question:\n\n{user_prompt}, give step-by-step diretives, if possible."}, ] # response = client.chat_completion(messages=messages, max_tokens=256, temperature=0.1) response = client.chat_completion(messages=messages, max_tokens=4000, temperature=0.1) result = response.choices[0].message.content.strip() return result # ── Show response ──────────────────────────────────────────────── def show_response(question_text, response_text): st.markdown(f"""
{question_text}
{response_text}
""", unsafe_allow_html=True) # ── Main UI ─────────────────────────────────────────────────────────────────── st.markdown('
➗ Math Bot
', unsafe_allow_html=True) st.markdown('
powered by Llama 70B · Hugging Face
', unsafe_allow_html=True) user_input = st.text_input( label="Your question", placeholder='e.g. "What are the roots of x^2 - 5x - 6?"', label_visibility="collapsed", ) run = st.button("Get Help → → →", use_container_width=False) if run and user_input.strip(): with st.spinner("Asking the LLM to help with math…"): try: ### get and handle response here response = get_llm_response(user_prompt=user_input) show_response(user_input, response) except Exception as e: st.markdown(f'
⚠️ LLM error: {e}
', unsafe_allow_html=True) elif run: st.markdown('
Please enter a question first.
', unsafe_allow_html=True) st.markdown("---") st.markdown( '
' 'LLM: Llama-3.3-70B-Instruct via Hugging Face Inference API' '
', unsafe_allow_html=True, )