Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 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
|
| 36 |
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
| 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 |
-
|
| 98 |
-
if
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if "Summary" in selected_services:
|
| 101 |
-
results["summary"] = generate_summary(
|
|
|
|
| 102 |
if "Subtitles" in selected_services:
|
| 103 |
-
results["subtitles"] = generate_subtitles(
|
|
|
|
| 104 |
if "Quiz" in selected_services:
|
| 105 |
-
results["quiz"] = generate_quiz(
|
|
|
|
| 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
|