Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshat2512 committed Nov 27, 2024
1 parent e354338 commit 4e4cf26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
12 changes: 6 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from backend.speech_proccessing import process_audio_stream
from backend.openai_models import transcribe_audio, generate_response, generate_image_response, ChatHistory


import time, wave
import asyncio
import json
Expand All @@ -25,7 +24,6 @@

load_dotenv()


app = FastAPI()

users_directory = {} # maintain users database or their chat history in their where each key represents the user_id
Expand All @@ -40,19 +38,20 @@ async def get(request: Request):

@app.websocket('/ws/{user_id}') # will be responsible for handling real time stream of audio chunks and all AI generated responses will be sent to the streamer client
async def chat(websocket: WebSocket, user_id: str):
i = 0

if user_id not in users_directory:
users_directory[user_id] = ChatHistory() # creates an instance of the ChatHistory class and each user will have their own instance of ChatHistory

chat_history = users_directory[user_id]

await websocket.accept()

audio_queue = asyncio.Queue()
response_queue = asyncio.Queue()

process_task = asyncio.create_task(process_audio_stream(audio_queue, response_queue)) # It will create asynchrounous task to handle audio_queue in the background, detect speeches in the audio_queue using pre trained Model and add it to response_queue


i = 0
while True:

try:
Expand All @@ -69,6 +68,7 @@ async def chat(websocket: WebSocket, user_id: str):
# print(audio_queue.qsize())
if not response_queue.empty():
logger.info('Speech detected')
await asyncio.sleep(0.1)
await generate_ai_response(response_queue, websocket, user_id, chat_history) # for generating ai responses and send it back to the client


Expand Down Expand Up @@ -100,7 +100,7 @@ async def handle_audio_new(websocket: WebSocket, audio_queue):

if not audio_data:
break
await audio_queue.put(audio_data)
await audio_queue.put(audio_data)

return True
except Exception as e:
Expand Down
5 changes: 1 addition & 4 deletions backend/speech_proccessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ async def process_audio_stream(audio_queue, response_queue):
audio_buffer = np.zeros(TARGET_LENGTH, dtype=np.float32)
audio_chunks = []
audio_data = b''
# Open the audio stream

# print("Listening... Press Ctrl+C to stop.")

speak = 0
silence = 0
# Continuously read from the stream and append to audio_data


while True:
try:
Expand Down

0 comments on commit 4e4cf26

Please sign in to comment.