-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal_deploy.py
More file actions
132 lines (108 loc) · 4.29 KB
/
modal_deploy.py
File metadata and controls
132 lines (108 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import modal
from pathlib import Path
image = (
modal.Image.debian_slim()
.pip_install(
"torch",
"transformers",
"peft",
"bitsandbytes",
"accelerate",
"scipy",
"fastapi[standard]"
)
.add_local_dir("./sherlock-finetuned", remote_path="/root/sherlock-finetuned")
)
app = modal.App("sherlock-detective")
REMOTE_MODEL_DIR = "/root/sherlock-finetuned"
@app.cls(
image=image,
gpu="T4",
timeout=600,
)
class SherlockModel:
@modal.enter()
def enter(self):
"""This runs once when the container starts"""
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel
base_model_name = "Qwen/Qwen2.5-7B-Instruct"
print("Loading tokenizer...")
self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
print("Loading base model (4-bit)...")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True,
)
base_model = AutoModelForCausalLM.from_pretrained(
base_model_name,
quantization_config=bnb_config,
device_map="auto",
torch_dtype="auto"
)
print(f"Loading Sherlock adapter from {REMOTE_MODEL_DIR}...")
self.model = PeftModel.from_pretrained(base_model, REMOTE_MODEL_DIR)
self.model.eval()
print("Sherlock is ready!")
@modal.fastapi_endpoint(method="POST")
def generate_web(self, data: dict):
"""Web endpoint for Streamlit - uses plain dialogue format matching training"""
prompt = data.get("prompt", "")
if not prompt:
return {"error": "No prompt provided"}
# Get conversation history if provided (for multi-turn support)
history = data.get("history", [])
import torch
# Build conversation context from history (matches training format)
context = ""
for turn in history:
if "human" in turn and "sherlock" in turn:
context += f"Human: {turn['human']}\nSherlock Holmes: {turn['sherlock']}\n\n"
# Use plain dialogue format (matching training data format)
input_text = f"{context}Human: {prompt}\nSherlock Holmes:"
inputs = self.tokenizer(input_text, return_tensors="pt").to(self.model.device)
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
do_sample=True,
pad_token_id=self.tokenizer.eos_token_id
)
response = self.tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
# Stop at next "Human:" if model keeps generating
response = response.split("Human:")[0].strip()
return {"response": response}
@modal.method()
def generate(self, prompt: str):
"""Remote method - uses plain dialogue format matching training"""
import torch
# Use plain dialogue format (matching training data format)
input_text = f"Human: {prompt}\nSherlock Holmes:"
inputs = self.tokenizer(input_text, return_tensors="pt").to(self.model.device)
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=256,
temperature=0.7,
top_p=0.9,
do_sample=True,
pad_token_id=self.tokenizer.eos_token_id
)
response = self.tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
# Stop at next "Human:" if model keeps generating
response = response.split("Human:")[0].strip()
return response
# 4. Local entrypoint to test it
@app.local_entrypoint()
def main(prompt: str = "Watson, what do you make of this?"):
print(f"Sending prompt to Modal: {prompt}")
model = SherlockModel()
response = model.generate.remote(prompt)
print(f"\nSherlock (from Cloud): {response}")