Spaces:
Running
Running
Create Math_Wizard.py
Browse files- Math_Wizard.py +46 -0
Math_Wizard.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Load tokenizer and model
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-math-7b-instruct")
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-math-7b-instruct", torch_dtype=torch.bfloat16, device_map="cpu")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
from transformers import GenerationConfig
|
| 11 |
+
model.generation_config = GenerationConfig.from_pretrained("deepseek-ai/deepseek-math-7b-instruct")
|
| 12 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def solve_math_problem(questions):
|
| 16 |
+
if isinstance(questions, str): # If input is a single string
|
| 17 |
+
questions = [questions]
|
| 18 |
+
|
| 19 |
+
results = []
|
| 20 |
+
for question in questions:
|
| 21 |
+
messages = [{"role": "user", "content": question}]
|
| 22 |
+
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 23 |
+
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
|
| 24 |
+
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
| 25 |
+
results.append(result)
|
| 26 |
+
return results
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=solve_math_problem,
|
| 31 |
+
inputs="text",
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Math Wizard",
|
| 34 |
+
description="""
|
| 35 |
+
Welcome to the Math Wizard!
|
| 36 |
+
Ask any math question, and let the wizard guide you through the solution step-by-step.
|
| 37 |
+
""",
|
| 38 |
+
allow_flagging=True,
|
| 39 |
+
examples=[
|
| 40 |
+
["What is the integral of x^2?"],
|
| 41 |
+
["How do I solve a quadratic equation?"]
|
| 42 |
+
],
|
| 43 |
+
theme="compact"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
interface.launch()
|