File size: 4,581 Bytes
066e3f7
 
 
5a04e4b
 
 
 
066e3f7
46da57c
066e3f7
76a2630
0b329dc
76a2630
 
 
 
fe712ed
 
7f4595d
 
fe712ed
 
 
 
e4c43ab
 
8615165
e4c43ab
 
8615165
f1dead6
7f4595d
fe712ed
7f4595d
 
ca4d5fb
fe712ed
3362f90
2d007ea
16b3f53
5b72b63
2d007ea
3603314
2d007ea
fe712ed
 
3362f90
7f4595d
 
 
fe712ed
 
 
3362f90
ca4d5fb
fe712ed
 
975bbfc
 
7f4595d
 
3603314
7f4595d
 
 
 
 
 
 
fe712ed
7f4595d
 
fe712ed
 
 
 
 
7f4595d
 
3403bfc
fe712ed
 
ef03b00
fe712ed
 
 
7f4595d
fe712ed
 
7f4595d
fe712ed
7f4595d
0b329dc
ef03b00
7f4595d
fe712ed
 
 
 
 
 
 
 
 
ca4d5fb
fe712ed
 
 
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
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"""
    <div class="llm-response">
        <div class="question">
            {question_text}
        </div>
        <div class="response">
            {response_text}
        </div>
    </div>
    """, unsafe_allow_html=True)

# ── Main UI ───────────────────────────────────────────────────────────────────
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:
            ### 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'<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,
)