math tool
Browse filessetting up math tool
app.py
CHANGED
|
@@ -3,15 +3,24 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import DuckDuckGoSearchTool,HfApiModel,load_tool, CodeAgent,PythonInterpreterTool,VisitWebpageTool,
|
| 7 |
import hashlib
|
| 8 |
import json
|
| 9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
import torch
|
| 11 |
|
| 12 |
-
class
|
| 13 |
name = "math_model"
|
| 14 |
description = "Answers advanced math questions using a pretrained math model."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def __init__(self, model_id="Qwen/Qwen2.5-Math-7B"):
|
| 17 |
print(f"Loading math model: {model_id}")
|
|
@@ -22,8 +31,11 @@ class math_model_tool(BaseTool):
|
|
| 22 |
device_map="auto",
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def
|
| 27 |
print(f"[MathModelTool] Question: {question}")
|
| 28 |
inputs = self.tokenizer(question, return_tensors="pt").to(self.model.device)
|
| 29 |
with torch.no_grad():
|
|
@@ -45,7 +57,7 @@ print("Made dir")
|
|
| 45 |
web_search = DuckDuckGoSearchTool()
|
| 46 |
python_interpreter = PythonInterpreterTool()
|
| 47 |
visit_webpage_tool = VisitWebpageTool()
|
| 48 |
-
|
| 49 |
|
| 50 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 51 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import DuckDuckGoSearchTool,HfApiModel,load_tool, CodeAgent,PythonInterpreterTool,VisitWebpageTool,Tool
|
| 7 |
import hashlib
|
| 8 |
import json
|
| 9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
import torch
|
| 11 |
|
| 12 |
+
class ModelMathTool(Tool):
|
| 13 |
name = "math_model"
|
| 14 |
description = "Answers advanced math questions using a pretrained math model."
|
| 15 |
+
|
| 16 |
+
inputs = {
|
| 17 |
+
"problem": {
|
| 18 |
+
"type": "string",
|
| 19 |
+
"description": "Math problem to solve.",
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
output_type = "string"
|
| 24 |
|
| 25 |
def __init__(self, model_id="Qwen/Qwen2.5-Math-7B"):
|
| 26 |
print(f"Loading math model: {model_id}")
|
|
|
|
| 31 |
device_map="auto",
|
| 32 |
trust_remote_code=True
|
| 33 |
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
|
| 38 |
+
def forward(self, question: str) -> str:
|
| 39 |
print(f"[MathModelTool] Question: {question}")
|
| 40 |
inputs = self.tokenizer(question, return_tensors="pt").to(self.model.device)
|
| 41 |
with torch.no_grad():
|
|
|
|
| 57 |
web_search = DuckDuckGoSearchTool()
|
| 58 |
python_interpreter = PythonInterpreterTool()
|
| 59 |
visit_webpage_tool = VisitWebpageTool()
|
| 60 |
+
model_math_tool = ModelMathTool()
|
| 61 |
|
| 62 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 63 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|