Update handler.py
Browse files- handler.py +41 -51
handler.py
CHANGED
|
@@ -1,52 +1,42 @@
|
|
| 1 |
-
from ctransformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import json
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def
|
| 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 |
-
return
|
| 45 |
-
|
| 46 |
-
def postprocess(self, data):
|
| 47 |
-
return data
|
| 48 |
-
|
| 49 |
-
def get_handler(model_dir):
|
| 50 |
-
handler = EndpointHandler(model_dir)
|
| 51 |
-
handler.load_model()
|
| 52 |
-
return handler
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict, List, Any
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
import gemma_tools as gem
|
| 6 |
+
|
| 7 |
+
MAX_TOKENS = 512
|
| 8 |
+
|
| 9 |
+
class EndpointHandler():
|
| 10 |
+
def __init__(self, data):
|
| 11 |
+
# Update the model path and filename with your ComicBot model
|
| 12 |
+
self.model = Llama.from_pretrained("njwright92/ComicBot_v.2-gguf", filename="ComicBot_v.2-q4_k_m.gguf", n_ctx=8192)
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 15 |
+
args = gem.get_args_or_none(data)
|
| 16 |
+
fmat = "<startofturn>system\n{system_prompt} <endofturn>\n<startofturn>user\n{prompt} <endofturn>\n<startofturn>model"
|
| 17 |
+
print(args, fmat)
|
| 18 |
+
if not args[0]:
|
| 19 |
+
return {
|
| 20 |
+
"status": args["status"],
|
| 21 |
+
"message": args["description"]
|
| 22 |
+
}
|
| 23 |
+
try:
|
| 24 |
+
fmat = fmat.format(system_prompt=args["system_prompt"], prompt=args["inputs"])
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return json.dumps({
|
| 27 |
+
"status": "error",
|
| 28 |
+
"reason": "invalid format"
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
max_length = data.pop("max_length", 512)
|
| 32 |
+
try:
|
| 33 |
+
max_length = int(max_length)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return json.dumps({
|
| 36 |
+
"status": "error",
|
| 37 |
+
"reason": "max_length was passed as something that was absolutely not a plain old int"
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
res = self.model(fmat, temperature=args["temperature"], top_p=args["top_p"], top_k=args["top_k"], max_tokens=max_length)
|
| 41 |
+
|
| 42 |
+
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|