Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,34 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import DuckDuckGoSearchTool,HfApiModel,load_tool, CodeAgent
|
| 7 |
from smolagents import CodeAgent
|
| 8 |
import hashlib
|
| 9 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
|
@@ -20,7 +44,8 @@ import os
|
|
| 20 |
os.makedirs("cache", exist_ok=True)
|
| 21 |
print("Made dir")
|
| 22 |
web_search = DuckDuckGoSearchTool()
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
# 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:
|
|
@@ -55,8 +80,8 @@ class BasicAgent:
|
|
| 55 |
print("BasicAgent initialized.")
|
| 56 |
self.agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[web_search],
|
| 59 |
-
max_steps=
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=3,
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import DuckDuckGoSearchTool,HfApiModel,load_tool, CodeAgent,PythonInterpreterTool,VisitWebpageTool,BaseTool
|
| 7 |
from smolagents import CodeAgent
|
| 8 |
import hashlib
|
| 9 |
import json
|
| 10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
class MathModelTool(BaseTool):
|
| 14 |
+
name = "math_model"
|
| 15 |
+
description = "Answers advanced math questions using a pretrained math model."
|
| 16 |
+
|
| 17 |
+
def __init__(self, model_id="Qwen/Qwen2.5-Math-7B"):
|
| 18 |
+
print(f"Loading math model: {model_id}")
|
| 19 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 20 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_id,
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
trust_remote_code=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def run(self, question: str) -> str:
|
| 28 |
+
print(f"[MathModelTool] Question: {question}")
|
| 29 |
+
inputs = self.tokenizer(question, return_tensors="pt").to(self.model.device)
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
output = self.model.generate(**inputs, max_new_tokens=256)
|
| 32 |
+
response = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
| 33 |
+
return response
|
| 34 |
|
| 35 |
|
| 36 |
# (Keep Constants as is)
|
|
|
|
| 44 |
os.makedirs("cache", exist_ok=True)
|
| 45 |
print("Made dir")
|
| 46 |
web_search = DuckDuckGoSearchTool()
|
| 47 |
+
python_interpreter = PythonInterpreterTool()
|
| 48 |
+
visit_webpage_tool = VisitWebpageTool()
|
| 49 |
|
| 50 |
|
| 51 |
# 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:
|
|
|
|
| 80 |
print("BasicAgent initialized.")
|
| 81 |
self.agent = CodeAgent(
|
| 82 |
model=model,
|
| 83 |
+
tools=[web_search,python_interpreter,visit_webpage_tool],
|
| 84 |
+
max_steps=2,
|
| 85 |
verbosity_level=1,
|
| 86 |
grammar=None,
|
| 87 |
planning_interval=3,
|