Last active
May 2, 2023 16:51
-
-
Save vikrum/e1587371a16f2faf1ca166bc49c03e8c to your computer and use it in GitHub Desktop.
If we can't use AI for terrible dad jokes and puns then what's the point?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Infinite loop, record 30 sec from mic, transcribe, gpt3.5-turbo to tell a terrible dad joke/pun | |
# brew install portaudio ffmpeg | |
# pip install -r requirements.txt | |
import pyaudio | |
import wave | |
from pydub import AudioSegment | |
import openai | |
import os | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
# Define parameters for audio recording | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 1 | |
RATE = 44100 | |
CHUNK = 1024 | |
# 30 sec is roughly ~256kb as mp3; whisper's limit is 25mb. | |
RECORD_SECONDS = 30 | |
FILE_NAME = "recording.mp3" | |
while True: | |
audio = pyaudio.PyAudio() | |
# Open the microphone stream | |
stream = audio.open(format=FORMAT, channels=CHANNELS, | |
rate=RATE, input=True, | |
frames_per_buffer=CHUNK) | |
print("Recording...") | |
# Create an empty list to store the audio frames | |
frames = [] | |
# Record audio for the specified number of seconds | |
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): | |
data = stream.read(CHUNK) | |
frames.append(data) | |
print("Recording finished.") | |
# Stop and close the microphone stream | |
stream.stop_stream() | |
stream.close() | |
audio.terminate() | |
# Convert the audio frames to a Pydub audio segment | |
audio_segment = AudioSegment( | |
data=b''.join(frames), | |
sample_width=audio.get_sample_size(FORMAT), | |
frame_rate=RATE, | |
channels=CHANNELS | |
) | |
# Save the audio segment as an MP3 file | |
audio_segment.export(FILE_NAME, format="mp3") | |
# Load the audio file | |
audio_file = open(FILE_NAME, "rb") | |
transcript = openai.Audio.transcribe("whisper-1", audio_file) | |
# print(transcript) | |
# Use the OpenAI API to generate a pun or terrible dad joke | |
prompt = "What's a good dad joke or pun based on the following transcript: " + transcript.text | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0.2, | |
) | |
# Print the pun or terrible dad joke | |
print(response.choices[0].message.content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pyaudio | |
wave | |
pydub | |
openai |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment