| 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"]) |
| |
| install_dependencies() |
|
|
| import streamlit as st |
| |
| import json |
| import os |
| from huggingface_hub import InferenceClient |
|
|
| |
| st.set_page_config( |
| page_title="Math Bot", |
| page_icon="➗", |
| layout="wide", |
| ) |
|
|
| |
| def local_css(file_name): |
| with open(file_name) as f: |
| st.markdown(f'{f.read()}', unsafe_allow_html=True) |
|
|
| |
| |
| local_css("style.css") |
| st.title("Math Bot") |
|
|
| |
| def get_llm_response(user_prompt): |
| """Use HF Inference API to get help with highschool math from a natural-language prompt.""" |
| client = InferenceClient( |
| |
| |
| |
| |
| 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=4000, temperature=0.1) |
| result = response.choices[0].message.content.strip() |
| |
| return result |
|
|
| |
| def show_response(question_text, response_text): |
| st.markdown(f""" |
| <div class="llm-response"> |
| <div class="question"> |
| {question_text} |
| </div> |
| <div class="response"> |
| {response_text} |
| </div> |
| </div> |
| """, unsafe_allow_html=True) |
|
|
| |
| st.markdown('<div class="main-title">➗ Math Bot</div>', unsafe_allow_html=True) |
| st.markdown('<div class="subtitle">powered by Llama 70B · Hugging Face</div>', 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: |
| |
| response = get_llm_response(user_prompt=user_input) |
| show_response(user_input, response) |
| |
| except Exception as e: |
| st.markdown(f'<div class="error-box">⚠️ LLM error: {e}</div>', unsafe_allow_html=True) |
|
|
| elif run: |
| st.markdown('<div class="error-box">Please enter a question first.</div>', unsafe_allow_html=True) |
|
|
| st.markdown("---") |
| st.markdown( |
| '<div style="font-family: Space Mono, monospace; font-size: 0.65rem; color: #37474f; text-align:center;">' |
| 'LLM: Llama-3.3-70B-Instruct via Hugging Face Inference API' |
| '</div>', |
| unsafe_allow_html=True, |
| ) |