| import os |
| from dotenv import load_dotenv |
|
|
| import wave |
| import pyaudio |
| from scipy.io import wavfile |
| import numpy as np |
|
|
| import whisper |
|
|
| from langchain.chains.llm import LLMChain |
| from langchain_core.prompts import PromptTemplate |
| from langchain_groq import ChatGroq |
|
|
| from gtts import gTTS |
| import pygame |
| from groq import Groq |
|
|
|
|
| load_dotenv() |
|
|
| groq_api_key = os.getenv("GROQ_API_KEY") |
|
|
|
|
| def is_silence(data, max_amplitude_threshold=3000): |
| """Check if audio data contains silence.""" |
| |
| max_amplitude = np.max(np.abs(data)) |
| return max_amplitude <= max_amplitude_threshold |
|
|
|
|
| def record_audio_chunk(audio, stream, chunk_length=5): |
| print("Recording...") |
| frames = [] |
| |
| |
| |
| num_chunks = int(16000 / 1024 * chunk_length) |
|
|
| |
| for _ in range(num_chunks): |
| data = stream.read(1024) |
| frames.append(data) |
|
|
| temp_file_path = os.path.dirname(__file__) + "/temp_audio_chunk.wav" |
| |
| print("Writing...") |
| with wave.open(temp_file_path, 'wb') as wf: |
| wf.setnchannels(1) |
| wf.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) |
| wf.setframerate(16000) |
| wf.writeframes(b''.join(frames)) |
|
|
| |
| try: |
| samplerate, data = wavfile.read(temp_file_path) |
| if is_silence(data): |
| |
| return True |
| else: |
| return False |
| except Exception as e: |
| print(f"Error while reading audio file: {e}") |
|
|
|
|
| def load_whisper(): |
| model = whisper.load_model("base") |
| return model |
|
|
|
|
| def transcribe_audio(api_key, filename): |
| print("Transcribing...") |
| |
| print("Current directory files:", os.listdir()) |
| if os.path.dirname(__file__) + "/"+ file_path: |
| results = model.transcribe(file_path) |
| return results['text'] |
| else: |
| return None |
|
|
| def load_prompt(): |
| input_prompt = """ |
| |
| You are a friendly conversational chatbot |
| |
| Previous conversation: |
| {chat_history} |
| |
| New human question: {question} |
| Response: |
| """ |
| return input_prompt |
|
|
|
|
| def load_llm(): |
| chat_groq = ChatGroq(temperature=0, model_name="llama3-8b-8192", |
| groq_api_key=groq_api_key) |
| return chat_groq |
|
|
|
|
| def get_response_llm(user_question, memory): |
| input_prompt = load_prompt() |
|
|
| chat_groq = load_llm() |
|
|
| |
| prompt = PromptTemplate.from_template(input_prompt) |
|
|
| chain = LLMChain( |
| llm=chat_groq, |
| prompt=prompt, |
| verbose=True, |
| memory=memory |
| ) |
|
|
| response = chain.invoke({"question": user_question}) |
|
|
| return response['text'] |
|
|
|
|
| def play_text_to_speech(text, language='en', slow=False): |
| |
| tts = gTTS(text=text, lang=language, slow=slow) |
|
|
| |
| temp_audio_file = "temp_audio.mp3" |
| tts.save(temp_audio_file) |
|
|
| |
| pygame.mixer.init() |
|
|
| |
| pygame.mixer.music.load(temp_audio_file) |
|
|
| |
| pygame.mixer.music.play() |
|
|
| |
| while pygame.mixer.music.get_busy(): |
| pygame.time.Clock().tick(10) |
|
|
| |
| pygame.mixer.music.stop() |
|
|
| |
| pygame.mixer.quit() |
| os.remove(temp_audio_file) |