|

How to Build a T4-Friendly Autonomous Data Science Agent with DeepAnalyze-8B, Sandboxed Code Execution, and Iterative Analysis

📊

In this tutorial, we construct an autonomous information science agent round DeepAnalyze-8B and run it. We start by getting ready a steady runtime, putting in the required machine-learning dependencies, and loading the DeepAnalyze tokenizer and mannequin in 4-bit mode to preserve the workflow sensible on restricted GPU reminiscence. We then create a sandboxed execution setting that permits the mannequin to generate Python code, execute it safely, observe the outcomes, and proceed its evaluation in an agentic loop. By the top of the workflow, we give the agent a lifelike multi-file e-commerce workspace and let it clear, be part of, analyze, visualize, and summarize the info as a structured analyst-grade report.

Installing DeepAnalyze-8B Runtime Dependencies

import os, sys, subprocess
os.environ["MPLBACKEND"] = "Agg"
def _pip(*args):
   subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", *args])
_SETUP_FLAG = "/content material/.da_ready"
if not os.path.exists(_SETUP_FLAG):
   print("Installing dependencies (one-time). The runtime will RESTART; "
         "simply re-run this cell afterwards.n")
   _pip("-U", "transformers>=4.44", "speed up>=0.30", "bitsandbytes>=0.43")
   _pip("sentencepiece")
   _pip("openpyxl")
   _pip("--force-reinstall", "numpy==2.0.2")
   open(_SETUP_FLAG, "w").shut()
   print("nDependencies prepared. Restarting runtime now...")
   os.kill(os.getpid(), 9)

We begin by getting ready the Colab runtime with the required machine-learning dependencies for DeepAnalyze-8B. We set up the transformer, acceleration, quantization, tokenizer, and spreadsheet libraries with out disturbing the broader pocket book workflow. We additionally pin NumPy and restart the runtime as soon as to preserve the setting clear and steady for the subsequent execution.

Loading DeepAnalyze-8B in 4-Bit Mode

import re, io, glob, time, sign, contextlib, warnings, traceback
from threading import Thread
import numpy as np, pandas as pd
import torch
from transformers import (AutoModelForCausalLM, AutoTokenizer,
                         BitsAndBytesConfig, Textual contentIteratorStreamer)
warnings.filterwarnings("ignore")
MODEL_ID   = "RUC-DataLab/DeepAnalyze-8B"
USE_4BIT   = True
COMPUTE_DT = torch.float16
assert torch.cuda.is_available(), (
   "No GPU detected. In Colab: Runtime -> Change runtime kind -> GPU.")
print("GPU:", torch.cuda.get_device_name(0), "| NumPy:", np.__version__)
print("nLoading tokenizer & mannequin (first run downloads ~16GB, be affected person)...")
tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
if tok.pad_token_id is None:
   tok.pad_token = tok.eos_token
bnb = BitsAndBytesConfig(
   load_in_4bit=True, bnb_4bit_quant_type="nf4",
   bnb_4bit_compute_dtype=COMPUTE_DT, bnb_4bit_use_double_quant=True,
) if USE_4BIT else None
mannequin = AutoModelForCausalLM.from_pretrained(
   MODEL_ID, quantization_config=bnb, device_map="auto",
   torch_dtype=COMPUTE_DT, low_cpu_mem_usage=True, trust_remote_code=True,
)
mannequin.eval()
print("Model loaded. VRAM used: %.1f GB" % (torch.cuda.memory_allocated()/1e9))

We import the primary libraries, configure the DeepAnalyze-8B mannequin, and confirm that a GPU is on the market in Colab. We load the tokenizer and put together 4-bit quantization so the mannequin can match extra comfortably on a T4 GPU. We then load the mannequin in analysis mode and verify GPU reminiscence utilization earlier than transferring on to the agent logic.

Building the Sandboxed Code Executor

class CodeSandbox:
   def __init__(self, timeout=120, max_chars=6000):
       self.ns = {"__name__": "__main__"}
       self.timeout, self.max_chars = timeout, max_chars
   def _run(self, code):
       with contextlib.redirect_stdout(io.StringIO()) as out, 
            contextlib.redirect_stderr(io.StringIO()) as err:
           exec(compile(code, "<cell>", "exec"), self.ns)
       return out.getvalue() + err.getvalue()
   def execute(self, code):
       def _handler(signum, body):
           increase TimeoutError(f"Execution exceeded {self.timeout}s")
       prev = sign.sign(sign.SIGALRM, _handler)
       sign.alarm(self.timeout)
       strive:
           out = self._run(code)
           outcome = out if out.strip() else "[Executed successfully, no stdout]"
       besides Exception as e:
           tb = traceback.format_exc().splitlines()
           loc = subsequent((l.strip() for l in tb if '"<cell>"' in l), "")
           outcome = f"[Error]n{loc}n{kind(e).__name__}: {e}".strip()
       lastly:
           sign.alarm(0)
           sign.sign(sign.SIGALRM, prev)
       if len(outcome) > self.max_chars:
           outcome = outcome[:self.max_chars] + "n...[output truncated]..."
       return outcome

We outline a sandboxed code executor that offers the agent a persistent Python namespace for operating generated code. We seize commonplace output and error streams so that each execution outcome may be handed again into the reasoning loop. We additionally implement a timeout and truncate lengthy outputs to preserve the autonomous workflow managed and readable.

Implementing the DeepAnalyze Agentic Loop

class DeepAnalyzeAgent:
   def __init__(self, mannequin, tok, temperature=0.5, top_p=0.95):
       self.mannequin, self.tok = mannequin, tok
       self.temperature, self.top_p = temperature, top_p
   def _stream_generate(self, context, max_new_tokens):
       inputs = self.tok(context, return_tensors="pt",
                         add_special_tokens=False).to(self.mannequin.gadget)
       streamer = Textual contentIteratorStreamer(self.tok, skip_prompt=True,
                                       skip_special_tokens=False)
       kwargs = dict(
           **inputs, max_new_tokens=max_new_tokens, do_sample=True,
           temperature=self.temperature, top_p=self.top_p,
           stop_strings=["</Code>"], tokenizer=self.tok, streamer=streamer,
           pad_token_id=self.tok.pad_token_id, eos_token_id=self.tok.eos_token_id,
       )
       Thread(goal=self.mannequin.generate, kwargs=kwargs).begin()
       items = []
       for chunk in streamer:
           items.append(chunk); print(chunk, finish="", flush=True)
       return "".be part of(items)
   @staticmethod
   def _extract_code(delta):
       if "<Code>" in delta and "</Code>" not in delta:
           delta += "</Code>"
       m = re.search(r"<Code>(.*?)</Code>", delta, re.DOTALL)
       if not m:
           return None
       code = m.group(1).strip()
       fenced = re.search(r"```(?:python)?(.*?)```", code, re.DOTALL)
       return (fenced.group(1) if fenced else code).strip()
   def run(self, instruction, workspace, max_rounds=12,
           max_new_tokens=3072, exec_timeout=120):
       immediate = build_prompt(instruction, workspace)
       prefix = self.tok.apply_chat_template(
           [{"role": "user", "content": prompt}],
           tokenize=False, add_generation_prompt=True)
       sandbox = CodeSandbox(timeout=exec_timeout)
       full, hint = prefix, []
       cwd0 = os.getcwd(); os.chdir(workspace)
       strive:
           for r in vary(max_rounds):
               print(f"nn{'='*70}n ROUND {r+1}n{'='*70}")
               delta = self._stream_generate(full, max_new_tokens)
               full += delta
               hint.append(("mannequin", delta))
               if "<Answer>" in delta:
                   print("nn[Agent finished: <Answer> produced]"); break
               code = self._extract_code(delta)
               if code is None:
                   print("nn[Agent stopped: no further action]"); break
               output = sandbox.execute(code)
               print(f"nn--- <Execute> ---n{output}n--- </Execute> ---")
               full += f"n<Execute>n{output}n</Execute>n"
               hint.append(("execute", output))
           else:
               print(f"nn[Reached max_rounds={max_rounds}]")
       lastly:
           os.chdir(cwd0)
       reply = ""
       if "<Answer>" in full:
           reply = full.break up("<Answer>")[-1]
           reply = re.sub(r"</?Answer>", "", reply)
           reply = re.sub(r"<[||][^>]*?[||]>", "", reply).strip()
       return {"full": full, "hint": hint, "reply": reply}

We implement the DeepAnalyze agent loop, which streams mannequin outputs, extracts the generated code, and executes it step-by-step. We permit the mannequin to alternate between reasoning, coding, execution suggestions, and ultimate answering by particular motion tags. We preserve the complete dialog hint so the agent can refine its evaluation based mostly on earlier outputs and execution outcomes.

Running the E-Commerce Analysis Workspace

def _hsize(nbytes):
   for u in ["B", "KB", "MB", "GB"]:
       if nbytes < 1024: return f"{nbytes:.1f}{u}"
       nbytes /= 1024
   return f"{nbytes:.1f}TB"
def build_prompt(instruction, workspace):
   exts = (".csv", ".xlsx", ".xls", ".json", ".xml", ".yaml", ".yml",
           ".txt", ".md", ".tsv", ".db", ".sqlite")
   information = sorted(f for f in os.listdir(workspace) if f.decrease().endswith(exts))
   traces = [f'File {i+1}: {{"name": "{f}", "size": "'
            f'{_hsize(os.path.getsize(os.path.join(workspace, f)))}"}}'
            for i, f in enumerate(files)]
   return f"# Instructionn{instruction}nn# Datan" + "n".be part of(traces)
WORKSPACE = "/content material/da_workspace"
os.makedirs(WORKSPACE, exist_ok=True)
rng = np.random.default_rng(42); N = 2500
classes = ["Electronics", "Home", "Fashion", "Books", "Toys"]
dates = pd.to_datetime("2024-01-01") + pd.to_timedelta(rng.integers(0, 365, N), unit="D")
tx = pd.DataBody({
   "order_id": np.arange(100000, 100000 + N), "date": dates,
   "customer_id": rng.integers(1, 601, N),
   "class": rng.selection(classes, N, p=[.3, .2, .25, .15, .1]),
   "area": rng.selection(["North", "South", "East", "West"], N),
   "amount": rng.integers(1, 6, N),
   "unit_price": np.spherical(rng.gamma(3, 12, N) + 5, 2),
   "low cost": np.spherical(rng.selection([0, .05, .1, .15, .2], N), 2),
})
tx["revenue"] = np.spherical(tx.amount * tx.unit_price * (1 - tx.low cost), 2)
tx.loc[rng.choice(N, 60, replace=False), "unit_price"] = np.nan
tx.to_csv(f"{WORKSPACE}/transactions.csv", index=False)
pd.DataBody({
   "customer_id": np.arange(1, 601),
   "signup_year": rng.selection([2021, 2022, 2023, 2024], 600),
   "section": rng.selection(["Consumer", "Corporate", "Home Office"], 600),
   "age": rng.integers(18, 70, 600),
}).to_excel(f"{WORKSPACE}/clients.xlsx", index=False)
print("Workspace information:", os.listdir(WORKSPACE))
INSTRUCTION = (
   "Perform an end-to-end evaluation of this e-commerce dataset. Explore and "
   "be part of the information, clear any data-quality points, analyze income tendencies over "
   "time, by area, class, and buyer section, and determine the important thing "
   "drivers of income. Create no less than one clear visualization and SAVE it as "
   "a PNG file within the present listing. Finish with a concise, well-structured "
   "analyst-grade report of your findings and 2-3 actionable suggestions."
)
existing_imgs = set(glob.glob(f"{WORKSPACE}/*.png"))
agent = DeepAnalyzeAgent(mannequin, tok, temperature=0.5)
t0 = time.time()
outcome = agent.run(INSTRUCTION, WORKSPACE, max_rounds=12,
                  max_new_tokens=3072, exec_timeout=120)
print(f"nnDone in {time.time()-t0:.0f}s | "
     f"{sum(1 for okay,_ in outcome['trace'] if okay=='execute')} code executions")
strive:
   from IPython.show import show, Markdown, Image
   if outcome["answer"]:
       show(Markdown("## 📊 Final Reportnn" + outcome["answer"]))
   else:
       print("No <Answer> block produced (strive elevating max_rounds).")
   for img in sorted(set(glob.glob(f"{WORKSPACE}/*.png")) - existing_imgs):
       print("Figure:", img); show(Image(filename=img))
besides Exception:
   print("n===== FINAL REPORT =====n", outcome["answer"])

We create the immediate builder, put together a pattern e-commerce workspace, and generate transaction and buyer information for evaluation. We give the agent a full analytical instruction that asks it to clear, be part of, discover, visualize, and summarize the dataset. We lastly run the agent, show its ultimate report, and render any saved PNG figures produced in the course of the autonomous evaluation.

Conclusion

In conclusion, we noticed how DeepAnalyze-8B can be utilized as greater than a easy text-generation mannequin: we flip it into an iterative data-analysis agent that causes over information, writes executable code, inspects outputs, and produces ultimate insights. We preserve the workflow light-weight whereas nonetheless preserving the core agentic sample of understanding the duty, producing code, executing it, and refining the evaluation based mostly on actual outcomes. It supplies us with a basis for constructing autonomous data-science notebooks by which the mannequin not solely describes an evaluation but additionally actively performs it and returns each visible outputs and a concise ultimate report.


Check out the FULL CODES with NOTEBOOK. Also, be at liberty to observe us on Twitter and don’t overlook to be part of our 150k+ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.

Need to associate with us for selling your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar and so forth.? Connect with us

The publish How to Build a T4-Friendly Autonomous Data Science Agent with DeepAnalyze-8B, Sandboxed Code Execution, and Iterative Analysis appeared first on MarkTechPost.

Similar Posts