Spaces:
Sleeping
Sleeping
Upload learnloop.py updated version / Paulina
Browse files- learnloop.py +107 -0
learnloop.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sympy as sp
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 7 |
+
SYSTEM_PROMPT = "You are a helpful tutor. Match the user's level."
|
| 8 |
+
|
| 9 |
+
tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_ID,
|
| 12 |
+
torch_dtype=torch.float32, # CPU
|
| 13 |
+
device_map=None
|
| 14 |
+
)
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
def verify_math(expr_str: str) -> str:
|
| 18 |
+
try:
|
| 19 |
+
expr = sp.sympify(expr_str)
|
| 20 |
+
simplified = sp.simplify(expr)
|
| 21 |
+
return f"Simplified: ${sp.latex(simplified)}$"
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Could not verify with SymPy: {e}"
|
| 24 |
+
|
| 25 |
+
def generate(question: str, level: str, step_by_step: bool) -> str:
|
| 26 |
+
if not question.strip():
|
| 27 |
+
return "Please enter a question."
|
| 28 |
+
style = f"Level: {level}. {'Explain step-by-step.' if step_by_step else 'Be concise.'}"
|
| 29 |
+
prompt = f"System: {SYSTEM_PROMPT}\n{style}\nUser: {question}\nAssistant:"
|
| 30 |
+
inputs = tok(prompt, return_tensors="pt")
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
out = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_new_tokens=192,
|
| 35 |
+
do_sample=True,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
top_p=0.95,
|
| 38 |
+
pad_token_id=tok.eos_token_id
|
| 39 |
+
)
|
| 40 |
+
text = tok.decode(out[0], skip_special_tokens=True)
|
| 41 |
+
if "Assistant:" in text:
|
| 42 |
+
text = text.split("Assistant:", 1)[1].strip()
|
| 43 |
+
is_math = any(ch in question for ch in "+-*/=^") or question.lower().startswith(("simplify","derive","integrate"))
|
| 44 |
+
sympy_note = verify_math(question) if is_math else "No math verification needed."
|
| 45 |
+
return f"{text}\n\n---\n**SymPy check:** {sympy_note}\n_Status: Transformers CPU_"
|
| 46 |
+
|
| 47 |
+
def build_app():
|
| 48 |
+
with gr.Blocks(title="LearnLoop — CPU Space") as demo:
|
| 49 |
+
|
| 50 |
+
# CSS styles and adding colours
|
| 51 |
+
gr.HTML("""
|
| 52 |
+
<style>
|
| 53 |
+
/* Button colours */
|
| 54 |
+
#explain-btn {
|
| 55 |
+
background-color: #5499C7; /* blue Explain */
|
| 56 |
+
color: white;
|
| 57 |
+
border-radius: 8px;
|
| 58 |
+
}
|
| 59 |
+
#reset-btn {
|
| 60 |
+
background-color: #EC7063; /* red Reset */
|
| 61 |
+
color: white;
|
| 62 |
+
border-radius: 8px;
|
| 63 |
+
}
|
| 64 |
+
/* Hover-efect */
|
| 65 |
+
#explain-btn:hover, #reset-btn:hover {
|
| 66 |
+
opacity: 0.85;
|
| 67 |
+
}
|
| 68 |
+
</style>
|
| 69 |
+
""")
|
| 70 |
+
|
| 71 |
+
# prints using instructions
|
| 72 |
+
gr.Markdown("""
|
| 73 |
+
# **LearnL**<span style="font-size:1.2em; color: #21618C">∞</span>**p — AI Tutor**
|
| 74 |
+
This app uses the [Qwen 2.5 model](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct)
|
| 75 |
+
to explain questions at different skill levels. It can also verify
|
| 76 |
+
mathematical expressions using the SymPy library.
|
| 77 |
+
|
| 78 |
+
**How to use:**
|
| 79 |
+
1️⃣ Type your question or a mathematical expression
|
| 80 |
+
2️⃣ Select your level (Beginner, Intermediate, Advanced)
|
| 81 |
+
3️⃣ Choose whether you want a step-by-step explanation
|
| 82 |
+
4️⃣ Press **"Explain"**
|
| 83 |
+
|
| 84 |
+
💬 You can ask your question in **Finnish or English** —
|
| 85 |
+
LearnLoop will reply in the same language you use.
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
q = gr.Textbox(label="Your question", placeholder="e.g., simplify (x^2 - 1)/(x - 1)")
|
| 89 |
+
level = gr.Dropdown(choices=["Beginner","Intermediate","Advanced"], value="Beginner", label="Level")
|
| 90 |
+
step = gr.Checkbox(value=True, label="Step-by-step")
|
| 91 |
+
|
| 92 |
+
# Mardown for results
|
| 93 |
+
out = gr.Markdown()
|
| 94 |
+
|
| 95 |
+
# buttons next to each other
|
| 96 |
+
with gr.Row():
|
| 97 |
+
btn = gr.Button("Explain", elem_id="explain-btn")
|
| 98 |
+
reset_btn = gr.ClearButton([q, out], value="Reset", elem_id="reset-btn")
|
| 99 |
+
|
| 100 |
+
# connect button to generate function
|
| 101 |
+
btn.click(generate, [q, level, step], out)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
return demo
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
build_app().launch()
|