Spaces:
Sleeping
Sleeping
Delete dialogue.py
Browse files- dialogue.py +0 -237
dialogue.py
DELETED
|
@@ -1,237 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
PlotWeaver Voice Agent — Dialogue Manager
|
| 3 |
-
==========================================
|
| 4 |
-
FSM for multi-turn Hausa conversations across 3 verticals.
|
| 5 |
-
State lives in Gradio session state (dict) — no Redis needed in the Space.
|
| 6 |
-
"""
|
| 7 |
-
from __future__ import annotations
|
| 8 |
-
from dataclasses import dataclass, field, asdict
|
| 9 |
-
from enum import Enum
|
| 10 |
-
from typing import Optional
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class Vertical(str, Enum):
|
| 14 |
-
BANK = "bank"
|
| 15 |
-
TELECOM = "telecom"
|
| 16 |
-
ECOMMERCE = "ecommerce"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@dataclass
|
| 20 |
-
class DialogueState:
|
| 21 |
-
session_id: str
|
| 22 |
-
vertical: str
|
| 23 |
-
current_state: str = "greeting"
|
| 24 |
-
slots: dict = field(default_factory=dict)
|
| 25 |
-
turn_count: int = 0
|
| 26 |
-
escalate_to_human: bool = False
|
| 27 |
-
history: list = field(default_factory=list)
|
| 28 |
-
|
| 29 |
-
def to_dict(self):
|
| 30 |
-
return asdict(self)
|
| 31 |
-
|
| 32 |
-
@classmethod
|
| 33 |
-
def from_dict(cls, d):
|
| 34 |
-
return cls(**d) if d else None
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
SCENARIOS = {
|
| 38 |
-
"bank": {
|
| 39 |
-
"name": "PlotWeaver Bank",
|
| 40 |
-
"states": {
|
| 41 |
-
"greeting": {
|
| 42 |
-
"ha": "Sannu! Wannan shine mataimakin banki na PlotWeaver. Yaya zan taimake ka yau? Za ka iya ce 'duba ma'auni', 'toshe kati', ko 'canjin kuɗi'.",
|
| 43 |
-
"en": "Hello! This is the PlotWeaver banking assistant. How can I help you today? You can say 'check balance', 'block card', or 'transfer money'.",
|
| 44 |
-
"expects": "intent",
|
| 45 |
-
"transitions": {"check_balance": "ask_account_number", "block_card": "confirm_block_card", "transfer_money": "ask_recipient"},
|
| 46 |
-
},
|
| 47 |
-
"ask_account_number": {
|
| 48 |
-
"ha": "Don Allah ka faɗi lambobin ƙarshe huɗu na asusunka.",
|
| 49 |
-
"en": "Please say the last four digits of your account number.",
|
| 50 |
-
"expects": "digits",
|
| 51 |
-
"transitions": {"provide_digits": "return_balance"},
|
| 52 |
-
},
|
| 53 |
-
"return_balance": {
|
| 54 |
-
"ha": "Ma'aunin asusunka shine Naira dubu ɗari biyu da arba'in da biyar. Akwai wani abu?",
|
| 55 |
-
"en": "Your account balance is two hundred forty-five thousand Naira. Anything else?",
|
| 56 |
-
"expects": "yesno",
|
| 57 |
-
"transitions": {"yes": "greeting", "no": "exit"},
|
| 58 |
-
},
|
| 59 |
-
"confirm_block_card": {
|
| 60 |
-
"ha": "Don tabbatar, kana son toshe katinka? Ka ce 'i' ko 'a'a'.",
|
| 61 |
-
"en": "To confirm, you want to block your card? Say 'yes' or 'no'.",
|
| 62 |
-
"expects": "yesno",
|
| 63 |
-
"transitions": {"yes": "card_blocked", "no": "greeting"},
|
| 64 |
-
},
|
| 65 |
-
"card_blocked": {
|
| 66 |
-
"ha": "An toshe katinka. Sabon kati zai iso a cikin kwanaki uku zuwa biyar. Ana juya ka ga wakili don tabbatar.",
|
| 67 |
-
"en": "Your card is blocked. A new card will arrive in 3-5 days. Transferring you to an agent for confirmation.",
|
| 68 |
-
"expects": None, "terminal": True, "escalate": True,
|
| 69 |
-
},
|
| 70 |
-
"ask_recipient": {
|
| 71 |
-
"ha": "Zuwa wa kake son turawa? Ka faɗi sunan mai karɓa.",
|
| 72 |
-
"en": "Who do you want to transfer to? Say the recipient's name.",
|
| 73 |
-
"expects": "name",
|
| 74 |
-
"transitions": {"provide_name": "ask_amount"},
|
| 75 |
-
},
|
| 76 |
-
"ask_amount": {
|
| 77 |
-
"ha": "Nawa kake son turawa, a Naira?",
|
| 78 |
-
"en": "How much do you want to transfer, in Naira?",
|
| 79 |
-
"expects": "amount",
|
| 80 |
-
"transitions": {"provide_amount": "confirm_transfer"},
|
| 81 |
-
},
|
| 82 |
-
"confirm_transfer": {
|
| 83 |
-
"ha": "Zan tura kuɗin yanzu. Ka ce 'i' don ci gaba.",
|
| 84 |
-
"en": "I'll send the money now. Say 'yes' to continue.",
|
| 85 |
-
"expects": "yesno",
|
| 86 |
-
"transitions": {"yes": "transfer_done", "no": "greeting"},
|
| 87 |
-
},
|
| 88 |
-
"transfer_done": {
|
| 89 |
-
"ha": "An tura kuɗin. Godiya da zabar PlotWeaver Bank.",
|
| 90 |
-
"en": "Money sent. Thank you for choosing PlotWeaver Bank.",
|
| 91 |
-
"expects": None, "terminal": True,
|
| 92 |
-
},
|
| 93 |
-
},
|
| 94 |
-
},
|
| 95 |
-
"telecom": {
|
| 96 |
-
"name": "PlotWeaver Telecom",
|
| 97 |
-
"states": {
|
| 98 |
-
"greeting": {
|
| 99 |
-
"ha": "Sannu! Wannan shine PlotWeaver Telecom. Kana son 'saya airtime', 'saya bundle', ko 'yin korafi'?",
|
| 100 |
-
"en": "Hello! This is PlotWeaver Telecom. Would you like to 'buy airtime', 'buy bundle', or 'file a complaint'?",
|
| 101 |
-
"expects": "intent",
|
| 102 |
-
"transitions": {"buy_airtime": "ask_airtime_amount", "buy_bundle": "ask_bundle_type", "complaint": "ask_complaint"},
|
| 103 |
-
},
|
| 104 |
-
"ask_airtime_amount": {
|
| 105 |
-
"ha": "Nawa na airtime kake son saya? Misali, Naira ɗari ko dubu.",
|
| 106 |
-
"en": "How much airtime? For example 100 or 1000 Naira.",
|
| 107 |
-
"expects": "amount",
|
| 108 |
-
"transitions": {"provide_amount": "airtime_done"},
|
| 109 |
-
},
|
| 110 |
-
"airtime_done": {
|
| 111 |
-
"ha": "An kara airtime. Ma'aunin ka sabo shine Naira dubu ɗaya da ɗari biyar.",
|
| 112 |
-
"en": "Airtime loaded. Your new balance is 1500 Naira.",
|
| 113 |
-
"expects": None, "terminal": True,
|
| 114 |
-
},
|
| 115 |
-
"ask_bundle_type": {
|
| 116 |
-
"ha": "Wane irin bundle? Muna da 'rana', 'mako', ko 'wata'.",
|
| 117 |
-
"en": "Which bundle type? 'day', 'week', or 'month'.",
|
| 118 |
-
"expects": "bundle",
|
| 119 |
-
"transitions": {"provide_bundle": "bundle_done"},
|
| 120 |
-
},
|
| 121 |
-
"bundle_done": {
|
| 122 |
-
"ha": "An kunna bundle ɗinka. Za ka iya yin amfani da shi yanzu.",
|
| 123 |
-
"en": "Your bundle is active. You can use it now.",
|
| 124 |
-
"expects": None, "terminal": True,
|
| 125 |
-
},
|
| 126 |
-
"ask_complaint": {
|
| 127 |
-
"ha": "Me ya faru? Ka bayyana matsalar da kake fuskanta.",
|
| 128 |
-
"en": "What happened? Please describe the issue.",
|
| 129 |
-
"expects": "text",
|
| 130 |
-
"transitions": {"provide_text": "escalate"},
|
| 131 |
-
},
|
| 132 |
-
"escalate": {
|
| 133 |
-
"ha": "Nagode. Zan juya ka ga wakili na mutum yanzu.",
|
| 134 |
-
"en": "Thank you. I'll transfer you to a human agent now.",
|
| 135 |
-
"expects": None, "terminal": True, "escalate": True,
|
| 136 |
-
},
|
| 137 |
-
},
|
| 138 |
-
},
|
| 139 |
-
"ecommerce": {
|
| 140 |
-
"name": "PlotWeaver Delivery",
|
| 141 |
-
"states": {
|
| 142 |
-
"greeting": {
|
| 143 |
-
"ha": "Sannu! Wannan shine PlotWeaver Delivery. Kana son 'bincika oda', 'sake tsara lokaci', ko 'mayar da kaya'?",
|
| 144 |
-
"en": "Hello! This is PlotWeaver Delivery. Would you like to 'check order', 'reschedule', or 'return'?",
|
| 145 |
-
"expects": "intent",
|
| 146 |
-
"transitions": {"check_order": "ask_order_id", "reschedule": "ask_order_id_reschedule", "return_item": "ask_order_id_return"},
|
| 147 |
-
},
|
| 148 |
-
"ask_order_id": {
|
| 149 |
-
"ha": "Ka faɗi lambar oda naka.",
|
| 150 |
-
"en": "Say your order number.",
|
| 151 |
-
"expects": "digits",
|
| 152 |
-
"transitions": {"provide_digits": "order_status"},
|
| 153 |
-
},
|
| 154 |
-
"order_status": {
|
| 155 |
-
"ha": "Oda ɗinka yana kan hanya. Za a isar gobe da yamma.",
|
| 156 |
-
"en": "Your order is on the way. It will be delivered tomorrow evening.",
|
| 157 |
-
"expects": None, "terminal": True,
|
| 158 |
-
},
|
| 159 |
-
"ask_order_id_reschedule": {
|
| 160 |
-
"ha": "Ka faɗi lambar oda da kake son sake tsarawa.",
|
| 161 |
-
"en": "Say the order number you want to reschedule.",
|
| 162 |
-
"expects": "digits",
|
| 163 |
-
"transitions": {"provide_digits": "ask_new_date"},
|
| 164 |
-
},
|
| 165 |
-
"ask_new_date": {
|
| 166 |
-
"ha": "Wace rana kake so? Misali 'jumma'a' ko 'asabar'.",
|
| 167 |
-
"en": "Which day? For example 'Friday' or 'Saturday'.",
|
| 168 |
-
"expects": "date",
|
| 169 |
-
"transitions": {"provide_date": "reschedule_done"},
|
| 170 |
-
},
|
| 171 |
-
"reschedule_done": {
|
| 172 |
-
"ha": "An sake tsara isar. Za ka sami SMS na tabbatarwa.",
|
| 173 |
-
"en": "Delivery rescheduled. You'll receive a confirmation SMS.",
|
| 174 |
-
"expects": None, "terminal": True,
|
| 175 |
-
},
|
| 176 |
-
"ask_order_id_return": {
|
| 177 |
-
"ha": "Ka faɗi lambar oda da kake son mayarwa.",
|
| 178 |
-
"en": "Say the order number you want to return.",
|
| 179 |
-
"expects": "digits",
|
| 180 |
-
"transitions": {"provide_digits": "return_reason"},
|
| 181 |
-
},
|
| 182 |
-
"return_reason": {
|
| 183 |
-
"ha": "Me ya sa kake son mayarwa?",
|
| 184 |
-
"en": "Why do you want to return it?",
|
| 185 |
-
"expects": "text",
|
| 186 |
-
"transitions": {"provide_reason": "return_done"},
|
| 187 |
-
},
|
| 188 |
-
"return_done": {
|
| 189 |
-
"ha": "An karɓi buƙatarka. Wakili zai tattara kaya a gobe.",
|
| 190 |
-
"en": "Your request is received. An agent will collect the item tomorrow.",
|
| 191 |
-
"expects": None, "terminal": True,
|
| 192 |
-
},
|
| 193 |
-
},
|
| 194 |
-
},
|
| 195 |
-
}
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
def get_prompt(vertical: str, state_name: str) -> dict:
|
| 199 |
-
if state_name == "escalate_virtual":
|
| 200 |
-
return {"ha": "Zan juya ka ga wakili na mutum yanzu. Ka jira ɗan lokaci.",
|
| 201 |
-
"en": "I'll transfer you to a human agent now. Please hold."}
|
| 202 |
-
if state_name == "exit":
|
| 203 |
-
return {"ha": "Nagode. Sai watan.", "en": "Thank you. Goodbye."}
|
| 204 |
-
s = SCENARIOS[vertical]["states"].get(state_name)
|
| 205 |
-
if not s:
|
| 206 |
-
return {"ha": "Ban fahimci abin da ka ce ba.", "en": "I didn't understand."}
|
| 207 |
-
return {"ha": s["ha"], "en": s["en"]}
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
def get_expected_slot(vertical: str, state_name: str) -> Optional[str]:
|
| 211 |
-
s = SCENARIOS[vertical]["states"].get(state_name)
|
| 212 |
-
return s.get("expects") if s else None
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
def transition(state: DialogueState, intent: str, entities: dict) -> DialogueState:
|
| 216 |
-
state.turn_count += 1
|
| 217 |
-
for k, v in entities.items():
|
| 218 |
-
state.slots[k] = v
|
| 219 |
-
|
| 220 |
-
if intent == "human_agent" or state.turn_count > 12:
|
| 221 |
-
state.current_state = "escalate_virtual"
|
| 222 |
-
state.escalate_to_human = True
|
| 223 |
-
return state
|
| 224 |
-
|
| 225 |
-
current = SCENARIOS[state.vertical]["states"].get(state.current_state)
|
| 226 |
-
if not current:
|
| 227 |
-
state.current_state = "greeting"
|
| 228 |
-
return state
|
| 229 |
-
|
| 230 |
-
next_state = current.get("transitions", {}).get(intent)
|
| 231 |
-
if next_state:
|
| 232 |
-
state.current_state = next_state
|
| 233 |
-
target = SCENARIOS[state.vertical]["states"].get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