Including TTS in app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,10 @@ from dotenv import load_dotenv
|
|
6 |
from langchain_groq import ChatGroq
|
7 |
from langchain_core.output_parsers import StrOutputParser
|
8 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
|
|
|
|
|
9 |
|
10 |
load_dotenv()
|
11 |
client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
@@ -16,19 +20,26 @@ def frontend():
|
|
16 |
st.title("Voice AI Demo")
|
17 |
status_placeholder = st.empty()
|
18 |
status_placeholder.write("Press Mic button to start asking question")
|
19 |
-
recorded_audio = audio_recorder(sample_rate=
|
|
|
|
|
|
|
|
|
20 |
if recorded_audio:
|
21 |
-
status_placeholder.write("Converting audio
|
22 |
data_to_file(recorded_audio)
|
23 |
status_placeholder.write("Audio conversion done.")
|
24 |
-
status_placeholder.write("
|
25 |
transcription = audio_to_text("temp_audio.wav")
|
26 |
status_placeholder.write("Transcription is now made.")
|
27 |
status_placeholder.write("Getting response...")
|
28 |
response = answer(transcription)
|
|
|
|
|
29 |
status_placeholder.write("Press mic button again to ask more questions")
|
30 |
st.write("Q:" + transcription)
|
31 |
st.write("A: " + response)
|
|
|
32 |
|
33 |
#Fuction to convert audio data to audio file
|
34 |
def data_to_file(recorded_audio):
|
@@ -54,7 +65,7 @@ def answer(user_question):
|
|
54 |
)
|
55 |
|
56 |
prompt = ChatPromptTemplate([
|
57 |
-
("system", "You are super knowlegable AI chat bot
|
58 |
("user", "User Query: {question}"),
|
59 |
])
|
60 |
|
@@ -64,4 +75,10 @@ def answer(user_question):
|
|
64 |
answer = chain.invoke({'question': user_question})
|
65 |
return answer
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
frontend()
|
|
|
6 |
from langchain_groq import ChatGroq
|
7 |
from langchain_core.output_parsers import StrOutputParser
|
8 |
from langchain_core.prompts import ChatPromptTemplate
|
9 |
+
import edge_tts
|
10 |
+
import asyncio
|
11 |
+
|
12 |
+
from gtts import gTTS
|
13 |
|
14 |
load_dotenv()
|
15 |
client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
|
|
20 |
st.title("Voice AI Demo")
|
21 |
status_placeholder = st.empty()
|
22 |
status_placeholder.write("Press Mic button to start asking question")
|
23 |
+
recorded_audio = audio_recorder(sample_rate=8000)
|
24 |
+
text = st.chat_input()
|
25 |
+
if text:
|
26 |
+
response = answer(text)
|
27 |
+
st.write(response)
|
28 |
if recorded_audio:
|
29 |
+
status_placeholder.write("Converting audio...")
|
30 |
data_to_file(recorded_audio)
|
31 |
status_placeholder.write("Audio conversion done.")
|
32 |
+
status_placeholder.write("Uploading audio...")
|
33 |
transcription = audio_to_text("temp_audio.wav")
|
34 |
status_placeholder.write("Transcription is now made.")
|
35 |
status_placeholder.write("Getting response...")
|
36 |
response = answer(transcription)
|
37 |
+
status_placeholder.write("Coverting response in audio")
|
38 |
+
asyncio.run(convert_audio(response))
|
39 |
status_placeholder.write("Press mic button again to ask more questions")
|
40 |
st.write("Q:" + transcription)
|
41 |
st.write("A: " + response)
|
42 |
+
st.audio("output.mp3", format="audio/mp3", loop=False, autoplay=True)
|
43 |
|
44 |
#Fuction to convert audio data to audio file
|
45 |
def data_to_file(recorded_audio):
|
|
|
65 |
)
|
66 |
|
67 |
prompt = ChatPromptTemplate([
|
68 |
+
("system", "You are super knowlegable AI chat bot which will answer all User Query, answer with confident, also this response will get convert back to speech, so dont make point or anything, but make your answer in para form and dont make it too large, and use proper annotation, comma, full stop, question mark, so that a better text to speach can be genrate back."),
|
69 |
("user", "User Query: {question}"),
|
70 |
])
|
71 |
|
|
|
75 |
answer = chain.invoke({'question': user_question})
|
76 |
return answer
|
77 |
|
78 |
+
# Audio conversion
|
79 |
+
async def convert_audio(text):
|
80 |
+
filename = "output.mp3"
|
81 |
+
communicte = edge_tts.Communicate(text, "en-IN-NeerjaNeural")
|
82 |
+
await communicte.save(filename)
|
83 |
+
|
84 |
frontend()
|