-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation.py
More file actions
83 lines (65 loc) · 2.6 KB
/
conversation.py
File metadata and controls
83 lines (65 loc) · 2.6 KB
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
from stt_whisper_stream import listen_until_silence
from name_extractor import extract_name, extract_spelled_name
from memory import reset, add, get_history
from history_store import save_call
from summary import summarize_call
def handle_call():
reset()
state = "ASK_NAME"
caller_name = None
attempts = 0
print("Assistant: Hello. May I know who is calling?")
while True:
user_text = listen_until_silence()
if not user_text:
continue
print("Caller:", user_text)
add("Caller", user_text)
# -------- ASK NAME --------
if state == "ASK_NAME":
name = extract_name(user_text)
attempts += 1
if name:
caller_name = name
reply = f"Did I hear your name as {caller_name}?"
state = "CONFIRM_NAME"
elif attempts >= 2:
reply = "Please spell your name letter by letter."
state = "SPELL_NAME"
else:
reply = "Sorry, I did not catch your name. Please say it again."
# -------- CONFIRM NAME --------
elif state == "CONFIRM_NAME":
t = user_text.lower().strip()
if t.startswith("yes"):
reply = f"Thank you {caller_name}. What is this regarding?"
state = "ASK_REASON"
elif t.startswith("no"):
reply = "Please spell your name letter by letter."
state = "SPELL_NAME"
else:
reply = f"Please say YES if your name is {caller_name}, or NO to repeat."
# -------- SPELL NAME --------
elif state == "SPELL_NAME":
spelled = extract_spelled_name(user_text)
if spelled:
caller_name = spelled
reply = f"Thank you {caller_name}. What is this regarding?"
state = "ASK_REASON"
else:
reply = "Please spell your name clearly."
# -------- ASK REASON --------
elif state == "ASK_REASON":
if len(user_text.split()) >= 3:
reply = "Thank you. I will inform the user."
add("Assistant", reply)
transcript = get_history()
summary = summarize_call(transcript)
path = save_call(transcript, summary)
print("📁 Call saved to:", path)
print("🧾 Summary:", summary)
break
else:
reply = "Could you please briefly explain the reason for your call?"
print("Assistant:", reply)
add("Assistant", reply)