Update handler.py
Browse files- handler.py +19 -16
handler.py
CHANGED
|
@@ -16,50 +16,53 @@ class EndpointHandler:
|
|
| 16 |
)
|
| 17 |
|
| 18 |
self.generation_config = {
|
| 19 |
-
"max_new_tokens":
|
| 20 |
"temperature": 0.7,
|
| 21 |
"top_p": 0.9,
|
| 22 |
"do_sample": True,
|
| 23 |
"pad_token_id": self.tokenizer.eos_token_id,
|
| 24 |
"eos_token_id": self.tokenizer.eos_token_id,
|
| 25 |
-
"repetition_penalty": 1.
|
| 26 |
}
|
| 27 |
|
| 28 |
-
def __call__(self, data: Dict[str, Any]) -> Any:
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Prompt bauen
|
| 38 |
-
prompt = """
|
| 39 |
Govori samo bosanski/srpski/hrvatski.
|
| 40 |
Budi direktan, ljubazan i prodajno jak.
|
| 41 |
Bez dugih priča. Slušaj kupca i zatvaraj prodaju.
|
| 42 |
|
| 43 |
"""
|
| 44 |
|
| 45 |
-
for msg in messages:
|
| 46 |
role = "Kupac" if msg.get("role") == "user" else "Agent"
|
| 47 |
prompt += f"{role}: {msg.get('content', '')}\n"
|
| 48 |
|
| 49 |
prompt += "Agent:"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=5000).to("cuda")
|
| 53 |
|
| 54 |
outputs = self.model.generate(**inputs, **self.generation_config)
|
| 55 |
|
| 56 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 57 |
|
| 58 |
-
# Nur den neuen Teil nehmen
|
| 59 |
if "Agent:" in response:
|
| 60 |
response = response.split("Agent:")[-1].strip()
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
except Exception:
|
| 65 |
-
return "Žao mi je, došlo je do greške.
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
self.generation_config = {
|
| 19 |
+
"max_new_tokens": 180,
|
| 20 |
"temperature": 0.7,
|
| 21 |
"top_p": 0.9,
|
| 22 |
"do_sample": True,
|
| 23 |
"pad_token_id": self.tokenizer.eos_token_id,
|
| 24 |
"eos_token_id": self.tokenizer.eos_token_id,
|
| 25 |
+
"repetition_penalty": 1.12
|
| 26 |
}
|
| 27 |
|
| 28 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 29 |
try:
|
| 30 |
+
# Flexible Input Verarbeitung (Playground + API)
|
| 31 |
+
raw_input = data.get("inputs") or data.get("messages") or data.get("input", "")
|
| 32 |
+
|
| 33 |
+
if isinstance(raw_input, str):
|
| 34 |
+
messages = [{"role": "user", "content": raw_input}]
|
| 35 |
+
elif isinstance(raw_input, list):
|
| 36 |
+
messages = raw_input
|
| 37 |
+
else:
|
| 38 |
+
messages = [{"role": "user", "content": str(raw_input)}]
|
| 39 |
|
| 40 |
# Prompt bauen
|
| 41 |
+
prompt = """Ti si profesionalni, brzi Telesales agent.
|
| 42 |
Govori samo bosanski/srpski/hrvatski.
|
| 43 |
Budi direktan, ljubazan i prodajno jak.
|
| 44 |
Bez dugih priča. Slušaj kupca i zatvaraj prodaju.
|
| 45 |
|
| 46 |
"""
|
| 47 |
|
| 48 |
+
for msg in messages[-10:]: # Sliding Window
|
| 49 |
role = "Kupac" if msg.get("role") == "user" else "Agent"
|
| 50 |
prompt += f"{role}: {msg.get('content', '')}\n"
|
| 51 |
|
| 52 |
prompt += "Agent:"
|
| 53 |
|
| 54 |
+
# Tokenisierung
|
| 55 |
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=5000).to("cuda")
|
| 56 |
|
| 57 |
outputs = self.model.generate(**inputs, **self.generation_config)
|
| 58 |
|
| 59 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
|
|
|
|
| 61 |
if "Agent:" in response:
|
| 62 |
response = response.split("Agent:")[-1].strip()
|
| 63 |
|
| 64 |
+
# Wichtiges Return-Format für HF Inference
|
| 65 |
+
return {"generated_text": response.strip()}
|
| 66 |
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return {"generated_text": f"Žao mi je, došlo je do greške. Ponovite molim."}
|