Spaces:
Sleeping
Sleeping
Added Multiple User Requests Handling
Browse files- main.py +61 -20
- media_download.py +33 -15
- transcription.py +1 -1
main.py
CHANGED
@@ -1,40 +1,81 @@
|
|
1 |
from media_download import *
|
2 |
from transcription import *
|
|
|
3 |
|
4 |
import uvicorn
|
5 |
-
from fastapi import FastAPI
|
6 |
|
7 |
app = FastAPI()
|
8 |
-
output_folder = '
|
9 |
-
|
|
|
|
|
10 |
|
11 |
|
12 |
@app.get("/get_media_info")
|
13 |
-
async def get_media_info(url: str):
|
|
|
|
|
|
|
|
|
|
|
14 |
youtube_downloader = YoutubeDownloader(url, output_folder)
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
@app.get("/download_media")
|
18 |
-
async def download_media(
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
return {"status": status}
|
26 |
|
|
|
27 |
@app.get("/generate_transcript")
|
28 |
-
async def generate_transcript(subtitle_format: str = 'srt', word_level: bool = True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
stable_whisper_transcript = StableWhisper(media_path, output_folder, subtitle_format=subtitle_format, word_level=word_level)
|
30 |
transcript = stable_whisper_transcript.generate_transcript()
|
31 |
-
youtube_transcript.save_transcript()
|
32 |
-
|
33 |
-
|
34 |
-
else
|
35 |
-
|
36 |
-
transcript
|
37 |
return {"status": status, "transcript": transcript}
|
38 |
|
|
|
39 |
if __name__ == "__main__":
|
40 |
-
uvicorn.run(app, host="127.0.0.1", port=8000)
|
|
|
1 |
from media_download import *
|
2 |
from transcription import *
|
3 |
+
import asyncio
|
4 |
|
5 |
import uvicorn
|
6 |
+
from fastapi import FastAPI, Request, Depends
|
7 |
|
8 |
app = FastAPI()
|
9 |
+
output_folder = 'Output'
|
10 |
+
|
11 |
+
# Create a context variable to store the contexts for each user
|
12 |
+
users_context = dict()
|
13 |
|
14 |
|
15 |
@app.get("/get_media_info")
|
16 |
+
async def get_media_info(request: Request, url: str):
|
17 |
+
|
18 |
+
# Getting User's IP
|
19 |
+
user_ip = request.client.host
|
20 |
+
|
21 |
+
# Getting User's Youtube Downloader
|
22 |
youtube_downloader = YoutubeDownloader(url, output_folder)
|
23 |
+
|
24 |
+
# Getting Youtube Media Info
|
25 |
+
media_info = youtube_downloader.get_media_info()
|
26 |
+
|
27 |
+
# Storing Info in the context for this user's session
|
28 |
+
users_context[user_ip] = dict()
|
29 |
+
users_context[user_ip]['downloader'] = youtube_downloader
|
30 |
+
# users_context[user_ip]['media_info'] = media_info
|
31 |
+
users_context[user_ip]['url'] = url
|
32 |
+
|
33 |
+
return media_info
|
34 |
+
|
35 |
|
36 |
@app.get("/download_media")
|
37 |
+
async def download_media(request: Request, media_type: str, media_format: str, media_quality: str):
|
38 |
+
|
39 |
+
# Getting User's IP
|
40 |
+
user_ip = request.client.host
|
41 |
+
|
42 |
+
# Downloading Media for User
|
43 |
+
media_path = users_context[user_ip]['downloader'].download(media_type, media_format, media_quality)
|
44 |
+
|
45 |
+
# Storing Media Info in the context for this user's session
|
46 |
+
users_context[user_ip]['media_path'] = media_path
|
47 |
+
users_context[user_ip]['media_type'] = media_type
|
48 |
+
|
49 |
+
# Getting Status
|
50 |
+
status = 1 if media_path else 0
|
51 |
+
|
52 |
return {"status": status}
|
53 |
|
54 |
+
|
55 |
@app.get("/generate_transcript")
|
56 |
+
async def generate_transcript(request: Request, subtitle_format: str = 'srt', word_level: bool = True):
|
57 |
+
|
58 |
+
# Getting User's IP
|
59 |
+
user_ip = request.client.host
|
60 |
+
|
61 |
+
# Retrieving the media_path from the context for this user's session
|
62 |
+
media_path = users_context[user_ip]['media_path']
|
63 |
+
|
64 |
+
# Checking if the media_type is Video, then extract it's audio
|
65 |
+
media_type = users_context[user_ip]['media_type']
|
66 |
+
if media_type == 'video':
|
67 |
+
media_path = users_context[user_ip]['downloader'].extract_audio(media_path)
|
68 |
+
|
69 |
+
# Whisper based transcription
|
70 |
stable_whisper_transcript = StableWhisper(media_path, output_folder, subtitle_format=subtitle_format, word_level=word_level)
|
71 |
transcript = stable_whisper_transcript.generate_transcript()
|
72 |
+
transcript_path = youtube_transcript.save_transcript()
|
73 |
+
|
74 |
+
# Getting Status
|
75 |
+
status = 1 if transcript else 0
|
76 |
+
|
|
|
77 |
return {"status": status, "transcript": transcript}
|
78 |
|
79 |
+
|
80 |
if __name__ == "__main__":
|
81 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|
media_download.py
CHANGED
@@ -16,7 +16,7 @@ class MediaDownloader(ABC):
|
|
16 |
|
17 |
def __init__(self, url, output_path, start_time=None, end_time=None):
|
18 |
self.url = url
|
19 |
-
self.output_path = output_path
|
20 |
self.start_time = start_time
|
21 |
self.end_time = end_time
|
22 |
self.__create_output_dir()
|
@@ -204,17 +204,13 @@ class YoutubeDownloader(MediaDownloader):
|
|
204 |
"-x", "--audio-format", audio_format,
|
205 |
"--audio-quality", quality,
|
206 |
"-o", output_path,
|
207 |
-
url, "-q"
|
208 |
]
|
209 |
|
210 |
# Running the command using Subprocess
|
211 |
subprocess.run(command)
|
212 |
|
213 |
return output_path
|
214 |
-
# stream = self.streams.filter(file_extension=audio_format, abr=audio_quality).first()
|
215 |
-
# print(stream)
|
216 |
-
# audio_path = stream.download(output_path=self.output_path, filename=f"{self.title}.{audio_format}")
|
217 |
-
# return audio_path
|
218 |
|
219 |
def _download_video(self, video_format, video_quality):
|
220 |
'''
|
@@ -366,12 +362,34 @@ class YoutubeDownloader(MediaDownloader):
|
|
366 |
'formats': self.media_formats_dict
|
367 |
}
|
368 |
return media_info
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def __init__(self, url, output_path, start_time=None, end_time=None):
|
18 |
self.url = url
|
19 |
+
self.output_path = os.path.join(os.getcwd(), output_path)
|
20 |
self.start_time = start_time
|
21 |
self.end_time = end_time
|
22 |
self.__create_output_dir()
|
|
|
204 |
"-x", "--audio-format", audio_format,
|
205 |
"--audio-quality", quality,
|
206 |
"-o", output_path,
|
207 |
+
self.url, "-q"
|
208 |
]
|
209 |
|
210 |
# Running the command using Subprocess
|
211 |
subprocess.run(command)
|
212 |
|
213 |
return output_path
|
|
|
|
|
|
|
|
|
214 |
|
215 |
def _download_video(self, video_format, video_quality):
|
216 |
'''
|
|
|
362 |
'formats': self.media_formats_dict
|
363 |
}
|
364 |
return media_info
|
365 |
+
|
366 |
+
@staticmethod
|
367 |
+
def extract_audio(video_path):
|
368 |
+
"""
|
369 |
+
Extract audio from a video file (MP4 or WebM) and save it as an MP3 file using ffmpeg.
|
370 |
+
|
371 |
+
Args:
|
372 |
+
video_path (str): Path to the input video file.
|
373 |
+
|
374 |
+
Returns:
|
375 |
+
bool: True if extraction is successful, False otherwise.
|
376 |
+
"""
|
377 |
+
try:
|
378 |
+
# Determine the file format (MP4 or WebM) based on the file extension
|
379 |
+
filename, extension = os.path.splitext(video_path)
|
380 |
+
|
381 |
+
# Extracted audio path
|
382 |
+
audio_path = filename + '.mp3'
|
383 |
+
|
384 |
+
# Choose the appropriate codec for the output audio format (MP3)
|
385 |
+
audio_codec = "libmp3lame" if extension.lower() in (".mp4", ".webm") else "mp3"
|
386 |
+
|
387 |
+
# Run the ffmpeg command to extract audio
|
388 |
+
subprocess.run(["ffmpeg", "-i", video_path, "-vn", "-acodec",
|
389 |
+
audio_codec, audio_path, '-loglevel', 'quiet'], check=True)
|
390 |
+
|
391 |
+
|
392 |
+
return audio_path
|
393 |
+
|
394 |
+
except subprocess.CalledProcessError as e:
|
395 |
+
print(f"Error: {e}")
|
transcription.py
CHANGED
@@ -25,7 +25,7 @@ class Transcription(ABC):
|
|
25 |
|
26 |
def __init__(self, media_path, output_path, subtitle_format):
|
27 |
self.media_path = media_path
|
28 |
-
self.output_path = output_path
|
29 |
self.filename = os.path.splitext(media_path)[0]
|
30 |
self.subtitle_format = subtitle_format
|
31 |
|
|
|
25 |
|
26 |
def __init__(self, media_path, output_path, subtitle_format):
|
27 |
self.media_path = media_path
|
28 |
+
self.output_path = os.path.join(os.getcwd(), output_path)
|
29 |
self.filename = os.path.splitext(media_path)[0]
|
30 |
self.subtitle_format = subtitle_format
|
31 |
|