OpenAI functions and clearer parameters
This commit is contained in:
parent
c64a9dd7db
commit
dba02ae5ea
@ -1,2 +1,5 @@
|
|||||||
OPENAI_API_KEY =
|
OPENAI_API_KEY =
|
||||||
OPENAI_BASE_URL =
|
OPENAI_BASE_URL =
|
||||||
|
OPENAI_RESPONSES_INSTRUCTIONS =
|
||||||
|
SEGMENT_DURATION =
|
||||||
|
TMP_AUDIO_PATH =
|
||||||
|
58
app.py
58
app.py
@ -1,40 +1,48 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from moviepy import VideoFileClip
|
from moviepy import VideoFileClip
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
from pydub import AudioSegment
|
from pydub import AudioSegment
|
||||||
from pydub.playback import play
|
|
||||||
|
|
||||||
DEFAULT_RESPONSES_INSTRUCTIONS = "You will be provided a video transcription for which you are to generate a blog post in Markdown format summarizing the video's contents."
|
PROMPT = "You will be provided a video transcription for which you are to generate a blog post in Markdown format summarizing the video contents."
|
||||||
TMP_AUDIO_PATH = "/tmp/video-summary-bot-tmp-audio.wav"
|
|
||||||
|
|
||||||
async def main():
|
def main(args):
|
||||||
parser = argparse.ArgumentParser(description="Use AI models to summarize videos")
|
openai_client = OpenAI()
|
||||||
parser.add_argument('--video-file', type=str, help="Path to the video to be summarized")
|
return summarize_transcription(
|
||||||
parser.add_argument('--segment-duration', type=int, help="Lenght of audio segments")
|
openai_client,
|
||||||
args = parser.parse_args()
|
transcribe_audio(
|
||||||
load_dotenv()
|
openai_client,
|
||||||
VideoFileClip(args.video_file).audio.write_audiofile(TMP_AUDIO_PATH)
|
get_audio_from_video(args.video_file_path)
|
||||||
audio = AudioSegment.from_wav(TMP_AUDIO_PATH)
|
)
|
||||||
segments = [audio[i:i + args.segment_duration] for i in range(0, len(audio), args.segment_duration)]
|
)
|
||||||
# TODO: Test OpenAI
|
|
||||||
client = OpenAI()
|
def get_audio_from_video(video_file_path):
|
||||||
transcription = ' '.join([
|
tmp_audio_path = os.getenv('TMP_AUDIO_PATH', '/tmp/video_summary_bot_tmp_audio.wav')
|
||||||
await client.audio.transcriptions.create(
|
VideoFileClip(video_file_path).audio.write_audiofile(tmp_audio_path)
|
||||||
model=args.transcription_model,
|
return AudioSegment.from_wav(tmp_audio_path)
|
||||||
|
|
||||||
|
def transcribe_audio(openai_client, audio):
|
||||||
|
segment_duration = int(os.getenv('SEGMENT_DURATION', 30000)),
|
||||||
|
transcription_model = os.getenv('OPENAI_TRANSCRIPTION_MODEL', 'whisper-1')
|
||||||
|
return ' '.join([
|
||||||
|
openai_client.audio.transcriptions.create(
|
||||||
|
model=transcription_model,
|
||||||
file=each
|
file=each
|
||||||
).text for each in segments
|
).text for each in [audio[i:i + segment_duration] for i in range(0, len(audio), segment_duration)]
|
||||||
])
|
])
|
||||||
summary = client.responses.create(
|
|
||||||
model=args.responses_model,
|
def summarize_transcription(openai_client, transcription):
|
||||||
instructions=DEFAULT_RESPONSES_INSTRUCTIONS,
|
responses_model = os.getenv('OPENAI_RESPONSES_MODEL', 'whisper-1')
|
||||||
|
return client.responses.create(
|
||||||
|
model=responses_model,
|
||||||
|
instructions=PROMPT,
|
||||||
input=transcription
|
input=transcription
|
||||||
)
|
)
|
||||||
return summary
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
summary = main()
|
load_dotenv()
|
||||||
print(summary)
|
parser = argparse.ArgumentParser(description="Use AI models to summarize videos")
|
||||||
|
parser.add_argument('--video-file-path', type=str, help="Path to the video to be summarized")
|
||||||
|
main(parser.parse_args())
|
||||||
|
Loading…
Reference in New Issue
Block a user