Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,43 +5,71 @@ import torch
|
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import pipeline
|
| 7 |
|
|
|
|
| 8 |
text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
return output[0]['summary_text']
|
| 13 |
|
|
|
|
| 14 |
def extract_video_id(url):
|
| 15 |
-
# Regex to extract the video ID from various YouTube URL formats
|
| 16 |
regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
|
| 17 |
match = re.search(regex, url)
|
| 18 |
if match:
|
| 19 |
return match.group(1)
|
| 20 |
return None
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def get_youtube_transcript(video_url):
|
| 24 |
video_id = extract_video_id(video_url)
|
| 25 |
if not video_id:
|
| 26 |
return "Video ID could not be extracted."
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
# Fetch the transcript
|
| 30 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 31 |
|
| 32 |
-
# Format the transcript into plain text
|
| 33 |
formatter = TextFormatter()
|
| 34 |
text_transcript = formatter.format_transcript(transcript)
|
| 35 |
-
summary_text = summary(text_transcript)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"An error occurred: {e}"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
demo.launch(share=True)
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Initialize the model for summarization
|
| 9 |
text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16)
|
| 10 |
|
| 11 |
+
# Function to summarize text
|
| 12 |
+
def summary(input_text):
|
| 13 |
+
output = text_summary(input_text)
|
| 14 |
return output[0]['summary_text']
|
| 15 |
|
| 16 |
+
# Function to extract YouTube video ID from URL
|
| 17 |
def extract_video_id(url):
|
|
|
|
| 18 |
regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
|
| 19 |
match = re.search(regex, url)
|
| 20 |
if match:
|
| 21 |
return match.group(1)
|
| 22 |
return None
|
| 23 |
|
| 24 |
+
# Function to get YouTube transcript and summary
|
| 25 |
+
def get_youtube_transcript(video_url, summarize=True):
|
| 26 |
video_id = extract_video_id(video_url)
|
| 27 |
if not video_id:
|
| 28 |
return "Video ID could not be extracted."
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 32 |
|
|
|
|
| 33 |
formatter = TextFormatter()
|
| 34 |
text_transcript = formatter.format_transcript(transcript)
|
|
|
|
| 35 |
|
| 36 |
+
if summarize:
|
| 37 |
+
return summary(text_transcript)
|
| 38 |
+
else:
|
| 39 |
+
return text_transcript
|
| 40 |
+
|
| 41 |
except Exception as e:
|
| 42 |
return f"An error occurred: {e}"
|
| 43 |
|
| 44 |
+
# Define the Gradio interface with customization
|
| 45 |
+
def create_gradio_interface():
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# YouTube Script Summarizer 🤖")
|
| 48 |
+
gr.Markdown("""
|
| 49 |
+
Enter the video URL, and choose whether you'd like the full Script or just the summary.
|
| 50 |
+
### Credits:
|
| 51 |
+
Created by **Taizun** – Providing a simple solution for video summarization!
|
| 52 |
+
""")
|
| 53 |
+
|
| 54 |
+
# Input for YouTube URL
|
| 55 |
+
video_url_input = gr.Textbox(label="Input YouTube URL", lines=1)
|
| 56 |
+
|
| 57 |
+
# Radio button for choosing output type (summary or full transcript)
|
| 58 |
+
output_type = gr.Radio(choices=["Summary", "Full Transcript"], label="Choose Output Type", value="Summary")
|
| 59 |
+
|
| 60 |
+
# Output for summarized or full transcript text
|
| 61 |
+
output_text = gr.Textbox(label="Result", lines=6)
|
| 62 |
+
|
| 63 |
+
# Submit button
|
| 64 |
+
submit_button = gr.Button("Generate", variant="primary")
|
| 65 |
+
|
| 66 |
+
# Define the action for the button press
|
| 67 |
+
submit_button.click(fn=get_youtube_transcript,
|
| 68 |
+
inputs=[video_url_input, output_type],
|
| 69 |
+
outputs=[output_text])
|
| 70 |
+
|
| 71 |
+
return demo
|
| 72 |
+
|
| 73 |
+
# Launch the interface with user credit
|
| 74 |
+
demo = create_gradio_interface()
|
| 75 |
demo.launch(share=True)
|