Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,81 @@
|
|
1 |
-
import whisper
|
2 |
-
from pytube import YouTube
|
3 |
import gradio as gr
|
|
|
|
|
4 |
import os
|
5 |
-
import
|
6 |
import logging
|
7 |
|
8 |
-
logging
|
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 |
else:
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import yt_dlp as yt
|
3 |
+
import whisper
|
4 |
import os
|
5 |
+
import torch
|
6 |
import logging
|
7 |
|
8 |
+
# Set up logging
|
9 |
+
logging.basicConfig(filename='transcription_logs.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
10 |
+
|
11 |
+
# Create a temporary download folder if it doesn't exist
|
12 |
+
temp_download_dir = os.path.join(os.getcwd(), "temp_download")
|
13 |
+
os.makedirs(temp_download_dir, exist_ok=True)
|
14 |
+
|
15 |
+
# Function to download audio from the given URL
|
16 |
+
def download_audio(url):
|
17 |
+
ydl_opts = {
|
18 |
+
'format': 'bestaudio/best',
|
19 |
+
'outtmpl': os.path.join(temp_download_dir, '%(title)s.%(ext)s'),
|
20 |
+
}
|
21 |
+
with yt.YoutubeDL(ydl_opts) as ydl:
|
22 |
+
info_dict = ydl.extract_info(url, download=True)
|
23 |
+
downloaded_file = ydl.prepare_filename(info_dict)
|
24 |
+
# Generate a new file name by replacing spaces with underscores
|
25 |
+
new_filename = os.path.join(temp_download_dir, os.path.basename(downloaded_file).replace(" ", "_"))
|
26 |
+
# Check if the new file name exists and create a unique name if necessary
|
27 |
+
base, extension = os.path.splitext(new_filename)
|
28 |
+
counter = 1
|
29 |
+
while os.path.exists(new_filename):
|
30 |
+
new_filename = f"{base}_{counter}{extension}"
|
31 |
+
counter += 1
|
32 |
+
# Rename the file
|
33 |
+
os.rename(downloaded_file, new_filename)
|
34 |
+
if os.path.exists(new_filename):
|
35 |
+
return new_filename
|
36 |
+
else:
|
37 |
+
raise Exception("Failed to download and rename audio file.")
|
38 |
+
|
39 |
+
# Function to transcribe audio to SRT format
|
40 |
+
def transcribe_to_srt(file_path):
|
41 |
+
if torch.cuda.is_available():
|
42 |
+
model = whisper.load_model("medium", device="cuda")
|
43 |
else:
|
44 |
+
model = whisper.load_model("medium")
|
45 |
+
|
46 |
+
result = model.transcribe(file_path)
|
47 |
+
|
48 |
+
srt_content = ""
|
49 |
+
for i, segment in enumerate(result["segments"]):
|
50 |
+
start = segment["start"]
|
51 |
+
end = segment["end"]
|
52 |
+
text = segment["text"]
|
53 |
+
srt_content += f"{i + 1}\n"
|
54 |
+
srt_content += f"{start:.3f}".replace(".", ",") + " --> " + f"{end:.3f}".replace(".", ",") + "\n"
|
55 |
+
srt_content += text + "\n\n"
|
56 |
+
|
57 |
+
return srt_content
|
58 |
+
|
59 |
+
def transcribe_video(url):
|
60 |
+
try:
|
61 |
+
logging.info(f"Transcribing video from URL: {url}")
|
62 |
+
audio_file = download_audio(url)
|
63 |
+
logging.info(f"Downloaded audio file: {audio_file}")
|
64 |
+
srt_content = transcribe_to_srt(audio_file)
|
65 |
+
logging.info("Transcription completed successfully!")
|
66 |
+
# Optionally, remove the audio file after transcription
|
67 |
+
# os.remove(audio_file)
|
68 |
+
return srt_content
|
69 |
+
except Exception as e:
|
70 |
+
logging.error(f"An error occurred: {e}")
|
71 |
+
return f"An error occurred: {e}"
|
72 |
+
|
73 |
+
iface = gr.Interface(fn=transcribe_video, inputs="text", outputs="text", live=True, title="YouTube/TikTok Video to SRT Transcription")
|
74 |
+
|
75 |
+
# Display the logs in the interface
|
76 |
+
log_viewer = gr.Textbox(text="Logs will appear here...", readonly=True, height=200)
|
77 |
+
log_handler = logging.StreamHandler(log_viewer)
|
78 |
+
log_handler.setLevel(logging.INFO)
|
79 |
+
logging.getLogger().addHandler(log_handler)
|
80 |
+
|
81 |
+
iface.launch()
|