Spaces:
Runtime error
Runtime error
Added S3 Functionality
Browse files- config.py +10 -0
- main.py +81 -20
- s3_handler.py +71 -0
- transcription.py +10 -0
config.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AWS S3 Credentials
|
2 |
+
AWS_ACCESS_KEY_ID = 'AKIA2YB4Z26VQCIW3DHO' #'AKIA2YB4Z26VTD4PH6BX'
|
3 |
+
AWS_SECRET_ACCESS_KEY = 'VgWGPIsJpdddRPmKALIpEvr+6y+RLJvtkihg2fvm' #'il0SbQjGbTrBhAkwSC2zvVCf7GqPpANmCoOold//'
|
4 |
+
BUCKET_NAME = 'wt-video-dl'
|
5 |
+
AWS_REGION = 'us-east-2'
|
6 |
+
S3_OBJ_BASE_URL = f"https://{BUCKET_NAME}.s3.{AWS_REGION}.amazonaws.com"
|
7 |
+
|
8 |
+
# OpenAI Credentials
|
9 |
+
OPENAI_API_KEY = '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'
|
10 |
+
OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions'
|
main.py
CHANGED
@@ -8,14 +8,17 @@ from contextlib import asynccontextmanager
|
|
8 |
|
9 |
from models import load_models
|
10 |
from helperfunctions import *
|
|
|
11 |
from media_download import YoutubeDownloader
|
12 |
# from transcription import StableWhisper
|
13 |
# from summarizer import Extract_Summary, AudioBookNarration
|
14 |
# from audiobook import AudioBook
|
15 |
|
16 |
-
# global MODELS
|
17 |
|
18 |
|
|
|
|
|
|
|
19 |
### API Configurations
|
20 |
|
21 |
# Context Manager for FastAPI Start/Shutdown
|
@@ -43,6 +46,9 @@ app = FastAPI(lifespan=lifespan)
|
|
43 |
# Output Directory for Files Storage
|
44 |
output_folder = 'Output'
|
45 |
|
|
|
|
|
|
|
46 |
# Create a context variable to store the contexts for each user
|
47 |
users_context = dict()
|
48 |
|
@@ -70,12 +76,23 @@ async def get_media_metadata(request: Request, url: str):
|
|
70 |
user_ip = request.client.host
|
71 |
user_id = generate_uuid(user_ip, url)
|
72 |
|
|
|
|
|
|
|
73 |
# Getting User's Youtube Downloader
|
74 |
-
youtube_downloader = YoutubeDownloader(url,
|
75 |
|
76 |
# Getting Youtube Media Info
|
77 |
media_metadata = youtube_downloader.get_media_metadata()
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Getting Status
|
80 |
status = 1 if media_metadata else 0
|
81 |
|
@@ -86,14 +103,25 @@ async def get_media_metadata(request: Request, url: str):
|
|
86 |
# users_context[user_id]['media_metadata'] = media_metadata
|
87 |
users_context[user_id]['url'] = url
|
88 |
|
89 |
-
return {'status': status, 'user_id': user_id, 'media_metadata': media_metadata}
|
90 |
|
91 |
|
92 |
@app.get("/get_media_formats")
|
93 |
async def get_media_formats(user_id: str):
|
94 |
|
95 |
-
#
|
96 |
media_formats = users_context[user_id]['downloader'].get_media_formats()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# Getting Status
|
99 |
status = 1 if media_formats else 0
|
@@ -102,7 +130,7 @@ async def get_media_formats(user_id: str):
|
|
102 |
# Storing Media Info in the context for this user's session
|
103 |
users_context[user_id]['media_formats'] = media_formats
|
104 |
|
105 |
-
return {'status': status, 'media_formats': media_formats}
|
106 |
|
107 |
|
108 |
@app.get("/download_media")
|
@@ -110,6 +138,10 @@ async def download_media(user_id: str, media_type: str, media_format: str, media
|
|
110 |
|
111 |
# Downloading Media for User
|
112 |
media_path = users_context[user_id]['downloader'].download(media_type, media_format, media_quality)
|
|
|
|
|
|
|
|
|
113 |
|
114 |
# Getting Status
|
115 |
status = 1 if media_path else 0
|
@@ -119,7 +151,7 @@ async def download_media(user_id: str, media_type: str, media_format: str, media
|
|
119 |
users_context[user_id]['media_path'] = media_path
|
120 |
users_context[user_id]['media_type'] = media_type
|
121 |
|
122 |
-
return {'status': status, 'media_path':
|
123 |
|
124 |
|
125 |
@app.get("/get_transcript")
|
@@ -141,12 +173,12 @@ async def get_transcript(user_id: str, subtitle_format: str = 'srt', word_level:
|
|
141 |
# Downloading Audio for Transcription
|
142 |
media_path = users_context[user_id]['downloader'].download('audio', 'mp3', '128kbps')
|
143 |
|
144 |
-
# Retrieving Audio
|
145 |
-
|
146 |
# # Whisper based transcription
|
147 |
-
#
|
|
|
148 |
# transcript = stable_whisper_transcript.generate_transcript()
|
149 |
# transcript_path = stable_whisper_transcript.save_transcript()
|
|
|
150 |
|
151 |
temp_dir = 'temp'
|
152 |
if word_level:
|
@@ -172,12 +204,19 @@ async def get_transcript(user_id: str, subtitle_format: str = 'srt', word_level:
|
|
172 |
|
173 |
@app.get("/get_translation")
|
174 |
async def get_translation(user_id: str, target_language: str = 'en'):
|
|
|
|
|
|
|
175 |
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
178 |
|
179 |
-
# #
|
180 |
-
#
|
|
|
181 |
# translated_transcript = nllb_translator.get_translated_transcript()
|
182 |
# translated_subtitles = nllb_translator.get_translated_subtitles()
|
183 |
|
@@ -207,8 +246,14 @@ async def get_translation(user_id: str, target_language: str = 'en'):
|
|
207 |
async def get_summary(user_id: str, Summary_type: str, Summary_strategy: str, Target_Person_type: str,
|
208 |
Response_length: str, Writing_style: str):
|
209 |
|
210 |
-
#
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
# # Extracting Summary
|
214 |
# summary_extractor = Extract_Summary(text_input=text_input)
|
@@ -239,8 +284,14 @@ async def get_summary(user_id: str, Summary_type: str, Summary_strategy: str, Ta
|
|
239 |
async def get_key_info(user_id: str, Summary_type: str, Summary_strategy: str, Target_Person_type: str,
|
240 |
Response_length: str, Writing_style: str):
|
241 |
|
242 |
-
#
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
# # Extracting Summary
|
246 |
# summary_extractor = Extract_Summary(text_input=text_input)
|
@@ -271,8 +322,14 @@ async def get_key_info(user_id: str, Summary_type: str, Summary_strategy: str, T
|
|
271 |
async def get_audiobook(user_id: str, narration_style: str, speaker: str = "male",
|
272 |
audio_format: str = "mp3", audio_quality: str = "128kbps"):
|
273 |
|
274 |
-
#
|
275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
|
277 |
# # Extracting Narration
|
278 |
|
@@ -325,10 +382,14 @@ async def get_rendered_video(user_id: str, video_format: str, video_quality: str
|
|
325 |
# Burning Subtitles & Rendering Video
|
326 |
rendered_video_path = burn_subtitles(media_path, subtitles_path)
|
327 |
|
|
|
|
|
|
|
|
|
328 |
# Getting Status
|
329 |
status = 1 if rendered_video_path else 0
|
330 |
|
331 |
-
return {'status': status, "rendered_video_path":
|
332 |
|
333 |
|
334 |
|
|
|
8 |
|
9 |
from models import load_models
|
10 |
from helperfunctions import *
|
11 |
+
from s3_handler import S3Handler
|
12 |
from media_download import YoutubeDownloader
|
13 |
# from transcription import StableWhisper
|
14 |
# from summarizer import Extract_Summary, AudioBookNarration
|
15 |
# from audiobook import AudioBook
|
16 |
|
|
|
17 |
|
18 |
|
19 |
+
# For Storing Models
|
20 |
+
global MODELS
|
21 |
+
|
22 |
### API Configurations
|
23 |
|
24 |
# Context Manager for FastAPI Start/Shutdown
|
|
|
46 |
# Output Directory for Files Storage
|
47 |
output_folder = 'Output'
|
48 |
|
49 |
+
# S3 Handler
|
50 |
+
s3 = S3Handler()
|
51 |
+
|
52 |
# Create a context variable to store the contexts for each user
|
53 |
users_context = dict()
|
54 |
|
|
|
76 |
user_ip = request.client.host
|
77 |
user_id = generate_uuid(user_ip, url)
|
78 |
|
79 |
+
# User Folder Path
|
80 |
+
user_folder_path = os.path.join(output_folder, user_id)
|
81 |
+
|
82 |
# Getting User's Youtube Downloader
|
83 |
+
youtube_downloader = YoutubeDownloader(url, user_folder_path)
|
84 |
|
85 |
# Getting Youtube Media Info
|
86 |
media_metadata = youtube_downloader.get_media_metadata()
|
87 |
|
88 |
+
# Storing User's Media Metadata to Directory
|
89 |
+
media_metadata_path = os.path.join(user_folder_path, 'media_metadata.json')
|
90 |
+
with open(media_metadata_path, "w") as outfile:
|
91 |
+
json.dump(media_metadata, outfile)
|
92 |
+
|
93 |
+
# Storing User's Media Metadata to S3
|
94 |
+
s3_path = s3.upload_file(user_id, 'media_metadata.json', media_metadata_path)
|
95 |
+
|
96 |
# Getting Status
|
97 |
status = 1 if media_metadata else 0
|
98 |
|
|
|
103 |
# users_context[user_id]['media_metadata'] = media_metadata
|
104 |
users_context[user_id]['url'] = url
|
105 |
|
106 |
+
return {'status': status, 'user_id': user_id, 'media_metadata': media_metadata, 'media_metadata_path': s3_path}
|
107 |
|
108 |
|
109 |
@app.get("/get_media_formats")
|
110 |
async def get_media_formats(user_id: str):
|
111 |
|
112 |
+
# Getting Media Formats for User
|
113 |
media_formats = users_context[user_id]['downloader'].get_media_formats()
|
114 |
+
|
115 |
+
# User Folder Path
|
116 |
+
user_folder_path = os.path.join(output_folder, user_id)
|
117 |
+
|
118 |
+
# Storing User's Media Formats to Directory
|
119 |
+
media_formats_path = os.path.join(user_folder_path, 'media_formats.json')
|
120 |
+
with open(media_formats_path, "w") as outfile:
|
121 |
+
json.dump(media_formats, outfile)
|
122 |
+
|
123 |
+
# Storing User's Media Formats to S3
|
124 |
+
s3_path = s3.upload_file(user_id, 'media_formats.json', media_formats_path)
|
125 |
|
126 |
# Getting Status
|
127 |
status = 1 if media_formats else 0
|
|
|
130 |
# Storing Media Info in the context for this user's session
|
131 |
users_context[user_id]['media_formats'] = media_formats
|
132 |
|
133 |
+
return {'status': status, 'media_formats': media_formats, 'media_formats_path': s3_path}
|
134 |
|
135 |
|
136 |
@app.get("/download_media")
|
|
|
138 |
|
139 |
# Downloading Media for User
|
140 |
media_path = users_context[user_id]['downloader'].download(media_type, media_format, media_quality)
|
141 |
+
|
142 |
+
# Storing User's Downloaded Media to S3
|
143 |
+
media_file = f"{media_type.lower()}_{media_quality.lower()}.{media_format.lower()}"
|
144 |
+
s3_path = s3.upload_file(user_id, media_file, media_path)
|
145 |
|
146 |
# Getting Status
|
147 |
status = 1 if media_path else 0
|
|
|
151 |
users_context[user_id]['media_path'] = media_path
|
152 |
users_context[user_id]['media_type'] = media_type
|
153 |
|
154 |
+
return {'status': status, 'media_path': s3_path}
|
155 |
|
156 |
|
157 |
@app.get("/get_transcript")
|
|
|
173 |
# Downloading Audio for Transcription
|
174 |
media_path = users_context[user_id]['downloader'].download('audio', 'mp3', '128kbps')
|
175 |
|
|
|
|
|
176 |
# # Whisper based transcription
|
177 |
+
# user_folder_path = os.path.join(output_folder, user_id)
|
178 |
+
# stable_whisper_transcript = StableWhisper(media_path, user_folder_path, subtitle_format=subtitle_format, word_level=word_level)
|
179 |
# transcript = stable_whisper_transcript.generate_transcript()
|
180 |
# transcript_path = stable_whisper_transcript.save_transcript()
|
181 |
+
# subtitles_path = stable_whisper_transcript.save_subtitles()
|
182 |
|
183 |
temp_dir = 'temp'
|
184 |
if word_level:
|
|
|
204 |
|
205 |
@app.get("/get_translation")
|
206 |
async def get_translation(user_id: str, target_language: str = 'en'):
|
207 |
+
|
208 |
+
# If Transcript Available
|
209 |
+
if 'transcript' in users_context[user_id].keys():
|
210 |
|
211 |
+
# Retrieving the transcript from the context for this user's session
|
212 |
+
transcript = users_context[user_id]['transcript']
|
213 |
+
|
214 |
+
else:
|
215 |
+
return {'status': 0, 'message': 'Transcript not generated yet'}
|
216 |
|
217 |
+
# # NLLB based Translation
|
218 |
+
# user_folder_path = os.path.join(output_folder, user_id)
|
219 |
+
# nllb_translator = Translation(transcript, transcript['language'], target_language, user_folder_path)
|
220 |
# translated_transcript = nllb_translator.get_translated_transcript()
|
221 |
# translated_subtitles = nllb_translator.get_translated_subtitles()
|
222 |
|
|
|
246 |
async def get_summary(user_id: str, Summary_type: str, Summary_strategy: str, Target_Person_type: str,
|
247 |
Response_length: str, Writing_style: str):
|
248 |
|
249 |
+
# If Transcript Available
|
250 |
+
if 'transcript' in users_context[user_id].keys():
|
251 |
+
|
252 |
+
# Retrieving the transcript from the context for this user's session
|
253 |
+
text_input = users_context[user_id]['transcript']
|
254 |
+
|
255 |
+
else:
|
256 |
+
return {'status': 0, 'message': 'Transcript not generated yet'}
|
257 |
|
258 |
# # Extracting Summary
|
259 |
# summary_extractor = Extract_Summary(text_input=text_input)
|
|
|
284 |
async def get_key_info(user_id: str, Summary_type: str, Summary_strategy: str, Target_Person_type: str,
|
285 |
Response_length: str, Writing_style: str):
|
286 |
|
287 |
+
# If Transcript Available
|
288 |
+
if 'transcript' in users_context[user_id].keys():
|
289 |
+
|
290 |
+
# Retrieving the transcript from the context for this user's session
|
291 |
+
text_input = users_context[user_id]['transcript']
|
292 |
+
|
293 |
+
else:
|
294 |
+
return {'status': 0, 'message': 'Transcript not generated yet'}
|
295 |
|
296 |
# # Extracting Summary
|
297 |
# summary_extractor = Extract_Summary(text_input=text_input)
|
|
|
322 |
async def get_audiobook(user_id: str, narration_style: str, speaker: str = "male",
|
323 |
audio_format: str = "mp3", audio_quality: str = "128kbps"):
|
324 |
|
325 |
+
# If Transcript Available
|
326 |
+
if 'transcript' in users_context[user_id].keys():
|
327 |
+
|
328 |
+
# Retrieving the transcript from the context for this user's session
|
329 |
+
text_input = users_context[user_id]['transcript']
|
330 |
+
|
331 |
+
else:
|
332 |
+
return {'status': 0, 'message': 'Transcript not generated yet'}
|
333 |
|
334 |
# # Extracting Narration
|
335 |
|
|
|
382 |
# Burning Subtitles & Rendering Video
|
383 |
rendered_video_path = burn_subtitles(media_path, subtitles_path)
|
384 |
|
385 |
+
# Storing User's Rendered Video to S3
|
386 |
+
media_file = f"subs_video_{video_quality.lower()}.{video_format.lower()}"
|
387 |
+
s3_path = s3.upload_file(user_id, media_file, media_path)
|
388 |
+
|
389 |
# Getting Status
|
390 |
status = 1 if rendered_video_path else 0
|
391 |
|
392 |
+
return {'status': status, "rendered_video_path": s3_path}
|
393 |
|
394 |
|
395 |
|
s3_handler.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import boto3
|
3 |
+
from botocore.exceptions import ClientError
|
4 |
+
|
5 |
+
from config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, BUCKET_NAME, S3_OBJ_BASE_URL
|
6 |
+
|
7 |
+
|
8 |
+
class S3Handler:
|
9 |
+
|
10 |
+
def __init__(self):
|
11 |
+
"""
|
12 |
+
Establishes connection with the required s3 bucket
|
13 |
+
"""
|
14 |
+
try:
|
15 |
+
self.s3 = boto3.client(
|
16 |
+
's3',
|
17 |
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
18 |
+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
|
19 |
+
)
|
20 |
+
except ClientError as e:
|
21 |
+
print(f"Failed to connect to S3: {e}")
|
22 |
+
|
23 |
+
def upload_file(self, user_id, s3_path, file_path, **kwargs):
|
24 |
+
"""
|
25 |
+
Uploads file to the s3
|
26 |
+
"""
|
27 |
+
|
28 |
+
try:
|
29 |
+
# Filename: File path to upload
|
30 |
+
# Bucket: Name of the bucket to upload the file.
|
31 |
+
# Key: Name of the key to upload to S3.
|
32 |
+
s3_user_path = user_id + '/' + s3_path
|
33 |
+
self.s3.upload_file(
|
34 |
+
Filename=file_path,
|
35 |
+
Bucket=BUCKET_NAME,
|
36 |
+
Key=s3_user_path,
|
37 |
+
# ExtraArgs={'ACL': 'public-read'}
|
38 |
+
)
|
39 |
+
print(f"File uploaded to {BUCKET_NAME} as {s3_path}")
|
40 |
+
s3_obj_path = S3_OBJ_BASE_URL + '/' + user_id + '/' + s3_path
|
41 |
+
return s3_obj_path
|
42 |
+
except ClientError as e:
|
43 |
+
print(f"Failed to upload file to S3: {e}")
|
44 |
+
|
45 |
+
def download_file(self, user_id, s3_path, file_path, **kwargs):
|
46 |
+
"""
|
47 |
+
Downloads file from s3
|
48 |
+
"""
|
49 |
+
|
50 |
+
try:
|
51 |
+
# Filename: Local File path to download to
|
52 |
+
# Bucket: Name of the bucket to download the file from
|
53 |
+
# Key: Name of the file to download from the bucket
|
54 |
+
s3_user_path = user_id + '/' + s3_path
|
55 |
+
self.s3.download_file(
|
56 |
+
Filename=file_path,
|
57 |
+
Bucket=BUCKET_NAME,
|
58 |
+
Key=s3_user_path
|
59 |
+
)
|
60 |
+
print(f"File downloaded from {BUCKET_NAME} as {file_path}")
|
61 |
+
s3_obj_path = S3_OBJ_BASE_URL + '/' + user_id + '/' + s3_path
|
62 |
+
return s3_obj_path
|
63 |
+
except ClientError as e:
|
64 |
+
print(f"Failed to download file from S3: {e}")
|
65 |
+
|
66 |
+
|
67 |
+
# s3_path = "sample_video_srt_2.mp4"
|
68 |
+
# file_path = "Output/video_1.mp4"
|
69 |
+
|
70 |
+
# s3 = S3Handler()
|
71 |
+
# s3.upload_file(s3_path, file_path, user_id="uzair")
|
transcription.py
CHANGED
@@ -212,6 +212,16 @@ class StableWhisper(Whisper):
|
|
212 |
'''
|
213 |
Writes the transcript into file
|
214 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
# Writing according to the Format
|
216 |
file_path = f'{self.filename}.{self.subtitle_format}'
|
217 |
if self.subtitle_format == 'ass':
|
|
|
212 |
'''
|
213 |
Writes the transcript into file
|
214 |
'''
|
215 |
+
# Writing to TXT file in UTF-8 format
|
216 |
+
file_path = f'{self.filename}.txt'
|
217 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
218 |
+
file.write(self.text)
|
219 |
+
return file_path
|
220 |
+
|
221 |
+
def save_subtitles(self):
|
222 |
+
'''
|
223 |
+
Writes the subtitles into file
|
224 |
+
'''
|
225 |
# Writing according to the Format
|
226 |
file_path = f'{self.filename}.{self.subtitle_format}'
|
227 |
if self.subtitle_format == 'ass':
|