-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (48 loc) · 2.05 KB
/
app.py
File metadata and controls
57 lines (48 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sounddevice as sd
from scipy.io.wavfile import write
import whisper
import os
from openai import OpenAI
from playsound import playsound
from pathlib import Path
# Set the path to the FFmpeg bin directory
ffmpeg_path = r"C:\Users\aghazouani\Downloads\ffmpeg-2023-11-05-git-44a0148fad-essentials_build\bin"
os.environ["PATH"] += os.pathsep + ffmpeg_path
# Record the audio
def record_audio(duration=10, sample_rate=44100, filename='output.wav'):
print("Recording...")
recording = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=2)
sd.wait() # Wait until recording is finished
write(filename, sample_rate, recording) # Save as WAV file
print("Recording finished.")
# Transcribe the audio
def transcribe_audio(filename='output.wav'):
print("Transcribing...")
model = whisper.load_model("base")
audio = whisper.load_audio(filename)
result = model.transcribe(audio)
print("Transcription:", result['text'])
return result['text']
# Instantiate the OpenAI client with your API key
Client = OpenAI(api_key="Your OpenAI Key")
# Function to convert text to speech and play it
def text_to_speech_and_play(text, speech_file_path='response.mp3'):
# Get GPT-3 response
gpt_response = Client.completions.create(prompt=text,model="text-davinci-003",max_tokens=150)
response_text = gpt_response.choices[0].text.strip()
print("GPT-3 Response:", response_text)
# Generate speech from the GPT-3 response
response = Client.audio.speech.create(
model="tts-1",
voice="alloy",
input=response_text
)
# Save the TTS response as an audio file
with open(speech_file_path, 'wb') as f:
f.write(response.content)
# Play the audio response
playsound(str(speech_file_path))
# Full process
record_audio(duration=5) # Record for 5 seconds
transcribed_text = transcribe_audio() # Transcribe the recorded audio
text_to_speech_and_play(transcribed_text) # Convert GPT-3 text response to speech and play it