Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,52 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
from
|
4 |
-
|
5 |
-
from pydub import AudioSegment
|
6 |
-
import io
|
7 |
|
8 |
-
# Initialize whisper.cpp
|
9 |
-
w = Whisper('tiny')
|
10 |
|
11 |
-
def inference(audio_segment):
|
12 |
-
# Convert AudioSegment to WAV format in memory
|
13 |
-
with NamedTemporaryFile(suffix=".wav", delete=False) as temp:
|
14 |
-
# Export AudioSegment to raw bytes in WAV format
|
15 |
-
audio_segment.export(temp.name, format="wav")
|
16 |
-
temp.close() # Ensure the file is written and closed before passing it to Whisper
|
17 |
-
result = w.transcribe(temp.name)
|
18 |
-
text = w.extract_text(result)
|
19 |
-
return text[0]
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
# Display chat messages from history on app rerun
|
31 |
-
for message in st.session_state.messages:
|
32 |
-
with st.chat_message(message["role"]):
|
33 |
-
st.markdown(message["content"])
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# Debugging: Check the type of the audio object
|
40 |
-
st.write(f"Audio Type: {type(audio)}")
|
41 |
|
42 |
-
# Handle the case where audio is in a byte format
|
43 |
-
if isinstance(audio, bytes):
|
44 |
-
try:
|
45 |
-
# Convert the raw byte data to an AudioSegment instance
|
46 |
-
audio_segment = AudioSegment.from_file(io.BytesIO(audio), format="wav")
|
47 |
-
prompt = inference(audio_segment)
|
48 |
-
except Exception as e:
|
49 |
-
st.error(f"Error processing audio: {e}")
|
50 |
-
prompt = "Sorry, there was an error processing your audio."
|
51 |
|
52 |
-
# Handle the case where audio is an AudioSegment object
|
53 |
-
elif isinstance(audio, AudioSegment):
|
54 |
-
# Process it directly since it's already an AudioSegment
|
55 |
-
prompt = inference(audio)
|
56 |
|
57 |
-
|
58 |
-
st.error("The audio data is not in the expected format.")
|
59 |
-
prompt = "Sorry, the audio format is not correct."
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from audio_recorder_streamlit import audio_recorder
|
3 |
+
from openai import OpenAI
|
4 |
+
API_KEY = 'enter-openai-api-key-here'
|
|
|
|
|
5 |
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def transcribe_text_to_voice(audio_location):
|
9 |
+
client = OpenAI(api_key=API_KEY)
|
10 |
+
audio_file= open(audio_location, "rb")
|
11 |
+
transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
|
12 |
+
return transcript.text
|
13 |
|
14 |
+
def chat_completion_call(text):
|
15 |
+
client = OpenAI(api_key=API_KEY)
|
16 |
+
messages = [{"role": "user", "content": text}]
|
17 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo-1106", messages=messages)
|
18 |
+
return response.choices[0].message.content
|
19 |
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
def text_to_speech_ai(speech_file_path, api_response):
|
22 |
+
client = OpenAI(api_key=API_KEY)
|
23 |
+
response = client.audio.speech.create(model="tts-1",voice="nova",input=api_response)
|
24 |
+
response.stream_to_file(speech_file_path)
|
|
|
|
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
st.title("🧑💻 Skolo Online 💬 Talking Assistant")
|
|
|
|
|
29 |
|
30 |
+
"""
|
31 |
+
Hi🤖 just click on the voice recorder and let me know how I can help you today?
|
32 |
+
"""
|
|
|
33 |
|
34 |
+
audio_bytes = audio_recorder()
|
35 |
+
if audio_bytes:
|
36 |
+
##Save the Recorded File
|
37 |
+
audio_location = "audio_file.wav"
|
38 |
+
with open(audio_location, "wb") as f:
|
39 |
+
f.write(audio_bytes)
|
40 |
+
|
41 |
+
#Transcribe the saved file to text
|
42 |
+
text = transcribe_text_to_voice(audio_location)
|
43 |
+
st.write(text)
|
44 |
+
|
45 |
+
#Use API to get an AI response
|
46 |
+
api_response = chat_completion_call(text)
|
47 |
+
st.write(api_response)
|
48 |
+
|
49 |
+
# Read out the text response using tts
|
50 |
+
speech_file_path = 'audio_response.mp3'
|
51 |
+
text_to_speech_ai(speech_file_path, api_response)
|
52 |
+
st.audio(speech_file_path)
|