Instructions to use AnalyticsIntelligence/PIDGIN_gemma3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AnalyticsIntelligence/PIDGIN_gemma3 with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("AnalyticsIntelligence/PIDGIN_gemma3", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Desktop
Update handler.py
Browse files- handler.py +67 -99
handler.py
CHANGED
|
@@ -1,101 +1,69 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from
|
| 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 |
-
generated_text = self.tokenizer.decode(
|
| 67 |
-
outputs[0],
|
| 68 |
-
skip_special_tokens=True
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
# Remove the input prompt from output
|
| 72 |
-
if prompt in generated_text:
|
| 73 |
-
generated_text = generated_text.replace(prompt, "").strip()
|
| 74 |
-
|
| 75 |
-
# Return in appropriate format based on input type
|
| 76 |
-
if "messages" in data:
|
| 77 |
-
# OpenAI Chat Completion response format
|
| 78 |
-
return {
|
| 79 |
-
"id": "chatcmpl-pidgin",
|
| 80 |
-
"object": "chat.completion",
|
| 81 |
-
"created": int(torch.cuda.Event().record().elapsed_time(torch.cuda.Event())),
|
| 82 |
-
"model": "pidgin-gemma-3",
|
| 83 |
-
"choices": [{
|
| 84 |
-
"index": 0,
|
| 85 |
-
"message": {
|
| 86 |
-
"role": "assistant",
|
| 87 |
-
"content": generated_text
|
| 88 |
-
},
|
| 89 |
-
"finish_reason": "stop"
|
| 90 |
-
}],
|
| 91 |
-
"usage": {
|
| 92 |
-
"prompt_tokens": len(inputs["input_ids"][0]),
|
| 93 |
-
"completion_tokens": len(outputs[0]) - len(inputs["input_ids"][0]),
|
| 94 |
-
"total_tokens": len(outputs[0])
|
| 95 |
-
}
|
| 96 |
}
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
"generated_text": generated_text
|
| 101 |
-
}]
|
|
|
|
| 1 |
+
# handler.py
|
| 2 |
+
import json
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# ---- LOAD MODEL ON STARTUP ----
|
| 7 |
+
pipe = pipeline(
|
| 8 |
+
"text-generation",
|
| 9 |
+
model="Ephraimmm/PIDGIN_gemma-3",
|
| 10 |
+
device_map="auto"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# ---- UTIL: convert OpenAI-like body into prompt ----
|
| 15 |
+
def build_prompt(messages: List[Dict[str, str]]) -> str:
|
| 16 |
+
out = []
|
| 17 |
+
for m in messages:
|
| 18 |
+
role = m["role"]
|
| 19 |
+
content = m["content"]
|
| 20 |
+
if role == "system":
|
| 21 |
+
out.append(f"<<SYS>> {content}\n")
|
| 22 |
+
elif role == "user":
|
| 23 |
+
out.append(f"User: {content}")
|
| 24 |
+
elif role == "assistant":
|
| 25 |
+
out.append(f"Assistant: {content}")
|
| 26 |
+
out.append("Assistant:")
|
| 27 |
+
return "\n".join(out)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# ---- MAIN HANDLER ENTRYPOINT ----
|
| 31 |
+
def handle(event, context=None):
|
| 32 |
+
"""AWS/SageMaker/HF style single entry."""
|
| 33 |
+
if isinstance(event, str): # if raw body
|
| 34 |
+
event = json.loads(event)
|
| 35 |
+
|
| 36 |
+
# Expect OpenAI style body
|
| 37 |
+
messages = event["messages"]
|
| 38 |
+
max_tokens = event.get("max_tokens", 256)
|
| 39 |
+
temperature = event.get("temperature", 0.7)
|
| 40 |
+
top_p = event.get("top_p", 0.95)
|
| 41 |
+
|
| 42 |
+
prompt = build_prompt(messages)
|
| 43 |
+
|
| 44 |
+
out = pipe(
|
| 45 |
+
prompt,
|
| 46 |
+
max_new_tokens=max_tokens,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
temperature=temperature,
|
| 49 |
+
top_p=top_p,
|
| 50 |
+
eos_token_id=pipe.tokenizer.eos_token_id,
|
| 51 |
+
)[0]["generated_text"]
|
| 52 |
+
|
| 53 |
+
# extract only the assistant continuation
|
| 54 |
+
completion = out.replace(prompt, "").strip()
|
| 55 |
+
|
| 56 |
+
# ---- RETURN IN OPENAI CHAT FORMAT ----
|
| 57 |
+
resp = {
|
| 58 |
+
"id": "chatcmpl-local-1",
|
| 59 |
+
"object": "chat.completion",
|
| 60 |
+
"choices": [{
|
| 61 |
+
"index": 0,
|
| 62 |
+
"finish_reason": "stop",
|
| 63 |
+
"message": {
|
| 64 |
+
"role": "assistant",
|
| 65 |
+
"content": completion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
+
}],
|
| 68 |
+
}
|
| 69 |
+
return resp
|
|
|
|
|
|