Spaces:
Sleeping
Sleeping
JiaenLiu
commited on
Commit
•
147a645
1
Parent(s):
ae0ed1b
small fix
Browse filesFormer-commit-id: 400a737586a90592188ba1fb899464599925400e
- README.md +8 -2
- pipeline.py +22 -18
README.md
CHANGED
@@ -8,11 +8,11 @@ pip install -r requirement.txt
|
|
8 |
|
9 |
## Usage
|
10 |
```
|
11 |
-
usage: pipeline.py [-h] [--link LINK] [--local_path LOCAL_PATH] [--download DOWNLOAD] [--result RESULT] [--video_name VIDEO_NAME]
|
12 |
|
13 |
quick start:
|
14 |
|
15 |
-
example online: python3 pipeline.py --link https://www.youtube.com/watch?v=
|
16 |
|
17 |
example offline: python3 pipeline.py --local_path test_translation.m4a --result ./results --video_name test_translation
|
18 |
|
@@ -27,4 +27,10 @@ options:
|
|
27 |
--result RESULT translate result path
|
28 |
--video_name VIDEO_NAME
|
29 |
video name
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
```
|
|
|
8 |
|
9 |
## Usage
|
10 |
```
|
11 |
+
usage: pipeline.py [-h] [--link LINK] [--local_path LOCAL_PATH] [--download DOWNLOAD] [--result RESULT] [--video_name VIDEO_NAME] [--model_name]
|
12 |
|
13 |
quick start:
|
14 |
|
15 |
+
example online: python3 pipeline.py --link https://www.youtube.com/watch?v=61c4dn6851g --download ./downloads --result ./results --video_name SO_I_CHOSE_RANDOM
|
16 |
|
17 |
example offline: python3 pipeline.py --local_path test_translation.m4a --result ./results --video_name test_translation
|
18 |
|
|
|
27 |
--result RESULT translate result path
|
28 |
--video_name VIDEO_NAME
|
29 |
video name
|
30 |
+
--model_name MODEL_NAME
|
31 |
+
model name
|
32 |
+
|
33 |
+
if you cannot download youtube video, please follow the link below.
|
34 |
+
https://github.com/pytube/pytube/issues/1498
|
35 |
+
|
36 |
```
|
pipeline.py
CHANGED
@@ -4,8 +4,6 @@ import argparse
|
|
4 |
import os
|
5 |
import io
|
6 |
|
7 |
-
|
8 |
-
|
9 |
parser = argparse.ArgumentParser()
|
10 |
parser.add_argument("--link", help="youtube video link here", default=None, type=str, required=False)
|
11 |
parser.add_argument("--local_path", help="local video path here", default=None, type=str, required=False)
|
@@ -13,40 +11,46 @@ parser.add_argument("--text_file", help="text file path here", default=None, typ
|
|
13 |
parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
|
14 |
parser.add_argument("--result", help="translate result path", default='./results', type=str, required=False)
|
15 |
parser.add_argument("--video_name", help="video name", default='placeholder', type=str, required=False)
|
16 |
-
parser.add_argument("--model_name", help="model name only support text-davinci-003 and gpt-3.5-turbo",
|
17 |
args = parser.parse_args()
|
18 |
|
19 |
if args.link is None and args.local_path is None and args.text_file is None:
|
20 |
print("need video source or text file")
|
21 |
exit()
|
22 |
|
|
|
23 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
24 |
DOWNLOAD_PATH = args.download
|
25 |
RESULT_PATH = args.result
|
26 |
VIDEO_NAME = args.video_name
|
27 |
-
n_threshold = 1000 # Token limit for the GPT-3.5 model
|
28 |
-
# model_name = "text-davinci-003" # replace this to our own fintune model
|
29 |
model_name = args.model_name
|
30 |
-
# model_name = "davinci"
|
31 |
|
32 |
# get source audio
|
33 |
if args.link is not None and args.local_path is None:
|
34 |
# Download audio from YouTube
|
35 |
video_link = args.link
|
|
|
|
|
36 |
try:
|
37 |
video = YouTube(video_link)
|
38 |
audio = video.streams.filter(only_audio=True, file_extension='mp4').first()
|
39 |
-
audio
|
40 |
-
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
print("Connection Error")
|
43 |
-
print(e)
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
elif args.local_path is not None:
|
47 |
# Read from local
|
48 |
-
audio_file= open(args.local_path, "rb")
|
49 |
-
|
50 |
|
51 |
|
52 |
# Instead of using the script_en variable directly, we'll use script_input
|
@@ -68,7 +72,7 @@ else:
|
|
68 |
script_input = script_en
|
69 |
|
70 |
# Split the video script by sentences and create chunks within the token limit
|
71 |
-
n_threshold =
|
72 |
script_split = script_input.split('.')
|
73 |
|
74 |
script_arr = []
|
@@ -93,11 +97,12 @@ for s in script_arr:
|
|
93 |
{"role": "system", "content": "You are a helpful assistant that translates English to Chinese and have decent background in starcraft2."},
|
94 |
{"role": "user", "content": 'Translate the following English text to Chinese: "{}"'.format(s)}
|
95 |
],
|
96 |
-
temperature=0.
|
97 |
)
|
98 |
with open(f"{RESULT_PATH}/{VIDEO_NAME}_zh.txt", 'a+') as f:
|
99 |
f.write(response['choices'][0]['message']['content'].strip())
|
100 |
-
|
|
|
101 |
if model_name == "text-davinci-003":
|
102 |
prompt = f"Please help me translate this into Chinese:\n\n{s}\n\n"
|
103 |
print(prompt)
|
@@ -112,5 +117,4 @@ for s in script_arr:
|
|
112 |
)
|
113 |
|
114 |
with open(f"{RESULT_PATH}/{VIDEO_NAME}_zh.txt", 'a+') as f:
|
115 |
-
f.write(response['choices'][0]['text'].strip())
|
116 |
-
f.write('\n')
|
|
|
4 |
import os
|
5 |
import io
|
6 |
|
|
|
|
|
7 |
parser = argparse.ArgumentParser()
|
8 |
parser.add_argument("--link", help="youtube video link here", default=None, type=str, required=False)
|
9 |
parser.add_argument("--local_path", help="local video path here", default=None, type=str, required=False)
|
|
|
11 |
parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
|
12 |
parser.add_argument("--result", help="translate result path", default='./results', type=str, required=False)
|
13 |
parser.add_argument("--video_name", help="video name", default='placeholder', type=str, required=False)
|
14 |
+
parser.add_argument("--model_name", help="model name only support text-davinci-003 and gpt-3.5-turbo", type=str, required=False, default="gpt-3.5-turbo")
|
15 |
args = parser.parse_args()
|
16 |
|
17 |
if args.link is None and args.local_path is None and args.text_file is None:
|
18 |
print("need video source or text file")
|
19 |
exit()
|
20 |
|
21 |
+
# set openai api key
|
22 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
23 |
DOWNLOAD_PATH = args.download
|
24 |
RESULT_PATH = args.result
|
25 |
VIDEO_NAME = args.video_name
|
|
|
|
|
26 |
model_name = args.model_name
|
|
|
27 |
|
28 |
# get source audio
|
29 |
if args.link is not None and args.local_path is None:
|
30 |
# Download audio from YouTube
|
31 |
video_link = args.link
|
32 |
+
video = None
|
33 |
+
audio = None
|
34 |
try:
|
35 |
video = YouTube(video_link)
|
36 |
audio = video.streams.filter(only_audio=True, file_extension='mp4').first()
|
37 |
+
if audio:
|
38 |
+
audio.download(DOWNLOAD_PATH)
|
39 |
+
print('Download Completed!')
|
40 |
+
else:
|
41 |
+
print("Error: Audio stream not found")
|
42 |
except Exception as e:
|
43 |
print("Connection Error")
|
44 |
+
print(e)
|
45 |
+
if audio:
|
46 |
+
audio_file = open('{}/{}'.format(DOWNLOAD_PATH, audio.default_filename), "rb")
|
47 |
+
VIDEO_NAME = audio.default_filename.split('.')[0]
|
48 |
+
else:
|
49 |
+
print("Error: Unable to download audio from the YouTube video")
|
50 |
+
exit()
|
51 |
elif args.local_path is not None:
|
52 |
# Read from local
|
53 |
+
audio_file = open(args.local_path, "rb")
|
|
|
54 |
|
55 |
|
56 |
# Instead of using the script_en variable directly, we'll use script_input
|
|
|
72 |
script_input = script_en
|
73 |
|
74 |
# Split the video script by sentences and create chunks within the token limit
|
75 |
+
n_threshold = 1500 # Token limit for the GPT-3 model
|
76 |
script_split = script_input.split('.')
|
77 |
|
78 |
script_arr = []
|
|
|
97 |
{"role": "system", "content": "You are a helpful assistant that translates English to Chinese and have decent background in starcraft2."},
|
98 |
{"role": "user", "content": 'Translate the following English text to Chinese: "{}"'.format(s)}
|
99 |
],
|
100 |
+
temperature=0.15
|
101 |
)
|
102 |
with open(f"{RESULT_PATH}/{VIDEO_NAME}_zh.txt", 'a+') as f:
|
103 |
f.write(response['choices'][0]['message']['content'].strip())
|
104 |
+
|
105 |
+
# using davinci model
|
106 |
if model_name == "text-davinci-003":
|
107 |
prompt = f"Please help me translate this into Chinese:\n\n{s}\n\n"
|
108 |
print(prompt)
|
|
|
117 |
)
|
118 |
|
119 |
with open(f"{RESULT_PATH}/{VIDEO_NAME}_zh.txt", 'a+') as f:
|
120 |
+
f.write(response['choices'][0]['text'].strip())
|
|