feat(week8): add ensemble-joshua assignment (API, Modal, agents); omit large RF model
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import modal
|
||||
from modal import App, Volume, Image
|
||||
|
||||
|
||||
app = modal.App("pricer-service")
|
||||
image = Image.debian_slim().pip_install("huggingface", "torch", "transformers", "bitsandbytes", "accelerate", "peft")
|
||||
|
||||
secrets = [modal.Secret.from_name("hf-secret")]
|
||||
|
||||
# Constants
|
||||
GPU = "T4"
|
||||
BASE_MODEL = "meta-llama/Meta-Llama-3.1-8B"
|
||||
PROJECT_NAME = "pricer"
|
||||
HF_USER = "ed-donner"
|
||||
RUN_NAME = "2024-09-13_13.04.39"
|
||||
PROJECT_RUN_NAME = f"{PROJECT_NAME}-{RUN_NAME}"
|
||||
REVISION = "e8d637df551603dc86cd7a1598a8f44af4d7ae36"
|
||||
FINETUNED_MODEL = f"{HF_USER}/{PROJECT_RUN_NAME}"
|
||||
CACHE_DIR = "/cache"
|
||||
|
||||
|
||||
MIN_CONTAINERS = 0
|
||||
|
||||
QUESTION = "How much does this cost to the nearest dollar?"
|
||||
PREFIX = "Price is $"
|
||||
|
||||
hf_cache_volume = Volume.from_name("hf-hub-cache", create_if_missing=True)
|
||||
|
||||
@app.cls(
|
||||
image=image.env({"HF_HUB_CACHE": CACHE_DIR}),
|
||||
secrets=secrets,
|
||||
gpu=GPU,
|
||||
timeout=1800,
|
||||
min_containers=MIN_CONTAINERS,
|
||||
volumes={CACHE_DIR: hf_cache_volume}
|
||||
)
|
||||
class Pricer:
|
||||
|
||||
@modal.enter()
|
||||
def setup(self):
|
||||
import torch
|
||||
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, set_seed
|
||||
from peft import PeftModel
|
||||
|
||||
# Quant Config
|
||||
quant_config = BitsAndBytesConfig(
|
||||
load_in_4bit=True,
|
||||
bnb_4bit_use_double_quant=True,
|
||||
bnb_4bit_compute_dtype=torch.bfloat16,
|
||||
bnb_4bit_quant_type="nf4"
|
||||
)
|
||||
|
||||
# Load model and tokenizer
|
||||
self.tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
||||
self.tokenizer.pad_token = self.tokenizer.eos_token
|
||||
self.tokenizer.padding_side = "right"
|
||||
self.base_model = AutoModelForCausalLM.from_pretrained(
|
||||
BASE_MODEL,
|
||||
quantization_config=quant_config,
|
||||
device_map="auto"
|
||||
)
|
||||
self.fine_tuned_model = PeftModel.from_pretrained(self.base_model, FINETUNED_MODEL, revision=REVISION)
|
||||
|
||||
@modal.method()
|
||||
def price(self, description: str) -> float:
|
||||
import os
|
||||
import re
|
||||
import torch
|
||||
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, set_seed
|
||||
from peft import PeftModel
|
||||
|
||||
set_seed(42)
|
||||
prompt = f"{QUESTION}\n\n{description}\n\n{PREFIX}"
|
||||
inputs = self.tokenizer.encode(prompt, return_tensors="pt").to("cuda")
|
||||
attention_mask = torch.ones(inputs.shape, device="cuda")
|
||||
outputs = self.fine_tuned_model.generate(inputs, attention_mask=attention_mask, max_new_tokens=5, num_return_sequences=1)
|
||||
result = self.tokenizer.decode(outputs[0])
|
||||
|
||||
contents = result.split("Price is $")[1]
|
||||
contents = contents.replace(',','')
|
||||
match = re.search(r"[-+]?\d*\.\d+|\d+", contents)
|
||||
return float(match.group()) if match else 0
|
||||
|
||||
|
||||
# Simple HTTP endpoint so external apps can call this on Modal
|
||||
@app.function(image=image, secrets=secrets, gpu=GPU, timeout=1800)
|
||||
@modal.web_endpoint(method="POST")
|
||||
def price_http(body: dict):
|
||||
"""HTTP endpoint: {"description": str} -> {"price": float}"""
|
||||
description = body.get("description", '').strip()
|
||||
if not description:
|
||||
return {"error": "Missing 'description'"}
|
||||
|
||||
pricer = Pricer()
|
||||
value = pricer.price.remote(description)
|
||||
return {"price": float(value)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user