File tree 4 files changed +161
-0
lines changed
4 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+ import sys
3
+ from datetime import datetime
4
+ import urllib .request
5
+ from dotenv import load_dotenv
6
+ import openai
7
+ from openai import OpenAI
8
+
9
+ load_dotenv ()
10
+
11
+ # Fail if no commandline argument is provided
12
+ if len (sys .argv ) < 2 :
13
+ print ("Please provide an text" )
14
+ exit (1 )
15
+
16
+ text_prompt = sys .argv [1 ]
17
+
18
+ client = OpenAI ()
19
+
20
+ image_url = ""
21
+
22
+ try :
23
+ response = client .images .generate (
24
+ model = "dall-e-3" ,
25
+ prompt = text_prompt , # "a white siamese cat",
26
+ size = "1024x1024" ,
27
+ quality = "standard" ,
28
+ n = 1 ,
29
+ )
30
+ image_url = response .data [0 ].url
31
+ except openai .OpenAIError as e :
32
+ print (e .http_status )
33
+ print (e .error )
34
+ exit (1 )
35
+
36
+ print (image_url )
37
+
38
+ # Download image using python and save it to file in the dalle folder
39
+ if not os .path .isdir ("dalle" ):
40
+ os .mkdir ("dalle" )
41
+ with urllib .request .urlopen (image_url ) as response :
42
+ cur_time = datetime .now ().strftime ("%Y-%m-%d_%H-%M-%S" )
43
+ with open (f"dalle/{ cur_time } .png" , "wb" ) as f :
44
+ f .write (response .read ())
45
+ with open (f"dalle/{ cur_time } .txt" , "wb" ) as f :
46
+ f .write (text_prompt .encode ("utf-8" ))
Original file line number Diff line number Diff line change
1
+ import os
2
+ import sys
3
+ from datetime import datetime
4
+ import urllib .request
5
+ from dotenv import load_dotenv
6
+ import openai
7
+ from openai import OpenAI
8
+ from pathlib import Path
9
+
10
+ load_dotenv ()
11
+
12
+ # Fail if no commandline argument is provided
13
+ if len (sys .argv ) < 2 :
14
+ print ("Please provide an text" )
15
+ exit (1 )
16
+
17
+ text_prompt = sys .argv [1 ]
18
+
19
+ client = OpenAI ()
20
+
21
+ speech_file_path = ""
22
+
23
+ # Stream audio and save it to file in the voice folder
24
+ if not os .path .isdir ("voice" ):
25
+ os .mkdir ("voice" )
26
+
27
+ cur_time = datetime .now ().strftime ("%Y-%m-%d_%H-%M-%S" )
28
+
29
+ try :
30
+ speech_file_path = f"voice/{ cur_time } .mp3"
31
+ response = client .audio .speech .create (
32
+ model = "tts-1" ,
33
+ voice = "alloy" ,
34
+ input = text_prompt
35
+ )
36
+
37
+ response .stream_to_file (speech_file_path )
38
+ except openai .OpenAIError as e :
39
+ print (e .http_status )
40
+ print (e .error )
41
+ exit (1 )
42
+
43
+ print (speech_file_path )
Original file line number Diff line number Diff line change
1
+ import os
2
+ import sys
3
+ import yt_dlp
4
+ import openai
5
+
6
+ # Fail if no commandline argument is provided
7
+ if len (sys .argv ) < 2 :
8
+ print ("Please provide an video link" )
9
+ exit (1 )
10
+
11
+ video_file_url = sys .argv [1 ]
12
+
13
+ # https://github.com/yt-dlp/yt-dlp#filter-videos
14
+ ydl_opts = {
15
+ 'format' : 'm4a/bestaudio/best' ,
16
+ 'postprocessors' : [{ # Extract audio using ffmpeg
17
+ 'key' : 'FFmpegExtractAudio' ,
18
+ 'preferredcodec' : 'm4a' ,
19
+ }]
20
+ }
21
+
22
+ audio_file_name = ""
23
+ with yt_dlp .YoutubeDL (ydl_opts ) as ydl :
24
+ info = ydl .extract_info (video_file_url , download = False )
25
+ audio_file_name = ydl .prepare_filename (info )
26
+ ydl .process_info (info ) # starts the download
27
+
28
+ # Check if file exists
29
+ if not os .path .isfile (audio_file_name ):
30
+ print ("File {audio_file_name} does not exist" )
31
+ exit (1 )
32
+
33
+ audio_file = open (audio_file_name , "rb" )
34
+ transcript = openai .Audio .transcribe ("whisper-1" , audio_file )
35
+
36
+ print (transcript )
37
+
38
+ # write transcript to file
39
+ if not os .path .isdir ("whisper_yt" ):
40
+ os .mkdir ("whisper_yt" )
41
+ with open (f"whisper_yt/{ audio_file_name } .txt" , "w" ) as f :
42
+ f .write (transcript ["text" ])
Original file line number Diff line number Diff line change
1
+ import os
2
+ import sys
3
+ import openai
4
+
5
+ # Fail if no commandline argument is provided
6
+ if len (sys .argv ) < 2 :
7
+ print ("Please provide an audio file" )
8
+ exit (1 )
9
+
10
+ audio_file_name = sys .argv [1 ]
11
+
12
+ # Check if file exists
13
+ if not os .path .isfile (audio_file_name ):
14
+ print ("File does not exist" )
15
+ exit (1 )
16
+
17
+ # If file is in ogg format convert it to mp3
18
+ if audio_file_name .endswith (".ogg" ):
19
+ os .system (f"ffmpeg -i '{ audio_file_name } ' -ab 320k '{ audio_file_name } .mp3'" )
20
+ audio_file_name = f"{ audio_file_name } .mp3"
21
+
22
+ audio_file = open (audio_file_name , "rb" )
23
+ transcript = openai .Audio .transcribe ("whisper-1" , audio_file , language = "de" )
24
+ print (transcript )
25
+
26
+ # write transcript to file
27
+ if not os .path .isdir ("whisper_audio" ):
28
+ os .mkdir ("whisper_audio" )
29
+ with open (f"whisper_audio/{ audio_file_name } .txt" , "w" ) as f :
30
+ f .write (transcript ["text" ])
You can’t perform that action at this time.
0 commit comments