Spaces:
Sleeping
Sleeping
Create coach.py
Browse files- engine/coach.py +59 -0
engine/coach.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from engine.utils import safe_log
|
| 4 |
+
|
| 5 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
+
|
| 7 |
+
coach_gen = pipeline(
|
| 8 |
+
"text-generation",
|
| 9 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 10 |
+
token=HF_TOKEN
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def generate_coach_feedback(rep_input, persona_response, persona_question, persona, history):
|
| 14 |
+
"""
|
| 15 |
+
Provides actionable coaching feedback after the persona's turn.
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
traits = persona.get("dynamic_state", {})
|
| 19 |
+
role = persona.get("role", "Client")
|
| 20 |
+
|
| 21 |
+
recent = []
|
| 22 |
+
for h in history[-5:]:
|
| 23 |
+
recent.append(f"Rep: {h['rep_input']}")
|
| 24 |
+
recent.append(f"Persona: {h['persona_response']}")
|
| 25 |
+
if h.get("persona_question"):
|
| 26 |
+
recent.append(f"Persona Q: {h['persona_question']}")
|
| 27 |
+
history_text = "\n".join(recent) if recent else "No prior turns."
|
| 28 |
+
|
| 29 |
+
system_prompt = f"""You are a sales coach analyzing a roleplay with a {role}.
|
| 30 |
+
Give specific, practical guidance in 2-4 sentences.
|
| 31 |
+
|
| 32 |
+
Persona traits: {traits}
|
| 33 |
+
Recent history:
|
| 34 |
+
{history_text}
|
| 35 |
+
|
| 36 |
+
Current exchange:
|
| 37 |
+
Rep: {rep_input}
|
| 38 |
+
Persona: {persona_response}
|
| 39 |
+
Persona Q: {persona_question}
|
| 40 |
+
|
| 41 |
+
Instructions:
|
| 42 |
+
- Identify the core objection or concern.
|
| 43 |
+
- Suggest one stronger framing or tactic (e.g., ROI, compliance, discovery).
|
| 44 |
+
- Offer one concise next step or question for the rep to ask.
|
| 45 |
+
- Do NOT break into persona voice; remain the coach.
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
full_prompt = f"<|system|>\n{system_prompt}\n<|user|>\nProvide coaching feedback.\n<|assistant|>\n"
|
| 49 |
+
|
| 50 |
+
out = coach_gen(full_prompt, max_new_tokens=180, do_sample=True, temperature=0.7)
|
| 51 |
+
text = out[0].get("generated_text") or out[0].get("text") or ""
|
| 52 |
+
if "<|assistant|>" in text:
|
| 53 |
+
text = text.split("<|assistant|>")[-1].strip()
|
| 54 |
+
|
| 55 |
+
return text.strip() or "Focus on clarifying value and risk mitigation. Ask one discovery question next."
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
safe_log("Coach feedback error", str(e))
|
| 59 |
+
return "Consider addressing the core objection directly, then bridge to measurable outcomes and a discovery question."
|