rajesh1729
commited on
Commit
•
342ece4
1
Parent(s):
db69fcf
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,78 @@
|
|
1 |
-
import
|
2 |
-
from pytube import YouTube
|
3 |
-
from transformers import pipeline
|
4 |
-
import gradio as gr
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
model = whisper.load_model("base")
|
8 |
summarizer = pipeline("summarization")
|
9 |
|
10 |
-
def get_audio(url):
|
11 |
-
yt = YouTube(url)
|
12 |
-
video = yt.streams.filter(only_audio=True).first()
|
13 |
-
out_file=video.download(output_path=".")
|
14 |
-
base, ext = os.path.splitext(out_file)
|
15 |
-
new_file = base+'.mp3'
|
16 |
-
os.rename(out_file, new_file)
|
17 |
-
a = new_file
|
18 |
-
return a
|
19 |
-
|
20 |
def get_text(url):
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def get_summary(url):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
with gr.Blocks() as demo:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
with gr.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
result_button.click(get_summary, inputs = input_text, outputs = output_text)
|
45 |
-
result_button_1.click(get_text, inputs = input_text_1, outputs = output_text_1)
|
46 |
demo.launch(debug=True)
|
|
|
1 |
+
import yt_dlp
|
|
|
|
|
|
|
2 |
import os
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
import whisper
|
6 |
|
7 |
+
def get_audio(url):
|
8 |
+
try:
|
9 |
+
# Configure yt-dlp options
|
10 |
+
ydl_opts = {
|
11 |
+
'format': 'bestaudio/best', # Choose best quality audio
|
12 |
+
'postprocessors': [{
|
13 |
+
'key': 'FFmpegExtractAudio',
|
14 |
+
'preferredcodec': 'mp3',
|
15 |
+
'preferredquality': '192',
|
16 |
+
}],
|
17 |
+
'outtmpl': 'audio_download.%(ext)s', # Output template
|
18 |
+
'quiet': True, # Less output
|
19 |
+
'no_warnings': True # No warnings
|
20 |
+
}
|
21 |
+
|
22 |
+
# Download the audio
|
23 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
24 |
+
ydl.download([url])
|
25 |
+
|
26 |
+
return 'audio_download.mp3' # Return the filename
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
raise gr.Error(f"Error downloading audio: {str(e)}")
|
30 |
+
|
31 |
+
# Load models
|
32 |
model = whisper.load_model("base")
|
33 |
summarizer = pipeline("summarization")
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def get_text(url):
|
36 |
+
try:
|
37 |
+
audio_file = get_audio(url)
|
38 |
+
result = model.transcribe(audio_file)
|
39 |
+
|
40 |
+
# Cleanup
|
41 |
+
try:
|
42 |
+
os.remove(audio_file)
|
43 |
+
except:
|
44 |
+
pass
|
45 |
+
|
46 |
+
return result['text']
|
47 |
+
except Exception as e:
|
48 |
+
return f"Error: {str(e)}"
|
49 |
|
50 |
def get_summary(url):
|
51 |
+
try:
|
52 |
+
article = get_text(url)
|
53 |
+
summary = summarizer(article)
|
54 |
+
return summary[0]['summary_text']
|
55 |
+
except Exception as e:
|
56 |
+
return f"Error: {str(e)}"
|
57 |
+
|
58 |
+
# Create Gradio interface
|
59 |
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("<h1><center>YouTube Video Transcription with OpenAI's Whisper</center></h1>")
|
61 |
+
gr.Markdown("<center>Enter the link of any YouTube video to get the transcription and summary.</center>")
|
62 |
+
|
63 |
+
with gr.Tab('Get the transcription of any Youtube video'):
|
64 |
+
with gr.Row():
|
65 |
+
input_text_1 = gr.Textbox(placeholder='Enter the Youtube video URL', label='URL')
|
66 |
+
output_text_1 = gr.Textbox(placeholder='Transcription of the video', label='Transcription')
|
67 |
+
result_button_1 = gr.Button('Get Transcription')
|
68 |
+
|
69 |
+
with gr.Tab('Summary of Youtube video'):
|
70 |
+
with gr.Row():
|
71 |
+
input_text = gr.Textbox(placeholder='Enter the Youtube video URL', label='URL')
|
72 |
+
output_text = gr.Textbox(placeholder='Summary text of the Youtube Video', label='Summary')
|
73 |
+
result_button = gr.Button('Get Summary')
|
74 |
+
|
75 |
+
result_button.click(get_summary, inputs=input_text, outputs=output_text)
|
76 |
+
result_button_1.click(get_text, inputs=input_text_1, outputs=output_text_1)
|
77 |
|
|
|
|
|
78 |
demo.launch(debug=True)
|