Spaces:
Sleeping
Sleeping
updated app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import re
|
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
| 8 |
# REPLACE WITH YOUR USERNAME
|
| 9 |
-
MODEL_ID = "
|
| 10 |
|
| 11 |
print(f"⏳ Loading {MODEL_ID}... (CPU Mode)")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
@@ -19,6 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 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,})"
|
|
@@ -35,13 +36,27 @@ You are a patient and friendly math teacher.
|
|
| 35 |
|
| 36 |
# Add History (Short Term Memory - Last 1 Turn)
|
| 37 |
history_context = ""
|
|
|
|
|
|
|
| 38 |
if len(history) > 0:
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"""
|
|
@@ -53,21 +68,24 @@ You are a patient and friendly math teacher.
|
|
| 53 |
|
| 54 |
def solve_single(question, history, temperature=0.6):
|
| 55 |
"""Standard generation."""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
def solve_majority_vote(question, history):
|
| 73 |
"""Smart Mode: Generates 3 answers and votes."""
|
|
@@ -101,7 +119,6 @@ def chat_logic(message, history, smart_mode):
|
|
| 101 |
else:
|
| 102 |
return solve_single(message, history)
|
| 103 |
|
| 104 |
-
# --- UI SETUP ---
|
| 105 |
# --- UI SETUP ---
|
| 106 |
demo = gr.ChatInterface(
|
| 107 |
fn=chat_logic,
|
|
@@ -112,18 +129,17 @@ demo = gr.ChatInterface(
|
|
| 112 |
description="""
|
| 113 |
<b>Portfolio Project:</b> A specialized math solver fine-tuned on GSM8K using LoRA.
|
| 114 |
<br><br>
|
| 115 |
-
<b
|
| 116 |
<ul>
|
| 117 |
-
<li><b>
|
| 118 |
-
<li><b>
|
|
|
|
| 119 |
</ul>
|
| 120 |
""",
|
| 121 |
examples=[
|
| 122 |
["If I have 30 candies and eat 12, then buy 5 more, how many do I have?", False],
|
| 123 |
-
["It takes 5 machines 5 minutes to make 5 widgets. How long for 100 machines?", True]
|
| 124 |
-
["Solve the integral solution for x + y + z = 15", True]
|
| 125 |
]
|
| 126 |
-
# theme="soft" <-- THIS LINE WAS REMOVED
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
|
|
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
| 8 |
# REPLACE WITH YOUR USERNAME
|
| 9 |
+
MODEL_ID = "Hariharan123/Qwen2.5-Math-1.5B-Solver"
|
| 10 |
|
| 11 |
print(f"⏳ Loading {MODEL_ID}... (CPU Mode)")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
| 19 |
# --- HELPER FUNCTIONS ---
|
| 20 |
def extract_answer(text):
|
| 21 |
"""Extracts the number after #### or the last number found."""
|
| 22 |
+
if not text: return None
|
| 23 |
if "####" in text:
|
| 24 |
text = text.split("####")[-1]
|
| 25 |
pattern = r"(-?[$0-9.,]{1,})"
|
|
|
|
| 36 |
|
| 37 |
# Add History (Short Term Memory - Last 1 Turn)
|
| 38 |
history_context = ""
|
| 39 |
+
|
| 40 |
+
# --- ROBUST HISTORY CHECK (The Fix) ---
|
| 41 |
if len(history) > 0:
|
| 42 |
+
try:
|
| 43 |
+
# Get the last interaction
|
| 44 |
+
last_turn = history[-1]
|
| 45 |
+
|
| 46 |
+
# Ensure it's a list/tuple
|
| 47 |
+
if isinstance(last_turn, (list, tuple)):
|
| 48 |
+
# Take only the first 2 elements (User, AI) and ignore extra metadata
|
| 49 |
+
last_q = last_turn[0]
|
| 50 |
+
last_a = last_turn[1]
|
| 51 |
+
|
| 52 |
+
history_context = f"""
|
| 53 |
<|im_start|>user
|
| 54 |
{last_q}<|im_end|>
|
| 55 |
<|im_start|>assistant
|
| 56 |
{last_a}<|im_end|>"""
|
| 57 |
+
except Exception:
|
| 58 |
+
# If history format is weird, just ignore it and continue safely
|
| 59 |
+
pass
|
| 60 |
|
| 61 |
# Current Input
|
| 62 |
user_input = f"""
|
|
|
|
| 68 |
|
| 69 |
def solve_single(question, history, temperature=0.6):
|
| 70 |
"""Standard generation."""
|
| 71 |
+
try:
|
| 72 |
+
prompt = format_prompt(question, history)
|
| 73 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 74 |
+
|
| 75 |
+
with torch.no_grad():
|
| 76 |
+
outputs = model.generate(
|
| 77 |
+
**inputs,
|
| 78 |
+
max_new_tokens=512,
|
| 79 |
+
temperature=temperature,
|
| 80 |
+
do_sample=True
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 84 |
+
if "<|im_start|>assistant" in response:
|
| 85 |
+
return response.split("<|im_start|>assistant")[-1].strip()
|
| 86 |
+
return response
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return f"Error generating response: {str(e)}"
|
| 89 |
|
| 90 |
def solve_majority_vote(question, history):
|
| 91 |
"""Smart Mode: Generates 3 answers and votes."""
|
|
|
|
| 119 |
else:
|
| 120 |
return solve_single(message, history)
|
| 121 |
|
|
|
|
| 122 |
# --- UI SETUP ---
|
| 123 |
demo = gr.ChatInterface(
|
| 124 |
fn=chat_logic,
|
|
|
|
| 129 |
description="""
|
| 130 |
<b>Portfolio Project:</b> A specialized math solver fine-tuned on GSM8K using LoRA.
|
| 131 |
<br><br>
|
| 132 |
+
<b>⚠️ Performance Note:</b> This demo runs on <b>Free CPU Tier</b>.
|
| 133 |
<ul>
|
| 134 |
+
<li><b>First Request:</b> May take 1-2 mins (Cold Start).</li>
|
| 135 |
+
<li><b>Warm Requests:</b> ~10-20 seconds.</li>
|
| 136 |
+
<li><b>Smart Mode:</b> Runs 3x slower for higher accuracy.</li>
|
| 137 |
</ul>
|
| 138 |
""",
|
| 139 |
examples=[
|
| 140 |
["If I have 30 candies and eat 12, then buy 5 more, how many do I have?", False],
|
| 141 |
+
["It takes 5 machines 5 minutes to make 5 widgets. How long for 100 machines?", True]
|
|
|
|
| 142 |
]
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|