File size: 5,574 Bytes
6d7f7c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import sympy as sp
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"  
SYSTEM_PROMPT = "You are a helpful tutor. Match the user's level."

tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    dtype=torch.float32,  # CPU
    device_map=None
)
model.eval()


# SymPy check
def verify_math(expr_str: str) -> str:
    try:
        expr = sp.sympify(expr_str)
        simplified = sp.simplify(expr)
        return f"Simplified: {simplified}"
    except Exception as e:
        return f"Could not verify with SymPy: {e}"


# Main function which processes the question 
def generate(question: str, level: str, step_by_step: bool) -> str:
    if not question.strip():
        return "Please enter a question."

    # style forming
    style = f"Level: {level}. {'Explain step-by-step.' if step_by_step else 'Be concise.'}"

    # dynamic amount of tokens
    max_tokens = 128 if level == "Beginner" else 192 if level == "Intermediate" else 256

    # final prompt
    prompt = (
        f"{SYSTEM_PROMPT}\n"
        f"{style}\n"
        f"User question: {question}\n"
        f"Assistant:"
    )

    inputs = tok(prompt, return_tensors="pt")
    with torch.no_grad():
        out = model.generate(
            **inputs,
            max_new_tokens=max_tokens,
            do_sample=False,
            pad_token_id=tok.eos_token_id
        )

    # decode the answer
    text = tok.decode(out[0], skip_special_tokens=True)
    if "Assistant:" in text:
        text = text.split("Assistant:", 1)[1].strip()

    # check if it is a math task
    is_math = any(ch in question for ch in "+-*/=^") or question.lower().startswith((
        "simplify", "derive", "integrate", "laske", "sievennä", "derivoi", "integroi"
    ))
    sympy_note = verify_math(question) if is_math else "No math verification needed."

    return f"{text}\n\n---\n**SymPy check:** {sympy_note}\n\n_Status: Transformers CPU_"


# Building app and IU
def build_app():
    with gr.Blocks(title="LearnLoop — CPU Space") as demo:
        
# CSS styles and adding colours
        gr.HTML("""
        <style>
       
       .gradio-container {
            background-color: #EDF6FA !important;  /* haalea sininen */
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }
       
        /* buttons */
        button {
            border-radius: 8px;
            transition: all 0.2s ease-in-out;
            font-weight: 500;
            letter-spacing: 0.5px;
        }
        button:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }
        button:active {
            filter: brightness(85%);
            transform: scale(0.98);
        }

        /* Explain ja Reset buttons */
        #explain-btn { 
            background-color: #5499C7;
            color: white;
            border: 2px solid #2E86C1;
        }
        #reset-btn { 
            background-color: #EC7063;
            color: white;
            border: 2px solid #CB4335;
        }
        #explain-btn:hover, #reset-btn:hover { 
            opacity: 0.85;
        }
        #explain-btn:active, #reset-btn:active {
            filter: brightness(85%);
            transform: scale(0.98);
        }
        </style>
        """)


        # prints using instructions 
        gr.Markdown("""
        # **LearnL**<span style="font-size:1.2em; color: #21618C">∞</span>**p — AI Tutor**
        This app uses the [Qwen 2.5 model](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) 
        to explain questions at different skill levels. It can also verify 
        mathematical expressions using the SymPy library.

        **How to use:**  
        1️⃣ Type your question or a mathematical expression.  
        2️⃣ Select your level (Beginner, Intermediate, Advanced).  
        3️⃣ Choose whether you want a step-by-step explanation.  
        4️⃣ Press **"Explain"** or **Enter** on your keyboard.  
        5️⃣ If you want to enter a new question, you can press **"Reset"** or simply **type a new question**.  
        
        💬 You can ask your question in **English**.
        """)

        # User's feed
        q = gr.Textbox(label="Your question", placeholder="e.g., simplify (x^2 - 1)/(x - 1)", elem_id="question-box")
        level = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], value="Beginner", label="Level")
        step = gr.Checkbox(value=True, label="Step-by-step")

        
        # Results
        loading = gr.Markdown(visible=False)  # spinner hided at first
        out = gr.Markdown()

        # Buttons next to each other
        with gr.Row():
            btn = gr.Button("Explain", elem_id="explain-btn")
            reset_btn = gr.ClearButton([q, out, loading], value="Reset", elem_id="reset-btn")

        # connect to generate function with spinner
        def wrapped_generate(q_val, level_val, step_val):
            # Näytetään spinner ensin
            loading_text = "⏳ Generating explanation..."
            result = generate(q_val, level_val, step_val)
            # hide spinner when ready
            return "", result 

        btn.click(fn=wrapped_generate, inputs=[q, level, step], outputs=[loading, out])
        q.submit(fn=wrapped_generate, inputs=[q, level, step], outputs=[loading, out])

    return demo

# start the app
if __name__ == "__main__":
    build_app().launch()