Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model Creation #8

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ async def results(request: Request):
req = await request.json()

# Extract action from the request
action = req.get('queryResult', {}).get('action', 'unknown')

# action = req.get('queryResult', {}).get('action', 'unknown')
response_text = req.get('queryResult', {}).get('fulfillmentText', 'Default response')
return JSONResponse(content={"fulfillmentText": response_text})
# Return a fulfillment response
return {"fulfillmentText": f"This is a response from webhook. Action: {action}"}
# return {"fulfillmentText": f"This is a response from webhook. Action: {action}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error processing request: {e}")

Expand Down
36 changes: 26 additions & 10 deletions service/server.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import socket
import os
from datetime import datetime
import threading
import time
from datetime import datetime
import requests

class AudioStreamServer:
def __init__(self, host=socket.gethostname(), port=5000, chunk_duration=10):
def __init__(self, host=socket.gethostname(), port=5000, chunk_duration=10, webhook_url="http://localhost:8000/upload-audio"):
self.host = host
self.port = port
self.chunk_duration = chunk_duration
self.webhook_url = webhook_url
self.buffer = bytearray()
self.buffer_lock = threading.Lock()

self.save_dir = "raw_chunks"
os.makedirs(self.save_dir, exist_ok=True)

self.socket_chunk_size = 8192
self.bytes_per_chunk = 44100 * 2 * 2 * self.chunk_duration # 44.1kHz, 16-bit, stereo

def start(self):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((self.host, self.port))
Expand All @@ -37,11 +39,11 @@ def handle_client(self, conn):
data = conn.recv(self.socket_chunk_size)
if not data:
break

with self.buffer_lock:
self.buffer.extend(data)
print(f"Received {len(data)} bytes")

except Exception as e:
print(f"Client handling error: {e}")
finally:
Expand All @@ -53,22 +55,36 @@ def process_chunks(self):
if len(self.buffer) >= self.bytes_per_chunk:
chunk_data = bytes(self.buffer[:self.bytes_per_chunk])
self.buffer = self.buffer[self.bytes_per_chunk:]
self.save_chunk_as_raw(chunk_data)
self.save_and_send_chunk(chunk_data)
time.sleep(0.1)

def save_chunk_as_raw(self, chunk_data):
def save_and_send_chunk(self, chunk_data):
try:
# Save the chunk locally
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"chunk_{timestamp}.raw"
filepath = os.path.join(self.save_dir, filename)

with open(filepath, 'wb') as f:
f.write(chunk_data)

print(f"Saved chunk as RAW: {filename}")

# Send the chunk to the webhook
self.send_chunk_to_webhook(filepath)

except Exception as e:
print(f"Error saving/sending RAW file: {e}")

def send_chunk_to_webhook(self, filepath):
try:
with open(filepath, 'rb') as f:
files = {'file': (os.path.basename(filepath), f, 'application/octet-stream')}
response = requests.post(self.webhook_url, files=files)
print(f"Webhook response: {response.status_code}, {response.json()}")

except Exception as e:
print(f"Error saving RAW file: {e}")
print(f"Error sending file to webhook: {e}")


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions service/stt-scam-detection.ipynb

Large diffs are not rendered by default.