Spaces:
Running
Running
ADITYA0205
commited on
Commit
•
39ba104
1
Parent(s):
9cbc1ac
Delete streamlit_app.py
Browse files- streamlit_app.py +0 -101
streamlit_app.py
DELETED
@@ -1,101 +0,0 @@
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|