Spaces:
Sleeping
Sleeping
created app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
from collections import Counter
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# --- CONFIGURATION ---
|
| 8 |
+
# REPLACE WITH YOUR USERNAME
|
| 9 |
+
MODEL_ID = "justhariharan/Qwen2.5-Math-1.5B-Solver"
|
| 10 |
+
|
| 11 |
+
print(f"⏳ Loading {MODEL_ID}... (CPU Mode)")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
torch_dtype=torch.float32,
|
| 16 |
+
device_map="cpu"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# --- HELPER FUNCTIONS ---
|
| 20 |
+
def extract_answer(text):
|
| 21 |
+
"""Extracts the number after #### or the last number found."""
|
| 22 |
+
if "####" in text:
|
| 23 |
+
text = text.split("####")[-1]
|
| 24 |
+
pattern = r"(-?[$0-9.,]{1,})"
|
| 25 |
+
matches = re.findall(pattern, text)
|
| 26 |
+
return matches[-1].replace(",", "").replace("$", "").strip() if matches else None
|
| 27 |
+
|
| 28 |
+
def format_prompt(current_question, history):
|
| 29 |
+
# System Prompt: Friendly Teacher
|
| 30 |
+
system_prompt = """<|im_start|>system
|
| 31 |
+
You are a patient and friendly math teacher.
|
| 32 |
+
1. Solve the problem step-by-step.
|
| 33 |
+
2. Explain the 'logic' simply so a student can understand.
|
| 34 |
+
3. Always end your final result with '#### Number'.<|im_end|>"""
|
| 35 |
+
|
| 36 |
+
# Add History (Short Term Memory - Last 1 Turn)
|
| 37 |
+
history_context = ""
|
| 38 |
+
if len(history) > 0:
|
| 39 |
+
last_q, last_a = history[-1]
|
| 40 |
+
history_context = f"""
|
| 41 |
+
<|im_start|>user
|
| 42 |
+
{last_q}<|im_end|>
|
| 43 |
+
<|im_start|>assistant
|
| 44 |
+
{last_a}<|im_end|>"""
|
| 45 |
+
|
| 46 |
+
# Current Input
|
| 47 |
+
user_input = f"""
|
| 48 |
+
<|im_start|>user
|
| 49 |
+
{current_question}<|im_end|>
|
| 50 |
+
<|im_start|>assistant
|
| 51 |
+
"""
|
| 52 |
+
return system_prompt + history_context + user_input
|
| 53 |
+
|
| 54 |
+
def solve_single(question, history, temperature=0.6):
|
| 55 |
+
"""Standard generation."""
|
| 56 |
+
prompt = format_prompt(question, history)
|
| 57 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 58 |
+
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
outputs = model.generate(
|
| 61 |
+
**inputs,
|
| 62 |
+
max_new_tokens=512,
|
| 63 |
+
temperature=temperature,
|
| 64 |
+
do_sample=True
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
+
if "<|im_start|>assistant" in response:
|
| 69 |
+
return response.split("<|im_start|>assistant")[-1].strip()
|
| 70 |
+
return response
|
| 71 |
+
|
| 72 |
+
def solve_majority_vote(question, history):
|
| 73 |
+
"""Smart Mode: Generates 3 answers and votes."""
|
| 74 |
+
candidates = []
|
| 75 |
+
raw_responses = []
|
| 76 |
+
|
| 77 |
+
for _ in range(3):
|
| 78 |
+
# Higher temp for variety
|
| 79 |
+
resp = solve_single(question, history, temperature=0.8)
|
| 80 |
+
raw_responses.append(resp)
|
| 81 |
+
ans = extract_answer(resp)
|
| 82 |
+
if ans:
|
| 83 |
+
candidates.append(ans)
|
| 84 |
+
|
| 85 |
+
if not candidates:
|
| 86 |
+
return raw_responses[0]
|
| 87 |
+
|
| 88 |
+
vote_counts = Counter(candidates)
|
| 89 |
+
winner_ans = vote_counts.most_common(1)[0][0]
|
| 90 |
+
|
| 91 |
+
for resp in raw_responses:
|
| 92 |
+
if extract_answer(resp) == winner_ans:
|
| 93 |
+
return f"🏆 **High Confidence Answer (Verified 3x)**\n\n{resp}"
|
| 94 |
+
|
| 95 |
+
return raw_responses[0]
|
| 96 |
+
|
| 97 |
+
# --- MAIN CHAT LOGIC ---
|
| 98 |
+
def chat_logic(message, history, smart_mode):
|
| 99 |
+
if smart_mode:
|
| 100 |
+
return solve_majority_vote(message, history)
|
| 101 |
+
else:
|
| 102 |
+
return solve_single(message, history)
|
| 103 |
+
|
| 104 |
+
# --- UI SETUP ---
|
| 105 |
+
demo = gr.ChatInterface(
|
| 106 |
+
fn=chat_logic,
|
| 107 |
+
additional_inputs=[
|
| 108 |
+
gr.Checkbox(label="🔥 Enable Smart Mode (Slow but 82% Accurate)", value=False)
|
| 109 |
+
],
|
| 110 |
+
title="🧮 AI Math Tutor (Qwen-1.5B Fine-Tuned)",
|
| 111 |
+
description="<b>Portfolio Project:</b> A specialized math solver fine-tuned on GSM8K using LoRA. <br>• <b>Standard Mode:</b> Fast (~70% Acc). <br>• <b>Smart Mode:</b> Uses Majority Voting to reach <b>82% Accuracy</b>.",
|
| 112 |
+
examples=[
|
| 113 |
+
["If I have 30 candies and eat 12, then buy 5 more, how many do I have?", False],
|
| 114 |
+
["It takes 5 machines 5 minutes to make 5 widgets. How long for 100 machines?", True]
|
| 115 |
+
],
|
| 116 |
+
theme="soft"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
demo.launch()
|