krishnasivaborra commited on
Commit
5b8dea2
·
verified ·
1 Parent(s): e2bd892

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import gradio as gr
3
+ from youtube_transcript_api import YouTubeTranscriptApi
4
+ from transformers import pipeline
5
+
6
+ # Load a pre-trained summarization model from Hugging Face
7
+ # 'sshleifer/distilbart-cnn-12-6' is a good general-purpose summarization model
8
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
9
+
10
+ def get_youtube_id(url):
11
+ """Extracts the YouTube video ID from a URL."""
12
+ if "youtu.be/" in url:
13
+ return url.split("youtu.be/")[1].split("?")[0]
14
+ elif "v=" in url:
15
+ return url.split("v=")[1].split("&")[0]
16
+ return None
17
+
18
+ def summarize_youtube_transcript(youtube_url):
19
+ """
20
+ Fetches the transcript of a YouTube video and summarizes it.
21
+
22
+ Args:
23
+ youtube_url (str): The URL of the YouTube video.
24
+
25
+ Returns:
26
+ str: The summarized transcript or an error message.
27
+ """
28
+ video_id = get_youtube_id(youtube_url)
29
+
30
+ if not video_id:
31
+ return "Error: Could not extract YouTube video ID from the provided URL."
32
+
33
+ try:
34
+ # Get the transcript for the video ID
35
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
36
+
37
+ # Concatenate the transcript text
38
+ transcript_text = " ".join([d['text'] for d in transcript_list])
39
+
40
+ # Summarize the transcript
41
+ # The summarizer pipeline handles splitting long texts if necessary,
42
+ # but for very long videos, you might need more advanced chunking.
43
+ # max_length and min_length control the summary length.
44
+ summary = summarizer(transcript_text, max_length=200, min_length=50, do_sample=False)
45
+
46
+ return summary[0]['summary_text']
47
+
48
+ except Exception as e:
49
+ return f"Error fetching or summarizing transcript: {e}"
50
+
51
+ # Create the Gradio interface
52
+ # The interface takes a text input (for the YouTube URL)
53
+ # and provides a text output (for the summarized transcript)
54
+ iface = gr.Interface(
55
+ fn=summarize_youtube_transcript,
56
+ inputs=gr.Textbox(label="Enter YouTube Video URL"),
57
+ outputs=gr.Textbox(label="Summarized Transcript"),
58
+ title="YouTube Transcript Summarizer",
59
+ description="Enter a YouTube video URL to get a summary of its transcript."
60
+ )
61
+
62
+ # Launch the Gradio app
63
+ # share=True creates a temporary public link (useful for testing)
64
+ # Setting debug=True provides detailed logs
65
+ # iface.launch(share=True, debug=True)
66
+
67
+ # To deploy on Hugging Face Spaces, you just need this file (e.g., app.py)
68
+ # and a requirements.txt file. Hugging Face Spaces will automatically run
69
+ # the Gradio app if it finds an interface defined.
70
+ # Remove the iface.launch() call when deploying to Hugging Face Spaces.
71
+ # The last line should be the interface object itself.
72
+ iface.launch() # Use this line for local testing
73
+ # iface # Use this line for Hugging Face Spaces deployment