Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,78 +8,247 @@ from io import BytesIO
|
|
| 8 |
import base64
|
| 9 |
import re
|
| 10 |
import os
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
# GPU
|
| 14 |
-
#
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
"""
|
| 18 |
try:
|
| 19 |
-
# First try CUDA (NVIDIA)
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
device = torch.device("cuda")
|
| 22 |
torch.backends.cudnn.benchmark = True
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Try MPS (Apple Silicon) - only check if CUDA not available
|
| 27 |
-
if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
|
| 28 |
device = torch.device("mps")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return device
|
| 37 |
-
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f"⚠️
|
| 40 |
-
return torch.device("cpu")
|
| 41 |
|
| 42 |
-
device =
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
# Model Loading
|
| 46 |
-
#
|
| 47 |
|
| 48 |
-
def
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
for attempt in range(max_retries):
|
| 53 |
try:
|
| 54 |
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-it")
|
| 55 |
-
|
| 56 |
-
# Dynamic precision based on device
|
| 57 |
-
torch_dtype = torch.float16 if device.type in ['cuda', 'mps'] else torch.float32
|
| 58 |
-
|
| 59 |
model = AutoModelForCausalLM.from_pretrained(
|
| 60 |
"google/gemma-2-2b-it",
|
| 61 |
torch_dtype=torch_dtype,
|
| 62 |
device_map="auto",
|
| 63 |
low_cpu_mem_usage=True
|
| 64 |
)
|
| 65 |
-
|
| 66 |
-
# Manual device movement if needed
|
| 67 |
-
if device.type == 'cuda' and model.device.type != 'cuda':
|
| 68 |
model = model.to(device)
|
| 69 |
-
|
| 70 |
-
print(f"Model loaded on {model.device}")
|
| 71 |
return model, tokenizer
|
| 72 |
-
|
| 73 |
except Exception as e:
|
| 74 |
-
|
| 75 |
-
if attempt == max_retries - 1:
|
| 76 |
raise
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import base64
|
| 9 |
import re
|
| 10 |
import os
|
| 11 |
+
from typing import Optional
|
| 12 |
|
| 13 |
+
# ======================
|
| 14 |
+
# GPU Optimization Setup
|
| 15 |
+
# ======================
|
| 16 |
|
| 17 |
+
def configure_hardware():
|
| 18 |
+
"""Aggressive GPU configuration with automatic fallback"""
|
| 19 |
try:
|
|
|
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
device = torch.device("cuda")
|
| 22 |
torch.backends.cudnn.benchmark = True
|
| 23 |
+
dtype = torch.float16
|
| 24 |
+
print("🚀 Using CUDA GPU acceleration")
|
| 25 |
+
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
|
|
|
|
|
|
|
| 26 |
device = torch.device("mps")
|
| 27 |
+
dtype = torch.float16
|
| 28 |
+
print("🍏 Using Apple MPS acceleration")
|
| 29 |
+
else:
|
| 30 |
+
device = torch.device("cpu")
|
| 31 |
+
torch.set_num_threads(os.cpu_count() or 4)
|
| 32 |
+
dtype = torch.float32
|
| 33 |
+
print("⚡ Using CPU with thread optimization")
|
| 34 |
+
return device, dtype
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
print(f"⚠️ Hardware config error: {e}, using CPU fallback")
|
| 37 |
+
return torch.device("cpu"), torch.float32
|
| 38 |
|
| 39 |
+
device, torch_dtype = configure_hardware()
|
| 40 |
|
| 41 |
+
# ======================
|
| 42 |
+
# Model Loading
|
| 43 |
+
# ======================
|
| 44 |
|
| 45 |
+
def load_models():
|
| 46 |
+
"""Load model with retries and automatic device placement"""
|
| 47 |
+
for attempt in range(3):
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-it")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
model = AutoModelForCausalLM.from_pretrained(
|
| 51 |
"google/gemma-2-2b-it",
|
| 52 |
torch_dtype=torch_dtype,
|
| 53 |
device_map="auto",
|
| 54 |
low_cpu_mem_usage=True
|
| 55 |
)
|
| 56 |
+
if model.device != device:
|
|
|
|
|
|
|
| 57 |
model = model.to(device)
|
| 58 |
+
print(f"✅ Model loaded on {model.device}")
|
|
|
|
| 59 |
return model, tokenizer
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
+
if attempt == 2:
|
|
|
|
| 62 |
raise
|
| 63 |
+
print(f"⚠️ Attempt {attempt+1} failed: {e}")
|
| 64 |
+
time.sleep(2)
|
| 65 |
+
|
| 66 |
+
model, tokenizer = load_models()
|
| 67 |
+
|
| 68 |
+
# ======================
|
| 69 |
+
# Core Processing
|
| 70 |
+
# ======================
|
| 71 |
+
|
| 72 |
+
def generate_plot(labels, values, title="Comparison"):
|
| 73 |
+
"""Generate and encode matplotlib plot"""
|
| 74 |
+
plt.figure(figsize=(8,4))
|
| 75 |
+
plt.bar(labels, values, color=['#4C72B0', '#DD8452'])
|
| 76 |
+
plt.title(title)
|
| 77 |
+
plt.grid(axis='y', linestyle='--', alpha=0.7)
|
| 78 |
+
buf = BytesIO()
|
| 79 |
+
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
| 80 |
+
buf.seek(0)
|
| 81 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
| 82 |
+
plt.close()
|
| 83 |
+
return img_str
|
| 84 |
+
|
| 85 |
+
def solve_known_problems(prompt: str) -> Optional[str]:
|
| 86 |
+
"""Predefined solutions for common problems"""
|
| 87 |
+
prompt_lower = prompt.lower()
|
| 88 |
+
numbers = [int(n) for n in re.findall(r'\d+', prompt)]
|
| 89 |
+
|
| 90 |
+
# 2+2 problem
|
| 91 |
+
if "2+2" in prompt_lower:
|
| 92 |
+
return """🔢 Step-by-Step Solution:
|
| 93 |
+
1. Start with first number: 2
|
| 94 |
+
2. Add second number: + 2
|
| 95 |
+
3. Combine the values: 2 + 2 = 4
|
| 96 |
+
|
| 97 |
+
✅ Final Answer: 4"""
|
| 98 |
+
|
| 99 |
+
# Shopping problem
|
| 100 |
+
if ("notebook" in prompt_lower and "pen" in prompt_lower and
|
| 101 |
+
len(numbers) >= 4 and any(w in prompt_lower for w in ["rs.", "$"])):
|
| 102 |
+
notebook_total = numbers[0] * numbers[2]
|
| 103 |
+
pen_total = numbers[1] * numbers[3]
|
| 104 |
+
total = notebook_total + pen_total
|
| 105 |
+
|
| 106 |
+
plot = generate_plot(
|
| 107 |
+
labels=['Notebooks', 'Pens'],
|
| 108 |
+
values=[notebook_total, pen_total],
|
| 109 |
+
title="Expense Breakdown"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
return f"""🛍️ Step-by-Step Solution:
|
| 113 |
+
1. Notebook cost: {numbers[0]} × Rs.{numbers[2]} = Rs.{notebook_total}
|
| 114 |
+
2. Pen cost: {numbers[1]} × Rs.{numbers[3]} = Rs.{pen_total}
|
| 115 |
+
3. Total expense: Rs.{notebook_total} + Rs.{pen_total} = Rs.{total}
|
| 116 |
+
|
| 117 |
+
💵 Total Amount Spent: Rs.{total}
|
| 118 |
+
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
# Sales comparison
|
| 122 |
+
if ("difference" in prompt_lower and "sales" in prompt_lower and
|
| 123 |
+
len(numbers) >= 2):
|
| 124 |
+
diff = numbers[0] - numbers[1]
|
| 125 |
+
plot = generate_plot(
|
| 126 |
+
labels=['Today', 'Yesterday'],
|
| 127 |
+
values=[numbers[0], numbers[1]],
|
| 128 |
+
title="Sales Comparison"
|
| 129 |
+
)
|
| 130 |
+
return f"""📊 Step-by-Step Solution:
|
| 131 |
+
1. Today's sales: {numbers[0]}
|
| 132 |
+
2. Yesterday's sales: {numbers[1]}
|
| 133 |
+
3. Difference: {numbers[0]} - {numbers[1]} = {diff}
|
| 134 |
+
|
| 135 |
+
📈 Sales Difference: {diff}
|
| 136 |
+
|
| 137 |
+
"""
|
| 138 |
+
|
| 139 |
+
# Complex numbers
|
| 140 |
+
if "z^2" in prompt and "complex" in prompt_lower:
|
| 141 |
+
return """🧮 Step-by-Step Solution:
|
| 142 |
+
1. Given equation: z² + 16 - 30i = 0
|
| 143 |
+
2. Rewrite: z² = -16 + 30i
|
| 144 |
+
3. Let z = a + bi → z² = (a²-b²) + (2ab)i
|
| 145 |
+
4. Equate components:
|
| 146 |
+
- Real part: a² - b² = -16
|
| 147 |
+
- Imaginary part: 2ab = 30 → ab = 15
|
| 148 |
+
5. Solve system:
|
| 149 |
+
b = 15/a → a² - (225/a²) = -16
|
| 150 |
+
Multiply by a²: a⁴ + 16a² - 225 = 0
|
| 151 |
+
6. Let x = a² → x² + 16x - 225 = 0
|
| 152 |
+
7. Quadratic formula: x = [-16 ± √(256 + 900)]/2
|
| 153 |
+
→ x = 9 or -25
|
| 154 |
+
8. Valid solution: a² = 9 → a = ±3
|
| 155 |
+
→ b = 15/3 = 5 or b = 15/-3 = -5
|
| 156 |
+
|
| 157 |
+
✅ Solutions: z = 3 + 5i or z = -3 - 5i"""
|
| 158 |
|
| 159 |
+
return None
|
| 160 |
|
| 161 |
+
def generate_response(prompt: str) -> str:
|
| 162 |
+
"""Generate step-by-step solution with performance tracking"""
|
| 163 |
+
start_time = time.time()
|
| 164 |
+
|
| 165 |
+
# First try predefined solutions
|
| 166 |
+
predefined = solve_known_problems(prompt)
|
| 167 |
+
if predefined:
|
| 168 |
+
gen_time = time.time() - start_time
|
| 169 |
+
return f"{predefined}\n\n⏱️ Generated in {gen_time:.3f} seconds"
|
| 170 |
+
|
| 171 |
+
# Generate with model
|
| 172 |
+
try:
|
| 173 |
+
formatted_prompt = f"""Provide a detailed, step-by-step solution to the following problem. Break down each part clearly and show all working.
|
| 174 |
+
|
| 175 |
+
Problem: {prompt}
|
| 176 |
+
|
| 177 |
+
Solution Steps:"""
|
| 178 |
+
|
| 179 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device)
|
| 180 |
+
|
| 181 |
+
outputs = model.generate(
|
| 182 |
+
**inputs,
|
| 183 |
+
max_new_tokens=1000,
|
| 184 |
+
temperature=0.3,
|
| 185 |
+
top_k=40,
|
| 186 |
+
top_p=0.9,
|
| 187 |
+
do_sample=True,
|
| 188 |
+
pad_token_id=tokenizer.eos_token_id
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 192 |
+
response = response.replace(formatted_prompt, "").strip()
|
| 193 |
+
|
| 194 |
+
if not response or len(response) < 20:
|
| 195 |
+
response = "Here's the step-by-step approach:\n1. Analyze the problem\n2. Break it into components\n3. Solve each part\n4. Combine results\n\n(Detailed steps could not be generated)"
|
| 196 |
+
|
| 197 |
+
gen_time = time.time() - start_time
|
| 198 |
+
return f"{response}\n\n⏱️ Generated in {gen_time:.3f} seconds"
|
| 199 |
+
|
| 200 |
+
except Exception as e:
|
| 201 |
+
return f"Error generating response: {str(e)}"
|
| 202 |
+
|
| 203 |
+
# ======================
|
| 204 |
+
# Gradio Interface
|
| 205 |
+
# ======================
|
| 206 |
+
|
| 207 |
+
examples = [
|
| 208 |
+
"What is 2+2? Explain step by step.",
|
| 209 |
+
"Sara bought 3 notebooks and two pens. Each notebook costs Rs.120 and each pen costs Rs.30. How much money did Sara spend in total?",
|
| 210 |
+
"Find the value of z in the equation z^2 + 16 - 30i = 0, where z is a complex number.",
|
| 211 |
+
"If today a company makes 2000 sales and yesterday it made 1455 sales, what is the difference between them?"
|
| 212 |
+
]
|
| 213 |
+
|
| 214 |
+
with gr.Blocks(title="Ultimate Problem Solver", theme=gr.themes.Soft()) as demo:
|
| 215 |
+
gr.Markdown("""# 🧠 Ultimate Step-by-Step Problem Solver
|
| 216 |
+
*Powered by Gemma-2B with GPU Acceleration*""")
|
| 217 |
+
|
| 218 |
+
with gr.Row():
|
| 219 |
+
input_prompt = gr.Textbox(
|
| 220 |
+
label="Enter your problem",
|
| 221 |
+
placeholder="Type your math, word problem, or equation here...",
|
| 222 |
+
lines=3,
|
| 223 |
+
max_lines=6
|
| 224 |
+
)
|
| 225 |
+
output_response = gr.Markdown(label="Detailed Solution")
|
| 226 |
+
|
| 227 |
+
with gr.Row():
|
| 228 |
+
submit_btn = gr.Button("Solve", variant="primary")
|
| 229 |
+
clear_btn = gr.Button("Clear")
|
| 230 |
+
|
| 231 |
+
gr.Examples(
|
| 232 |
+
examples=examples,
|
| 233 |
+
inputs=input_prompt,
|
| 234 |
+
label="Try these examples",
|
| 235 |
+
examples_per_page=2
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
submit_btn.click(
|
| 239 |
+
fn=generate_response,
|
| 240 |
+
inputs=input_prompt,
|
| 241 |
+
outputs=output_response,
|
| 242 |
+
api_name="solve"
|
| 243 |
+
)
|
| 244 |
+
clear_btn.click(
|
| 245 |
+
lambda: ("", ""),
|
| 246 |
+
outputs=[input_prompt, output_response]
|
| 247 |
+
)
|
| 248 |
|
| 249 |
+
if __name__ == "__main__":
|
| 250 |
+
demo.launch(
|
| 251 |
+
server_name="0.0.0.0",
|
| 252 |
+
server_port=7860,
|
| 253 |
+
share=False
|
| 254 |
+
)
|