navyaamittal commited on
Commit
bc464ef
·
verified ·
1 Parent(s): e4b56a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -70
app.py CHANGED
@@ -1,85 +1,55 @@
1
- import re
2
  import gradio as gr
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  from transformers import pipeline
 
5
 
6
- # Lazy load models (critical for HF Spaces)
7
- summarizer = None
8
- qa_pipeline = None
9
-
10
- def load_models():
11
- global summarizer, qa_pipeline
12
- if summarizer is None:
13
- summarizer = pipeline(
14
- task="text2text-generation",
15
- model="sshleifer/distilbart-cnn-12-6"
16
- )
17
- if qa_pipeline is None:
18
- qa_pipeline = pipeline(
19
- task="question-answering",
20
- model="distilbert-base-cased-distilled-squad"
21
- )
22
 
23
  def extract_video_id(url):
24
- regex = r"(?:v=|youtu\.be/)([a-zA-Z0-9_-]{11})"
25
  match = re.search(regex, url)
26
  return match.group(1) if match else None
27
 
28
- def get_transcript(url):
29
- video_id = extract_video_id(url)
30
- if not video_id:
31
- return None, "❌ Invalid YouTube URL"
32
  try:
33
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
34
- text = " ".join([t["text"] for t in transcript])
35
- return text, None
36
- except Exception as e:
37
- return None, f"❌ Transcript Error: {str(e)}"
38
-
39
- def chunk_text(text, size=400):
40
- words = text.split()
41
- for i in range(0, len(words), size):
42
- yield " ".join(words[i:i+size])
43
 
44
  def summarize_text(text):
45
- load_models()
46
- result = []
47
- for chunk in chunk_text(text):
48
- out = summarizer(chunk, max_length=100, min_length=30, do_sample=False)
49
- result.append(out[0]["generated_text"])
50
- return " ".join(result)
51
-
52
- def answer_question(context, question):
53
- load_models()
54
- best = ("", 0)
55
- for chunk in chunk_text(context):
56
- try:
57
- res = qa_pipeline(question=question, context=chunk)
58
- if res["score"] > best[1]:
59
- best = (res["answer"], res["score"])
60
- except:
61
- continue
62
- return best[0] if best[0] else "❌ No answer found"
63
-
64
- def process(url, question):
65
- text, err = get_transcript(url)
66
- if err:
67
- return err, ""
68
- summary = summarize_text(text)
69
- answer = answer_question(text, question) if question.strip() else ""
70
- return summary, answer
71
-
72
- demo = gr.Interface(
73
- fn=process,
74
- inputs=[
75
- gr.Textbox(label="YouTube URL"),
76
- gr.Textbox(label="Ask a question (optional)")
77
- ],
78
- outputs=[
79
- gr.Textbox(label="Summary"),
80
- gr.Textbox(label="Answer")
81
- ],
82
- title="YouTube Video Summarizer + Chat"
83
  )
84
 
85
- demo.launch()
 
 
 
1
  import gradio as gr
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
  from transformers import pipeline
4
+ import re
5
 
6
+ # Load summarizer (lightweight for Spaces)
7
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def extract_video_id(url):
10
+ regex = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
11
  match = re.search(regex, url)
12
  return match.group(1) if match else None
13
 
14
+ def get_transcript(video_id):
 
 
 
15
  try:
16
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
17
+ text = " ".join([t['text'] for t in transcript])
18
+ return text
19
+ except:
20
+ return None
 
 
 
 
 
21
 
22
  def summarize_text(text):
23
+ max_chunk = 1000
24
+ chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
25
+
26
+ summary = ""
27
+ for chunk in chunks:
28
+ result = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
29
+ summary += result[0]['summary_text'] + " "
30
+
31
+ return summary
32
+
33
+ def process_video(url):
34
+ video_id = extract_video_id(url)
35
+ if not video_id:
36
+ return "Invalid YouTube URL"
37
+
38
+ transcript = get_transcript(video_id)
39
+ if not transcript:
40
+ return "Could not fetch transcript"
41
+
42
+ summary = summarize_text(transcript)
43
+ return summary
44
+
45
+ # Gradio UI
46
+ iface = gr.Interface(
47
+ fn=process_video,
48
+ inputs=gr.Textbox(label="Enter YouTube URL"),
49
+ outputs=gr.Textbox(label="Summary"),
50
+ title="YouTube Video Summarizer",
51
+ description="Paste a YouTube link to get a summarized transcript."
 
 
 
 
 
 
 
 
 
52
  )
53
 
54
+ if __name__ == "__main__":
55
+ iface.launch()