File size: 17,810 Bytes
3762381
9ba1aa5
d59cbe6
4770ee5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac8aabc
1e124b5
4770ee5
1e124b5
 
 
4770ee5
ac8aabc
 
 
 
4770ee5
 
ac8aabc
 
d59cbe6
 
 
05adb3f
 
 
 
 
ac8aabc
d59cbe6
4770ee5
 
40202db
 
05adb3f
4770ee5
 
ac8aabc
 
 
 
9ba1aa5
4770ee5
ac8aabc
 
4770ee5
ac8aabc
40202db
ac8aabc
40202db
 
 
 
4770ee5
ac8aabc
 
3762381
4770ee5
40202db
 
ac8aabc
 
 
 
 
 
d59cbe6
 
4770ee5
ac8aabc
4770ee5
ac8aabc
 
 
 
 
4770ee5
 
 
ac8aabc
4770ee5
ac8aabc
4770ee5
 
05adb3f
4770ee5
 
 
 
 
 
 
ac8aabc
4770ee5
ac8aabc
 
 
4770ee5
ac8aabc
4770ee5
 
 
ac8aabc
4770ee5
ac8aabc
 
 
 
 
3762381
4770ee5
ac8aabc
4770ee5
52ad975
4770ee5
52ad975
4770ee5
05adb3f
 
 
 
52ad975
 
 
 
 
 
 
 
 
 
4770ee5
 
 
 
 
 
 
05adb3f
4770ee5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52ad975
 
4770ee5
52ad975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4770ee5
05adb3f
 
 
 
52ad975
 
 
05adb3f
 
52ad975
 
 
 
 
 
 
05adb3f
 
4770ee5
 
05adb3f
3762381
ac8aabc
05adb3f
3762381
 
05adb3f
 
 
 
 
40202db
05adb3f
3762381
 
4770ee5
05adb3f
3762381
 
4770ee5
 
ac8aabc
3762381
ac8aabc
4770ee5
05adb3f
4770ee5
 
 
 
05adb3f
4770ee5
05adb3f
4770ee5
 
05adb3f
4770ee5
 
 
 
 
 
 
 
 
 
40202db
4770ee5
 
 
 
40202db
4770ee5
 
 
 
40202db
 
4770ee5
52ad975
 
4770ee5
05adb3f
4770ee5
05adb3f
 
4770ee5
 
05adb3f
4770ee5
52ad975
 
4770ee5
52ad975
 
 
 
 
 
 
 
4770ee5
 
05adb3f
4770ee5
05adb3f
 
4770ee5
05adb3f
 
 
 
 
 
 
 
4770ee5
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#tope_version
# handler.py
# handler.py
# from typing import Any, Dict, List, Union, Optional
# import torch
# from huggingface_inference_toolkit.logging import logger
# from unsloth import FastLanguageModel
# from peft import PeftModel
 
# # OpenAI-style prompt:
# #   - str
# #   - [{"role": "...", "content": "..." | [{"type":"text","text":"..."}]}]
# Prompt = Union[str, List[Dict[str, Any]], Dict[str, Any]]
 
 
# def _to_block_list(content: Any) -> List[Dict[str, str]]:
#     """Ensure content is a list[{'type':'text','text':...}]."""
#     if content is None:
#         return [{"type": "text", "text": ""}]
#     if isinstance(content, list):
#         # Assume already blocks
#         return content
#     # Fallback: stringify
#     return [{"type": "text", "text": str(content)}]
 
 
# class EndpointHandler:
#     """
#     HF Inference Endpoint handler that mirrors your Gradio setup:
#     - Loads Unsloth 4-bit Gemma-3-4B IT
#     - Attaches LoRA adapter
#     - Uses tokenizer.apply_chat_template
#     - Always replies in Nigerian Pidgin
#     """
 
#     def __init__(self, model_dir: str, **_: Any):
#         logger.info(f"Initializing with model_dir={model_dir}")
 
#         base_model_name = "unsloth/gemma-3-4b-it-unsloth-bnb-4bit"
#         model, tokenizer = FastLanguageModel.from_pretrained(
#             model_name=base_model_name,
#             max_seq_length=2048,
#             dtype=torch.float16,
#             load_in_4bit=True,
#         )
 
#         lora_repo = "Ephraimmm/PIDGIN_gemma-3"
#         model = PeftModel.from_pretrained(model, lora_repo)
#         FastLanguageModel.for_inference(model)
 
#         self.model = model.eval()
#         self.tokenizer = tokenizer
#         self.device = getattr(
#             self.model,
#             "device",
#             torch.device("cuda" if torch.cuda.is_available() else "cpu"),
#         )
#         # Safety: some tokenizers lack pad_token_id
#         if getattr(self.tokenizer, "pad_token_id", None) is None:
#             self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
 
#         logger.info(f"Device: {self.device} | eos_id: {self.tokenizer.eos_token_id}")
 
#         self._system_text = (
#             "You are a Nigerian assistant that speaks PIDGIN ENGLISH. "
#             "When asked 'how far', reply 'I dey o, how you dey?'. "
#             "Always answer in Pidgin English."
#         )
 
#     def _normalize_messages(self, prompt: Prompt) -> List[Dict[str, Any]]:
#         """Return a list of messages with block-style contents only."""
#         # Accept single dict
#         if isinstance(prompt, dict):
#             prompt = [prompt]
 
#         # Accept raw string
#         if isinstance(prompt, str):
#             msgs = [
#                 {"role": "system", "content": _to_block_list(self._system_text)},
#                 {"role": "user", "content": _to_block_list(prompt)},
#             ]
#             return msgs
 
#         if not isinstance(prompt, list):
#             raise ValueError("`inputs` must be a string, a message dict, or a list of messages.")
 
#         # Normalize all contents to blocks
#         norm = []
#         for m in prompt:
#             role = m.get("role", "user")
#             content = _to_block_list(m.get("content", ""))
#             norm.append({"role": role, "content": content})
 
#         # Ensure a system message exists (prepend if missing)
#         if not any(m.get("role") == "system" for m in norm):
#             norm = [{"role": "system", "content": _to_block_list(self._system_text)}] + norm
 
#         return norm
 
#     def _build_model_inputs(self, prompt: Prompt):
#         messages = self._normalize_messages(prompt)
#         # Use the same path as your Gradio script
#         model_inputs = self.tokenizer.apply_chat_template(
#             messages,
#             add_generation_prompt=True,
#             return_tensors="pt",
#             tokenize=True,
#             return_dict=True,
#         ).to(self.device)
#         return model_inputs
 
#     def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
#         logger.info(f"Incoming keys: {list(data.keys())}")
#         if "inputs" not in data:
#             raise ValueError("Missing `inputs` in request body.")
 
#         prompt: Prompt = data["inputs"]
#         params: Dict[str, Any] = (data.get("parameters") or {})
 
#         # Defaults aligned with your Gradio code
#         max_new_tokens = int(params.get("max_new_tokens", 256))   # raise per request if needed
#         temperature = float(params.get("temperature", 0.1))
#         top_p = float(params.get("top_p", 1.0))
#         top_k: Optional[int] = params.get("top_k", None)
#         use_cache = bool(params.get("use_cache", False))
#         repetition_penalty = float(params.get("repetition_penalty", 1.0))
#         eos_token_id = params.get("eos_token_id", self.tokenizer.eos_token_id)
#         pad_token_id = params.get("pad_token_id", self.tokenizer.pad_token_id)
 
#         inputs = self._build_model_inputs(prompt)
 
#         gen_kwargs = dict(
#             **inputs,
#             max_new_tokens=max_new_tokens,
#             temperature=temperature,
#             top_p=top_p,
#             do_sample=True,                 # streaming-like sampling on
#             use_cache=use_cache,
#             repetition_penalty=repetition_penalty,
#             eos_token_id=eos_token_id,
#             pad_token_id=pad_token_id,
#         )
#         if top_k is not None:
#             gen_kwargs["top_k"] = int(top_k)
 
#         with torch.no_grad():
#             output_ids = self.model.generate(**gen_kwargs)
 
#         # Return ONLY the continuation (like your streamer)
#         input_len = inputs["input_ids"].shape[-1]
#         generated_ids = output_ids[0, input_len:]
#         reply_text = self.tokenizer.decode(generated_ids, skip_special_tokens=True)
 
#         return {
#             "reply": [reply_text],
#             "usage": {
#                 "prompt_tokens": int(input_len),
#                 "generated_tokens": int(generated_ids.shape[-1]),
#             },
#         }
 
 
# if __name__ == "__main__":
#     h = EndpointHandler(model_dir=".")
#     # Raw string
#     print(h({"inputs": "How far?", "parameters": {"max_new_tokens": 64}}))
#     # Block-style content (your Gradio shape)
#     print(h({
#         "inputs": [
#             {
#                 "role": "user",
#                 "content": [{"type": "text", "text": "Explain why sky dey blue."}]
#             }
#         ],
#         "parameters": {"max_new_tokens": 64}
#     }))


from typing import Any, Dict, List, Union, Optional
import torch
import time
from huggingface_inference_toolkit.logging import logger
from unsloth import FastLanguageModel
from peft import PeftModel

# OpenAI-style prompt:
#   - str
#   - [{"role": "...", "content": "..." | [{"type":"text","text":"..."}]}]
Prompt = Union[str, List[Dict[str, Any]], Dict[str, Any]]


def _to_block_list(content: Any) -> List[Dict[str, str]]:
    """Ensure content is a list[{'type':'text','text':...}]."""
    if content is None:
        return [{"type": "text", "text": ""}]
    if isinstance(content, list):
        # Check if it's already in block format
        if all(isinstance(item, dict) and "type" in item for item in content):
            return content
        # Otherwise treat as raw list, stringify
        return [{"type": "text", "text": str(content)}]
    # Fallback: stringify
    return [{"type": "text", "text": str(content)}]


class EndpointHandler:
    """
    HF Inference Endpoint handler that accepts pure OpenAI API format:
    - Accepts OpenAI-style requests with 'messages', 'model', etc.
    - Returns OpenAI-style responses with 'choices', 'usage', etc.
    - Loads Unsloth 4-bit Gemma-3-4B IT
    - Attaches LoRA adapter
    - Uses tokenizer.apply_chat_template
    - Always replies in Nigerian Pidgin
    """

    def __init__(self, model_dir: str, **_: Any):
        logger.info(f"Initializing with model_dir={model_dir}")

        base_model_name = "unsloth/gemma-3-4b-it-unsloth-bnb-4bit"
        model, tokenizer = FastLanguageModel.from_pretrained(
            model_name=base_model_name,
            max_seq_length=2048,
            dtype=torch.float16,
            load_in_4bit=True,
        )

        lora_repo = "Ephraimmm/PIDGIN_gemma-3"
        model = PeftModel.from_pretrained(model, lora_repo)
        FastLanguageModel.for_inference(model)

        self.model = model.eval()
        self.tokenizer = tokenizer
        self.device = getattr(
            self.model,
            "device",
            torch.device("cuda" if torch.cuda.is_available() else "cpu"),
        )
        # Safety: some tokenizers lack pad_token_id
        if getattr(self.tokenizer, "pad_token_id", None) is None:
            self.tokenizer.pad_token_id = self.tokenizer.eos_token_id

        logger.info(f"Device: {self.device} | eos_id: {self.tokenizer.eos_token_id}")

        self._system_text = (
            "You are a Nigerian assistant that speaks PIDGIN ENGLISH. "
            "When asked 'how far', reply 'I dey o, how you dey?'. "
            "Always answer in Pidgin English."
        )

    def _normalize_messages(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        """Normalize OpenAI-style messages to block-style contents."""
        norm = []
        for m in messages:
            role = m.get("role", "user")
            content = m.get("content", "")
            
            # Convert content to block list format for internal processing
            if isinstance(content, str):
                content = _to_block_list(content)
            elif isinstance(content, list):
                content = _to_block_list(content)
            else:
                content = _to_block_list(str(content))
            
            norm.append({"role": role, "content": content})

        # Ensure a system message exists (prepend if missing)
        if not any(m.get("role") == "system" for m in norm):
            norm = [{"role": "system", "content": _to_block_list(self._system_text)}] + norm

        return norm

    def _build_model_inputs(self, messages: List[Dict[str, Any]]):
        normalized_messages = self._normalize_messages(messages)
        model_inputs = self.tokenizer.apply_chat_template(
            normalized_messages,
            add_generation_prompt=True,
            return_tensors="pt",
            tokenize=True,
            return_dict=True,
        ).to(self.device)
        return model_inputs

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Process requests in either OpenAI or HF format and return OpenAI-style responses.
        
        Input format (OpenAI API - directly from client):
        {
            "model": "Ephraimmm/PIDGIN_gemma-3",
            "messages": [
                {"role": "user", "content": "What is deep learning?"}
            ],
            "temperature": 0.7,
            "max_tokens": 256
        }
        
        OR HF Inference Endpoint format (with inputs wrapper):
        {
            "inputs": {
                "model": "Ephraimmm/PIDGIN_gemma-3",
                "messages": [{"role": "user", "content": "What is deep learning?"}]
            }
        }
        
        Output format (OpenAI API):
        {
            "id": "chatcmpl-...",
            "object": "chat.completion",
            "created": 1234567890,
            "model": "Ephraimmm/PIDGIN_gemma-3",
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": "Response text"
                },
                "finish_reason": "stop"
            }],
            "usage": {
                "prompt_tokens": 10,
                "completion_tokens": 20,
                "total_tokens": 30
            }
        }
        """
        logger.info(f"Incoming request keys: {list(data.keys())}")
        logger.info(f"Full incoming data: {data}")
        
        # Handle different input formats
        # Case 1: HF sends {"inputs": "string"} - treat as user message
        if "inputs" in data and isinstance(data["inputs"], str):
            messages = [{"role": "user", "content": data["inputs"]}]
            model_name = "Ephraimmm/PIDGIN_gemma-3"
            params = data.get("parameters", {})
        # Case 2: HF sends {"inputs": {...OpenAI format...}}
        elif "inputs" in data and isinstance(data["inputs"], dict):
            openai_data = data["inputs"]
            # Preserve any parameters at root level
            if "parameters" in data:
                for key, value in data["parameters"].items():
                    if key not in openai_data:
                        openai_data[key] = value
            messages = openai_data.get("messages")
            model_name = openai_data.get("model", "Ephraimmm/PIDGIN_gemma-3")
            params = openai_data
        # Case 3: HF sends {"inputs": [messages array]}
        elif "inputs" in data and isinstance(data["inputs"], list):
            messages = data["inputs"]
            model_name = "Ephraimmm/PIDGIN_gemma-3"
            params = data.get("parameters", {})
        # Case 4: Direct OpenAI format {"messages": [...]}
        elif "messages" in data:
            messages = data["messages"]
            model_name = data.get("model", "Ephraimmm/PIDGIN_gemma-3")
            params = data
        else:
            raise ValueError("Missing required field: 'messages' or 'inputs'")
        
        # Validate messages
        if not messages:
            raise ValueError("'messages' cannot be empty")
        
        messages = data["messages"]
        if not isinstance(messages, list):
            raise ValueError("'messages' must be a list of message objects")
        
        messages = data["messages"]
        if not isinstance(messages, list):
            raise ValueError("'messages' must be a list of message objects")
        
        # Extract OpenAI-style parameters with defaults
        max_tokens = int(params.get("max_tokens", 256))
        temperature = float(params.get("temperature", 0.1))
        top_p = float(params.get("top_p", 1.0))
        top_k = params.get("top_k", None)
        stream = params.get("stream", False)  # Not implemented yet
        presence_penalty = params.get("presence_penalty", 0.0)
        frequency_penalty = params.get("frequency_penalty", 0.0)
        
        # Build model inputs
        inputs = self._build_model_inputs(messages)

        # Prepare generation parameters
        gen_kwargs = dict(
            **inputs,
            max_new_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            do_sample=temperature > 0,  # Use sampling if temperature > 0
            use_cache=True,
            repetition_penalty=1.0 + frequency_penalty,  # Map frequency_penalty
            eos_token_id=self.tokenizer.eos_token_id,
            pad_token_id=self.tokenizer.pad_token_id,
        )
        
        if top_k is not None:
            gen_kwargs["top_k"] = int(top_k)

        # Generate response
        with torch.no_grad():
            output_ids = self.model.generate(**gen_kwargs)

        # Extract only the generated tokens (exclude prompt)
        input_len = inputs["input_ids"].shape[-1]
        generated_ids = output_ids[0, input_len:]
        reply_text = self.tokenizer.decode(generated_ids, skip_special_tokens=True)

        # Calculate token usage
        prompt_tokens = int(input_len)
        completion_tokens = int(generated_ids.shape[-1])
        total_tokens = prompt_tokens + completion_tokens

        # Return OpenAI-compatible response
        response = {
            "id": f"chatcmpl-{int(time.time() * 1000)}",
            "object": "chat.completion",
            "created": int(time.time()),
            "model": model_name,
            "choices": [
                {
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": reply_text
                    },
                    "finish_reason": "stop"
                }
            ],
            "usage": {
                "prompt_tokens": prompt_tokens,
                "completion_tokens": completion_tokens,
                "total_tokens": total_tokens
            }
        }

        return response


if __name__ == "__main__":
    h = EndpointHandler(model_dir=".")
    
    # Test 1: OpenAI format (direct)
    print("=== Test 1: OpenAI Format (Direct) ===")
    response = h({
        "model": "Ephraimmm/PIDGIN_gemma-3",
        "messages": [
            {"role": "user", "content": "What is deep learning?"}
        ]
    })
    print(response)
    print()
    
    # Test 2: HF Inference Endpoint format (with inputs wrapper)
    print("=== Test 2: HF Format (With inputs wrapper) ===")
    response = h({
        "inputs": {
            "model": "Ephraimmm/PIDGIN_gemma-3",
            "messages": [
                {"role": "user", "content": "How far?"}
            ],
            "temperature": 0.7,
            "max_tokens": 64
        }
    })
    print(response)
    print()
    
    # Test 3: Multi-turn conversation
    print("=== Test 3: Multi-turn Conversation ===")
    response = h({
        "model": "Ephraimmm/PIDGIN_gemma-3",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Explain why the sky is blue"},
            {"role": "assistant", "content": "The sky dey blue because..."},
            {"role": "user", "content": "Tell me more"}
        ],
        "max_tokens": 100
    })
    print(response)