Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
ZennyKenny
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import pipeline
|
4 |
-
import
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def main():
|
8 |
# Force GPU if available, fallback to CPU
|
9 |
device = 0 if torch.cuda.is_available() else -1
|
@@ -19,8 +39,8 @@ def main():
|
|
19 |
# Function to process audio
|
20 |
def process_audio(audio_file):
|
21 |
try:
|
22 |
-
# Transcribe the audio
|
23 |
-
transcription =
|
24 |
# Summarize the transcription
|
25 |
summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
|
26 |
return transcription, summary
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import pipeline
|
4 |
+
import librosa # For audio processing
|
5 |
|
6 |
+
def split_audio(audio_path, chunk_duration=30):
|
7 |
+
"""Split audio into chunks of chunk_duration seconds."""
|
8 |
+
audio, sr = librosa.load(audio_path, sr=None)
|
9 |
+
chunks = []
|
10 |
+
for start in range(0, len(audio), int(chunk_duration * sr)):
|
11 |
+
end = start + int(chunk_duration * sr)
|
12 |
+
chunks.append(audio[start:end])
|
13 |
+
return chunks, sr
|
14 |
+
|
15 |
+
def transcribe_long_audio(audio_path, transcriber, chunk_duration=30):
|
16 |
+
"""Transcribe long audio by splitting into smaller chunks."""
|
17 |
+
chunks, sr = split_audio(audio_path, chunk_duration)
|
18 |
+
transcriptions = []
|
19 |
+
for chunk in chunks:
|
20 |
+
temp_path = "temp_chunk.wav"
|
21 |
+
librosa.output.write_wav(temp_path, chunk, sr)
|
22 |
+
transcription = transcriber(temp_path)["text"]
|
23 |
+
transcriptions.append(transcription)
|
24 |
+
return " ".join(transcriptions)
|
25 |
+
|
26 |
+
@spaces.GPU(duration=3)
|
27 |
def main():
|
28 |
# Force GPU if available, fallback to CPU
|
29 |
device = 0 if torch.cuda.is_available() else -1
|
|
|
39 |
# Function to process audio
|
40 |
def process_audio(audio_file):
|
41 |
try:
|
42 |
+
# Transcribe the audio (long-form support)
|
43 |
+
transcription = transcribe_long_audio(audio_file, transcriber, chunk_duration=30)
|
44 |
# Summarize the transcription
|
45 |
summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
|
46 |
return transcription, summary
|