Spaces:
Sleeping
Sleeping
Ankit19102004 commited on
Commit ·
b763074
1
Parent(s): d218d36
update
Browse files- honeypot_api.py +39 -3
honeypot_api.py
CHANGED
|
@@ -149,9 +149,9 @@ def generate_agent_reply(history):
|
|
| 149 |
"Keep replies to 1–2 sentences and end with a question.\n\n"
|
| 150 |
)
|
| 151 |
|
| 152 |
-
convo=""
|
| 153 |
for h in history[-6:]:
|
| 154 |
-
convo+=f"{h['sender']}: {h['text']}\n"
|
| 155 |
|
| 156 |
prompt = persona + convo + "user:"
|
| 157 |
|
|
@@ -167,7 +167,43 @@ def generate_agent_reply(history):
|
|
| 167 |
)
|
| 168 |
|
| 169 |
txt = agent_tokenizer.decode(out[0], skip_special_tokens=True)
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
def compute_engagement_score(session_id, last_agent_reply):
|
| 173 |
conv = conversation_store.get(session_id, [])
|
|
|
|
| 149 |
"Keep replies to 1–2 sentences and end with a question.\n\n"
|
| 150 |
)
|
| 151 |
|
| 152 |
+
convo = ""
|
| 153 |
for h in history[-6:]:
|
| 154 |
+
convo += f"{h['sender']}: {h['text']}\n"
|
| 155 |
|
| 156 |
prompt = persona + convo + "user:"
|
| 157 |
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
txt = agent_tokenizer.decode(out[0], skip_special_tokens=True)
|
| 170 |
+
reply = txt.split("user:")[-1].strip()
|
| 171 |
+
reply = reply.replace("\n", " ").strip()
|
| 172 |
+
|
| 173 |
+
if "password is not your default email" in reply.lower():
|
| 174 |
+
reply = ""
|
| 175 |
+
|
| 176 |
+
sentences = [s.strip() for s in re.split(r"(?<=[.!?])\s+", reply) if s.strip()]
|
| 177 |
+
unique_sentences = []
|
| 178 |
+
seen = set()
|
| 179 |
+
for s in sentences:
|
| 180 |
+
key = s.lower()
|
| 181 |
+
if key in seen:
|
| 182 |
+
continue
|
| 183 |
+
seen.add(key)
|
| 184 |
+
unique_sentences.append(s)
|
| 185 |
+
if len(unique_sentences) >= 2:
|
| 186 |
+
break
|
| 187 |
+
|
| 188 |
+
if not unique_sentences:
|
| 189 |
+
fallbacks = [
|
| 190 |
+
"I am a bit confused about this. What exactly do you want me to do?",
|
| 191 |
+
"I am worried about my account. What should I do now?",
|
| 192 |
+
"I am not sure I understand. Can you explain what I need to share?"
|
| 193 |
+
]
|
| 194 |
+
idx = len(history) % len(fallbacks)
|
| 195 |
+
reply = fallbacks[idx]
|
| 196 |
+
else:
|
| 197 |
+
reply = " ".join(unique_sentences)
|
| 198 |
+
|
| 199 |
+
for forbidden in ["scam", "fraud", "phishing", "honeypot"]:
|
| 200 |
+
reply = re.sub(forbidden, "", reply, flags=re.IGNORECASE)
|
| 201 |
+
reply = " ".join(reply.split())
|
| 202 |
+
|
| 203 |
+
if "?" not in reply:
|
| 204 |
+
reply = reply.rstrip(".!") + "?"
|
| 205 |
+
|
| 206 |
+
return reply
|
| 207 |
|
| 208 |
def compute_engagement_score(session_id, last_agent_reply):
|
| 209 |
conv = conversation_store.get(session_id, [])
|