ADITYA0205 commited on
Commit
e37f3e5
1 Parent(s): 13065fd

Upload 6 files

Browse files
Files changed (6) hide show
  1. .env +3 -0
  2. .env.sample +2 -0
  3. Voice_Deepgram.zip +3 -0
  4. app.py +101 -0
  5. requirements.txt +8 -0
  6. temp.wav +0 -0
.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ DEEPGRAM_API_KEY="f5984a104dec71de1e6a2cc10cd3242cedccfe10"
2
+ #OPENAI_API_KEY="sk-proj-DPCl3xqekKXbqesokl5JT3BlbkFJfNvUT163z04Y8AUFd89"
3
+ GROQ_API_KEY="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"
.env.sample ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ DEEPGRAM_API_KEY=""
2
+ GROQ_API_KEY=""
Voice_Deepgram.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0cdab898b9816a1729ab14ff156c32d737c08edb08ee6cfd2792a76484935a21
3
+ size 564225
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ import sounddevice as sd
4
+ import numpy as np
5
+ import wave
6
+ import pygame
7
+ import streamlit as st
8
+ from deepgram import Deepgram
9
+ from groq import Groq
10
+ from dotenv import load_dotenv
11
+ from gtts import gTTS
12
+
13
+ # Load API keys from .env file
14
+ load_dotenv()
15
+ DEEPGRAM_API_KEY = os.getenv("DEEPGRAM_API_KEY")
16
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
17
+
18
+ if not DEEPGRAM_API_KEY or not GROQ_API_KEY:
19
+ raise ValueError("API keys for Deepgram and Groq must be set in the .env file")
20
+
21
+ # Initialize Deepgram and Groq clients
22
+ dg_client = Deepgram(DEEPGRAM_API_KEY)
23
+ groq_client = Groq(api_key=GROQ_API_KEY)
24
+
25
+ # Audio recording parameters
26
+ DURATION = 5 # seconds
27
+ SAMPLERATE = 16000
28
+ FILENAME = "output.wav"
29
+ RESPONSE_AUDIO = "response.mp3"
30
+
31
+ async def recognize_audio_deepgram(filename):
32
+ with open(filename, 'rb') as audio:
33
+ source = {'buffer': audio.read(), 'mimetype': 'audio/wav'}
34
+ response = await dg_client.transcription.prerecorded(source, {'punctuate': True, 'language': 'en-US'})
35
+ return response['results']['channels'][0]['alternatives'][0]['transcript']
36
+
37
+ def record_audio(filename, duration, samplerate):
38
+ st.write("Recording🔉...")
39
+ audio_data = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype=np.int16)
40
+ sd.wait() # Wait until recording is finished
41
+ wavefile = wave.open(filename, 'wb')
42
+ wavefile.setnchannels(1)
43
+ wavefile.setsampwidth(2)
44
+ wavefile.setframerate(samplerate)
45
+ wavefile.writeframes(audio_data.tobytes())
46
+ wavefile.close()
47
+ st.write("Recording finished🔴.")
48
+
49
+ def generate_response(prompt):
50
+ response = groq_client.chat.completions.create(
51
+ model="llama3-8b-8192",
52
+ messages=[
53
+ {"role": "system", "content": "You are a helpful assistant."},
54
+ {"role": "user", "content": prompt}
55
+ ],
56
+ temperature=0.29,
57
+ max_tokens=100,
58
+ top_p=1,
59
+ stream=False,
60
+ stop=None,
61
+ )
62
+ return response.choices[0].message.content.strip()
63
+
64
+ def play_response(text):
65
+ tts = gTTS(text=text, lang='en')
66
+ tts.save(RESPONSE_AUDIO)
67
+ pygame.mixer.init()
68
+ pygame.mixer.music.load(RESPONSE_AUDIO)
69
+ pygame.mixer.music.play()
70
+ while pygame.mixer.music.get_busy():
71
+ pygame.time.Clock().tick(10)
72
+ pygame.mixer.quit()
73
+ os.remove(RESPONSE_AUDIO) # Clean up the response audio file
74
+
75
+ async def main():
76
+ stop_keywords = {"thank you", "goodbye", "exit"}
77
+
78
+ while True:
79
+ record_audio(FILENAME, DURATION, SAMPLERATE)
80
+ user_input = await recognize_audio_deepgram(FILENAME)
81
+ st.write(f"User: {user_input}")
82
+
83
+ if any(keyword in user_input.lower() for keyword in stop_keywords):
84
+ st.write("Conversation ended.")
85
+ play_response("Goodbye! Have a great day!")
86
+ break
87
+
88
+ response = generate_response(user_input)
89
+ st.write(f"Bot: {response}")
90
+ play_response(response)
91
+ os.remove(FILENAME) # Clean up the audio file
92
+
93
+ # Streamlit UI
94
+ def run_streamlit_app():
95
+ st.title("Voice Chatbot🔊")
96
+
97
+ if st.button("Start Conversation"):
98
+ asyncio.run(main())
99
+
100
+ if __name__ == "__main__":
101
+ run_streamlit_app()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ deepgram
2
+ gtts
3
+ groq
4
+ numpy
5
+ pygame
6
+ python-dotenv
7
+ sounddevice
8
+ streamlit
temp.wav ADDED
Binary file (441 kB). View file