thomasanto7001 commited on
Commit
f3aa119
·
verified ·
1 Parent(s): 0fe9459

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -19
app.py CHANGED
@@ -6,12 +6,13 @@ import os
6
  import re
7
  import random
8
  import subprocess
 
9
  from sklearn.feature_extraction.text import TfidfVectorizer
10
- from sklearn.metrics.pairwise import cosine_similarity
11
  from nltk.tokenize import sent_tokenize
12
  from nltk.corpus import stopwords
13
 
14
- nltk.download('punkt_tab')
 
15
  nltk.download('stopwords')
16
  stop_words = set(stopwords.words('english'))
17
 
@@ -31,17 +32,37 @@ def extract_audio(video_path):
31
 
32
  def transcribe_audio(audio_path):
33
  model = whisper.load_model("base")
34
- result = model.transcribe(audio_path)
35
- return result["text"]
36
 
37
- def generate_summary(text, max_len=130):
38
- summarizer = pipeline("summarization")
39
- sentences = sent_tokenize(text)
40
- chunks = [' '.join(sentences[i:i+10]) for i in range(0, len(sentences), 10)]
41
- summary = ""
42
- for chunk in chunks:
43
- summary += summarizer(chunk, max_length=max_len, min_length=30, do_sample=False)[0]["summary_text"] + " "
44
- return summary.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def generate_summary(text, max_len=130, min_len=30):
47
  summarizer = pipeline("summarization")
@@ -52,7 +73,7 @@ def generate_summary(text, max_len=130, min_len=30):
52
  for chunk in chunks:
53
  input_len = len(chunk.split())
54
  dynamic_max_len = min(max_len, max(20, input_len // 2))
55
- dynamic_min_len = min(min_len, dynamic_max_len - 5) # Ensure min_len < max_len
56
 
57
  result = summarizer(
58
  chunk,
@@ -94,13 +115,20 @@ def process_video(video_path, selected_services):
94
  results = {}
95
  print("🔧 Extracting audio...")
96
  audio_path = extract_audio(video_path)
97
- transcription = transcribe_audio(audio_path) if "Transcription" in selected_services else None
98
- if transcription:
99
- results["transcription"] = transcription
 
 
 
 
100
  if "Summary" in selected_services:
101
- results["summary"] = generate_summary(transcription)
 
102
  if "Subtitles" in selected_services:
103
- results["subtitles"] = generate_subtitles(transcription)
 
104
  if "Quiz" in selected_services:
105
- results["quiz"] = generate_quiz(transcription)
 
106
  return results
 
6
  import re
7
  import random
8
  import subprocess
9
+ import datetime
10
  from sklearn.feature_extraction.text import TfidfVectorizer
 
11
  from nltk.tokenize import sent_tokenize
12
  from nltk.corpus import stopwords
13
 
14
+ # Download necessary NLTK data
15
+ nltk.download('punkt')
16
  nltk.download('stopwords')
17
  stop_words = set(stopwords.words('english'))
18
 
 
32
 
33
  def transcribe_audio(audio_path):
34
  model = whisper.load_model("base")
35
+ result = model.transcribe(audio_path, word_timestamps=True) # includes segment timestamps
36
+ return result # full result with segments
37
 
38
+ def format_timestamp(seconds):
39
+ """Convert seconds to SRT timestamp format."""
40
+ td = datetime.timedelta(seconds=float(seconds))
41
+ return str(td)[:12].replace('.', ',')
42
+
43
+ def generate_subtitles(transcription):
44
+ """
45
+ Generate subtitles in a format similar to SRT using Whisper's segment output.
46
+ Assumes transcription is a dict containing a 'segments' key with timestamps.
47
+ """
48
+ if not transcription or 'segments' not in transcription:
49
+ raise ValueError("Transcription must include 'segments'.")
50
+
51
+ subtitles = []
52
+ for idx, segment in enumerate(transcription['segments'], start=1):
53
+ start = format_timestamp(segment['start'])
54
+ end = format_timestamp(segment['end'])
55
+ text = segment['text'].strip()
56
+
57
+ subtitle = {
58
+ "index": idx,
59
+ "start": start,
60
+ "end": end,
61
+ "text": text
62
+ }
63
+ subtitles.append(subtitle)
64
+
65
+ return subtitles
66
 
67
  def generate_summary(text, max_len=130, min_len=30):
68
  summarizer = pipeline("summarization")
 
73
  for chunk in chunks:
74
  input_len = len(chunk.split())
75
  dynamic_max_len = min(max_len, max(20, input_len // 2))
76
+ dynamic_min_len = min(min_len, dynamic_max_len - 5)
77
 
78
  result = summarizer(
79
  chunk,
 
115
  results = {}
116
  print("🔧 Extracting audio...")
117
  audio_path = extract_audio(video_path)
118
+
119
+ transcription_result = transcribe_audio(audio_path) if "Transcription" in selected_services else None
120
+ transcription_text = transcription_result["text"] if transcription_result else None
121
+
122
+ if transcription_result:
123
+ results["transcription"] = transcription_text
124
+
125
  if "Summary" in selected_services:
126
+ results["summary"] = generate_summary(transcription_text)
127
+
128
  if "Subtitles" in selected_services:
129
+ results["subtitles"] = generate_subtitles(transcription_result)
130
+
131
  if "Quiz" in selected_services:
132
+ results["quiz"] = generate_quiz(transcription_text)
133
+
134
  return results