prabalGaur commited on
Commit
bdcbcb8
·
verified ·
1 Parent(s): dff9603

Upload pricer_service.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pricer_service.py +67 -0
pricer_service.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import modal
2
+ from modal import Image
3
+
4
+ # Setup - define our infrastructure with code!
5
+
6
+ app = modal.App("pricer-service")
7
+ image = Image.debian_slim().pip_install(
8
+ "torch", "transformers", "bitsandbytes", "accelerate", "peft"
9
+ )
10
+
11
+ # This collects the secret from Modal.
12
+ # Depending on your Modal configuration, you may need to replace "huggingface-secret" with "hf-secret"
13
+ secrets = [modal.Secret.from_name("huggingface-secret")]
14
+
15
+ # Constants
16
+
17
+ GPU = "T4"
18
+ BASE_MODEL = "meta-llama/Llama-3.2-3B"
19
+ PROJECT_NAME = "price"
20
+ HF_USER = "ed-donner" # your HF name here! Or use mine if you just want to reproduce my results.
21
+ RUN_NAME = "2025-11-28_18.47.07"
22
+ PROJECT_RUN_NAME = f"{PROJECT_NAME}-{RUN_NAME}"
23
+ REVISION = "b19c8bfea3b6ff62237fbb0a8da9779fc12cefbd"
24
+ FINETUNED_MODEL = f"{HF_USER}/{PROJECT_RUN_NAME}"
25
+
26
+
27
+ @app.function(image=image, secrets=secrets, gpu=GPU, timeout=1800)
28
+ def price(description: str) -> float:
29
+ import re
30
+ import torch
31
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, set_seed
32
+ from peft import PeftModel
33
+
34
+ PREFIX = "Price is $"
35
+ QUESTION = "What does this cost to the nearest dollar?"
36
+
37
+ prompt = f"{QUESTION}\n\n{description}\n\n{PREFIX}"
38
+
39
+ # Quant Config
40
+ quant_config = BitsAndBytesConfig(
41
+ load_in_4bit=True,
42
+ bnb_4bit_use_double_quant=True,
43
+ bnb_4bit_compute_dtype=torch.float16,
44
+ bnb_4bit_quant_type="nf4",
45
+ )
46
+
47
+ # Load model and tokenizer
48
+
49
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
50
+ tokenizer.pad_token = tokenizer.eos_token
51
+ tokenizer.padding_side = "right"
52
+
53
+ base_model = AutoModelForCausalLM.from_pretrained(
54
+ BASE_MODEL, quantization_config=quant_config, device_map="auto"
55
+ )
56
+
57
+ fine_tuned_model = PeftModel.from_pretrained(base_model, FINETUNED_MODEL, revision=REVISION)
58
+
59
+ set_seed(42)
60
+ inputs = tokenizer.encode(prompt, return_tensors="pt").to("cuda")
61
+ with torch.no_grad():
62
+ outputs = fine_tuned_model.generate(inputs, max_new_tokens=5)
63
+ result = tokenizer.decode(outputs[0])
64
+ contents = result.split("Price is $")[1]
65
+ contents = contents.replace(",", "")
66
+ match = re.search(r"[-+]?\d*\.\d+|\d+", contents)
67
+ return float(match.group()) if match else 0