-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer_py
More file actions
214 lines (180 loc) · 7.99 KB
/
Server_py
File metadata and controls
214 lines (180 loc) · 7.99 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Launch code : python3 server.py
cat << 'EOF' > server.py
import asyncio
import websockets
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import vosk
import whisper
import wave
import os
import datetime
import time
from rapidfuzz import process, fuzz
from word2number import w2n
import re
# --- CONFIGURATION ---
SHEET_NAME = "ArrayButton_DB"
CREDENTIALS_FILE = "credentials.json"
STATUS_FILE = "node_status.json"
# 1. DEFINE VALID ITEMS
INVENTORY_ITEMS = ["Cement", "Bricks", "Dust", "Steel", "Water", "Sand", "Product"]
# 2. DEFINE SYNONYMS (Hinglish Support)
SYNONYMS = {
"concrete": "Cement", "semment": "Cement", "card": "Cement",
"liquid": "Water", "aqua": "Water",
"block": "Bricks", "brick": "Bricks",
"rod": "Steel", "iron": "Steel", "metal": "Steel", "sariya": "Steel", "loha": "Steel",
"powder": "Dust", "ret": "Sand", "balu": "Sand", "eet": "Bricks",
"simtya": "Cement"
}
# 3. HINDI NUMBERS
HINDI_NUMBERS = {
"ek": 1, "do": 2, "teen": 3, "char": 4, "paanch": 5, "che": 6, "saat": 7, "aath": 8, "nau": 9, "das": 10,
"gyarah": 11, "barah": 12, "pandrah": 15,
"bees": 20, "bis": 20, "beej": 20, "bhees": 20, "vis": 20,
"tis": 30, "tees": 30, "chalis": 40, "pachas": 50, "pachaas": 50,
"saath": 60, "sattar": 70, "assi": 80, "nabbey": 90, "sau": 100, "so": 100, "hazar": 1000
}
USER_WAKE_WORDS = {
"Shreyas": "alpha",
"Likith": "delta",
"Arun": "charlie"
}
DEFAULT_WAKE_WORD = "button"
# --- AI MODELS ---
if not os.path.exists("model"):
print("❌ Error: 'model' folder not found.")
exit()
vosk.SetLogLevel(-1)
vosk_model = vosk.Model("model")
whisper_model = whisper.load_model("small")
# --- ROBUST DB CONNECTION ---
def get_db_connection():
while True:
try:
print("☁️ Connecting to Google Sheets...")
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, scope)
client = gspread.authorize(creds)
sheet = client.open(SHEET_NAME)
logs = sheet.worksheet("Logs")
inv = sheet.worksheet("Inventory")
print("✅ Database Connected!")
return logs, inv, sheet
except Exception as e:
print(f"⚠️ Connection Error: {e}")
time.sleep(60)
logs_tab, inventory_tab, sheet = get_db_connection()
connected_nodes = {}
def update_status_file():
with open(STATUS_FILE, "w") as f:
json.dump(connected_nodes, f)
def parse_quantity(text):
digits = re.findall(r'\d+', text)
if digits: return int(digits[0])
for word, number in HINDI_NUMBERS.items():
if word in text: return number
try: return w2n.word_to_num(text)
except: return None
async def process_audio(websocket):
node_id = "Unknown"
user_name = "Ghost"
my_wake_word = DEFAULT_WAKE_WORD
try:
# HANDSHAKE
message = await websocket.recv()
if isinstance(message, str):
data = json.loads(message)
node_id = data.get("id", "Unknown")
user_name = data.get("user", "Ghost")
my_wake_word = USER_WAKE_WORDS.get(user_name, DEFAULT_WAKE_WORD)
print(f"🔗 CONNECTED: {node_id} ({user_name}) --> Wake Word: '{my_wake_word}'")
connected_nodes[node_id] = "Online"
update_status_file()
except: return
# --- TURBO VOSK SETUP ---
# Grammar makes waking up fast and accurate
grammar = '["alpha", "bravo", "charlie", "delta", "button", "[unk]"]'
rec = vosk.KaldiRecognizer(vosk_model, 16000, grammar)
state = "IDLE"
buffer = bytearray()
start_time = 0
try:
async for message in websocket:
if not isinstance(message, bytes): continue
# --- PHASE 1: INSTANT WAKE DETECTION ---
if state == "IDLE":
wake_detected = False
# Check Partial (Fastest)
if rec.AcceptWaveform(message):
res = json.loads(rec.Result())
if my_wake_word in res.get('text', ''): wake_detected = True
else:
partial = json.loads(rec.PartialResult())
if my_wake_word in partial.get('partial', ''): wake_detected = True
if wake_detected:
print(f"⚡ INSTANT WAKE: {user_name}!")
state = "LISTENING"
buffer = bytearray()
start_time = time.time()
# We DO NOT clear the buffer here anymore.
# We start fresh.
# --- PHASE 2: TIME-BASED RECORDING (4 SECONDS) ---
elif state == "LISTENING":
buffer.extend(message)
# Record for exactly 4 seconds (Time based is safer than Byte based)
if time.time() - start_time > 4.0:
print(f"🛑 Processing {user_name} (Captured 4s)...")
filename = f"temp_{node_id}.wav"
with wave.open(filename, 'wb') as wf:
wf.setnchannels(1); wf.setsampwidth(2); wf.setframerate(16000); wf.writeframes(buffer)
# WHISPER
try:
result = whisper_model.transcribe(filename, fp16=False)
text = result['text'].strip().lower()
except: text = ""
# Filter out Whisper Hallucinations (Short junk)
if len(text) < 3 or text in ["you", "thank you", "subtitles"]:
print("🗑️ Ignored Silence/Noise.")
else:
# SYNONYM FIX
for wrong, right in SYNONYMS.items():
if wrong in text: text = text.replace(wrong, right.lower())
print(f"🗣️ Heard: '{text}'")
# LOGIC
item_match = process.extractOne(text, INVENTORY_ITEMS, scorer=fuzz.partial_ratio)
quantity = parse_quantity(text)
if item_match and item_match[1] > 50 and quantity:
item_name = item_match[0]
print(f"✅ ACTION: {item_name} -> {quantity}")
try:
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logs_tab.append_row([timestamp, node_id, user_name, item_name, quantity, "Used"])
records = inventory_tab.get_all_records()
for i, row in enumerate(records):
if row['Item Name'] == item_name:
new_total = int(row['Total']) - quantity
inventory_tab.update_cell(i + 2, 2, new_total)
print(f"📉 Stock Updated.")
break
except: print("⚠️ Cloud Error (Quota?)")
else:
print(f"🤷♂️ Ignored (Qty: {quantity}, Item: {item_match[0] if item_match else 'None'})")
# RESET
state = "IDLE"
rec = vosk.KaldiRecognizer(vosk_model, 16000, grammar)
except: pass
finally:
print(f"🔴 DISCONNECTED: {node_id}")
if node_id in connected_nodes: del connected_nodes[node_id]
update_status_file()
async def main():
update_status_file()
print("👑 Queen Server Started (GOLDEN RATIO MODE)...")
async with websockets.serve(process_audio, "0.0.0.0", 8765, ping_interval=None):
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())
EOF