File size: 11,978 Bytes
2af1c20
 
 
fe3c57c
393ce04
955f567
 
e074083
955f567
 
fe9dbf9
 
 
 
955f567
e074083
 
 
955f567
e074083
 
955f567
 
 
 
 
fe9dbf9
 
 
 
 
e074083
fe9dbf9
 
 
 
 
 
 
e074083
 
 
955f567
fe9dbf9
 
955f567
 
3d67c2c
 
955f567
 
 
 
 
fe9dbf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955f567
fe9dbf9
 
 
 
 
 
955f567
fe9dbf9
955f567
 
 
 
 
 
3d67c2c
 
955f567
 
 
 
 
 
 
 
 
 
 
 
fe9dbf9
955f567
 
 
fe9dbf9
955f567
 
3d67c2c
 
955f567
 
 
 
 
 
 
fe9dbf9
955f567
8f186bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955f567
 
 
 
 
 
 
 
 
fe9dbf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955f567
 
 
 
 
 
 
3d67c2c
 
955f567
 
 
 
 
8f186bb
 
 
 
 
 
 
 
955f567
8f186bb
 
 
 
 
 
955f567
 
 
 
 
 
 
fe9dbf9
955f567
 
 
 
 
 
 
3d67c2c
 
955f567
 
 
 
 
8f186bb
 
 
 
 
 
 
 
955f567
8f186bb
 
 
 
 
 
955f567
 
 
 
 
 
 
fe9dbf9
955f567
 
4bbdff0
fe9dbf9
955f567
4bbdff0
 
 
955f567
4bbdff0
 
 
955f567
4bbdff0
 
fe9dbf9
4bbdff0
 
 
 
 
8f186bb
955f567
4bbdff0
 
955f567
4bbdff0
 
 
955f567
fe9dbf9
955f567
 
fe9dbf9
 
955f567
 
3d67c2c
 
955f567
 
 
4bbdff0
955f567
4bbdff0
 
 
fe9dbf9
4bbdff0
 
 
 
 
 
 
 
 
955f567
 
 
 
 
 
 
 
fe9dbf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955f567
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import os
import json

# import pytorch_test

import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware

from media_download import YoutubeDownloader
# from transcription import StableWhisper
# from summarizer import Extract_Summary, AudioBookNarration
# from audiobook import AudioBook
from helperfunctions import *


### API Configurations

app = FastAPI()

# Output Directory for Files Storage
output_folder = 'Output'

# Create a context variable to store the contexts for each user
users_context = dict()

# # CORS (Cross-Origin Resource Sharing)
# origins = [
#     "http://localhost",
#     "http://localhost:4200",
# ]

# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],  # origins,
#     allow_credentials=True,
#     allow_methods=["*"],
#     allow_headers=["*"],
# )


### APIs

@app.get("/get_media_metadata")
async def get_media_metadata(request: Request, url: str):

    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1

    # Getting User's Youtube Downloader
    youtube_downloader = YoutubeDownloader(url, output_folder)

    # Getting Youtube Media Info
    media_metadata = youtube_downloader.get_media_metadata()

    # Getting Status
    status = 1 if media_metadata else 0
    
    if status:
        # Storing Info in the context for this user's session
        users_context[user_ip] = dict()
        users_context[user_ip]['downloader'] = youtube_downloader
        # users_context[user_ip]['media_metadata'] = media_metadata
        users_context[user_ip]['url'] = url
    
    return {'status': status, 'media_metadata': media_metadata}


@app.get("/get_media_formats")
async def get_media_formats(request: Request):

    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1
    
    # Downloading Media for User
    media_formats = users_context[user_ip]['downloader'].get_media_formats()
    
    # Getting Status
    status = 1 if media_formats else 0

    if status:
        # Storing Media Info in the context for this user's session
        users_context[user_ip]['media_formats'] = media_formats
    
    return {'status': status, 'media_formats': media_formats}


@app.get("/download_media")
async def download_media(request: Request, media_type: str, media_format: str, media_quality: str):

    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1
    
    # Downloading Media for User
    media_path = users_context[user_ip]['downloader'].download(media_type, media_format, media_quality)
    
    # Getting Status
    status = 1 if media_path else 0

    if status:
        # Storing Media Info in the context for this user's session
        users_context[user_ip]['media_path'] = media_path
        users_context[user_ip]['media_type'] = media_type
    
    return {'status': status, 'media_path': media_path}


@app.get("/get_transcript")
async def get_transcript(request: Request, subtitle_format: str = 'srt', word_level: bool = False):
    
    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1
    
    # Retrieving the media_path from the context for this user's session
    media_path = users_context[user_ip]['media_path']

    # Checking if the media_type is Video, then extract it's audio
    media_type = users_context[user_ip]['media_type']
    if media_type == 'video':
        media_path = extract_audio(media_path)

    # # Whisper based transcription
    # stable_whisper_transcript = StableWhisper(media_path, output_folder, subtitle_format=subtitle_format, word_level=word_level)
    # transcript = stable_whisper_transcript.generate_transcript()
    # transcript_path = stable_whisper_transcript.save_transcript()

    temp_dir = 'temp'
    if word_level:
        transcript_path = os.path.join(temp_dir, 'word_level_transcript.json')
        with open(transcript_path, "r") as json_file:
            transcript = json.load(json_file)
    else:
        transcript_path = os.path.join(temp_dir, 'sentence_level_transcript.json')
        with open(transcript_path, "r") as json_file:
            transcript = json.load(json_file)
    
    
    # Getting Status
    status = 1 if transcript else 0

    if status:
        # Storing Transcript Info in the context for this user's session
        users_context[user_ip]['transcript'] = transcript
        users_context[user_ip]['transcript_path'] = transcript_path
    
    return {'status': status, "transcript": transcript}


@app.get("/get_translation")
async def get_translation(request: Request, target_language: str = 'en'):
    
    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1
    
    # Retrieving the transcript from the context for this user's session
    transcript = users_context[user_ip]['transcript']

    # # # NLLB based Translation
    # nllb_translator = Translation(transcript, transcript['language'], target_language, 'output_path')
    # translated_transcript = nllb_translator.get_translated_transcript()
    # translated_subtitles = nllb_translator.get_translated_subtitles()

    temp_dir = 'temp'

    translated_transcript_path = os.path.join(temp_dir, 'translated_transcript.txt')
    with open(translated_transcript_path, "r", encoding="utf-8") as f:
        translated_transcript = f.read()

    translated_subtitles_path = os.path.join(temp_dir, 'translated_subtitles.json')
    with open(translated_subtitles_path, "r", encoding="utf-8") as json_file:
        translated_subtitles = json.load(json_file)
    
    # Getting Status
    status = 1 if translated_transcript and translated_subtitles else 0

    if status:
        # Storing Translated Transcript Info in the context for this user's session
        users_context[user_ip]['translated_transcript'] = translated_transcript
        users_context[user_ip]['translated_subtitles'] = translated_subtitles
        # users_context[user_ip]['transcript_path'] = transcript_path
    
    return {'status': status, "transcript": translated_transcript, "subtitles": translated_subtitles}


@app.get("/get_summary")
async def get_summary(request: Request, Summary_type: str, Summary_strategy: str, Target_Person_type: str,
                      Response_length:  str, Writing_style: str, text_input: str = None):
    
    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1

    # Getting Transcript if not provided
    if not text_input:
        text_input = users_context[user_ip]['transcript']
    
    # # Extracting Summary
    # summary_extractor = Extract_Summary(text_input=text_input)
    # output = summary_extractor.define_chain(Summary_type=Summary_type,
    #                                         Summary_strategy=Summary_strategy,
    #                                         Target_Person_type=Target_Person_type,
    #                                         Response_length=Response_length,
    #                                         Writing_style=Writing_style,
    #                                         key_information=False)
    
    temp_dir = 'temp'
    file_path = os.path.join(temp_dir, 'summary.txt')
    with open(file_path, 'r') as file:
        output = file.read()


    # Getting Status
    status = 1 if output else 0

    if status:
        # Storing Summary Info in the context for this user's session
        users_context[user_ip]['summary'] = output
    
    return {'status': status, "summary": output}


@app.get("/get_key_info")
async def get_key_info(request: Request, Summary_type: str, Summary_strategy: str, Target_Person_type: str, 
                       Response_length: str, Writing_style: str, text_input: str = None):
    
    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1

    # Getting Transcript if not provided
    if not text_input:
        text_input = users_context[user_ip]['transcript']
    
    # # Extracting Summary
    # summary_extractor = Extract_Summary(text_input=text_input)
    # output = summary_extractor.define_chain(Summary_type=Summary_type,
    #                                         Summary_strategy=Summary_strategy,
    #                                         Target_Person_type=Target_Person_type,
    #                                         Response_length=Response_length,
    #                                         Writing_style=Writing_style,
    #                                         key_information=True)
    
    temp_dir = 'temp'
    file_path = os.path.join(temp_dir, 'key_info.txt')
    with open(file_path, 'r') as file:
        output = file.read()


    # Getting Status
    status = 1 if output else 0

    if status:
        # Storing Key Info in the context for this user's session
        users_context[user_ip]['key_info'] = output
    
    return {'status': status, "key_info": output}


# @app.get("/get_narration")
# async def get_narration(request: Request, narration_style: str, text_input: str = None):
    
#     # Getting User's IP
#     # user_ip = request.client.host
#     user_ip = 1

#     # Getting Transcript if not provided
#     if not text_input:
#         text_input = users_context[user_ip]['transcript']
    
#     # # Extracting Narration
#     # narrator = AudioBookNarration(text_input=text_input)
#     # output = narrator.define_chain(narration_style=narration_style)

#     temp_dir = 'temp'
#     file_path = os.path.join(temp_dir, 'narration.txt')
#     with open(file_path, 'r') as file:
#         output = file.read()
    

#     # Getting Status
#     status = 1 if output else 0

#     if status:
#         # Storing Narration Info in the context for this user's session
#         users_context[user_ip]['narration'] = output
    
#     return {'status': status, "narration": output}


@app.get("/get_audiobook")
async def get_audiobook(request: Request, output_type : str, narration_style: str, speaker: str = "male", text_input: str = None):

    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1

    # Getting Transcript if not provided
    if not text_input:
        text_input = users_context[user_ip]['transcript']
    
    # # Extracting Narration

    # narrator = AudioBookNarration(text_input=text_input)
    # output = narrator.define_chain(narration_style=narration_style)

    # # Generating Audiobook
    # audiobook = AudioBook(output_folder=output_folder)
    # audio_path = audiobook.generate_audio_from_text(output, speaker=speaker, filename="output_audio")
    
    temp_dir = 'temp'
    file_path = os.path.join(temp_dir, 'narration.txt')

    audio_path = file_path 
    
    # Getting Status
    status = 1 if audio_path else 0

    if status:
        # Storing Audiobook path in the context for this user's session
        users_context[user_ip]['audiobook_path'] = audio_path
    
    return {'status': status, "audiobook_path": audio_path}


@app.get("/get_rendered_video")
async def get_rendered_video(request: Request, subtitles_type: str = 'original'):

    # Getting User's IP
    # user_ip = request.client.host
    user_ip = 1

    # Retrieving the media_path from the context for this user's session
    media_path = users_context[user_ip]['media_path']
    
    # Getting Required Subtitles
    if subtitles_type == 'original':
        subtitles_path = users_context[user_ip]['transcript_path']

    elif subtitles_type == 'translated':

        # Getting Translated Subtitles from the context for this user's session
        translated_subtitles = users_context[user_ip]['translated_subtitles']

        # Saving Translated Subtitles
        subtitles_path = save_translated_subtitles(translated_subtitles, media_path)
    
    # Burning Subtitles & Rendering Video
    rendered_video_path = burn_subtitles(media_path, subtitles_path)

    # Getting Status
    status = 1 if rendered_video_path else 0
    
    return {'status': status, "rendered_video_path": rendered_video_path}



if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)