Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,15 @@
|
|
1 |
from flask import Flask, request, render_template, redirect, url_for
|
2 |
import os
|
3 |
-
import requests
|
4 |
from moviepy.editor import VideoFileClip
|
5 |
-
|
6 |
-
from pydub.utils import make_chunks
|
7 |
-
import time
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
# Configure the maximum content length for uploads (500 MB)
|
12 |
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 500 # 500 MB limit
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
# Read the API token from the environment variable
|
17 |
-
API_TOKEN = os.getenv('HF_API_TOKEN')
|
18 |
|
19 |
@app.route('/')
|
20 |
def index():
|
@@ -36,8 +31,8 @@ def upload_video():
|
|
36 |
try:
|
37 |
# Extract audio from the video
|
38 |
audio_path = extract_audio(video_path)
|
39 |
-
#
|
40 |
-
transcript =
|
41 |
except Exception as e:
|
42 |
return f"Error: {e}"
|
43 |
|
@@ -53,48 +48,13 @@ def extract_audio(video_path):
|
|
53 |
raise RuntimeError(f"Error extracting audio: {e}")
|
54 |
return audio_path
|
55 |
|
56 |
-
def
|
57 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
58 |
-
|
59 |
-
for attempt in range(retries):
|
60 |
-
try:
|
61 |
-
with open(audio_chunk_path, "rb") as audio_chunk:
|
62 |
-
response = requests.post(API_URL, headers=headers, files={"file": audio_chunk})
|
63 |
-
|
64 |
-
if response.status_code == 200:
|
65 |
-
result = response.json()
|
66 |
-
return result.get("text", "")
|
67 |
-
elif response.status_code == 503 and 'estimated_time' in response.json():
|
68 |
-
# If the model is loading, wait and retry
|
69 |
-
wait = response.json()['estimated_time']
|
70 |
-
time.sleep(wait)
|
71 |
-
else:
|
72 |
-
response.raise_for_status()
|
73 |
-
except Exception as e:
|
74 |
-
if attempt == retries - 1:
|
75 |
-
raise RuntimeError(f"Error during transcription after {retries} attempts: {e}")
|
76 |
-
else:
|
77 |
-
time.sleep(wait_time)
|
78 |
-
return ""
|
79 |
-
|
80 |
-
def split_and_transcribe_audio(audio_path):
|
81 |
if not os.path.exists(audio_path):
|
82 |
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
83 |
|
84 |
try:
|
85 |
-
|
86 |
-
|
87 |
-
chunks = make_chunks(audio, chunk_length_ms)
|
88 |
-
|
89 |
-
transcript = ""
|
90 |
-
for i, chunk in enumerate(chunks):
|
91 |
-
chunk_path = f"{audio_path[:-4]}_chunk{i}.wav"
|
92 |
-
chunk.export(chunk_path, format="wav")
|
93 |
-
|
94 |
-
transcript += transcribe_audio_chunk(chunk_path)
|
95 |
-
transcript += " " # Add space between chunks' transcriptions
|
96 |
-
|
97 |
-
return transcript.strip()
|
98 |
except Exception as e:
|
99 |
raise RuntimeError(f"Error during transcription: {e}")
|
100 |
|
|
|
1 |
from flask import Flask, request, render_template, redirect, url_for
|
2 |
import os
|
|
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
+
import whisper
|
|
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
# Configure the maximum content length for uploads (500 MB)
|
9 |
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 500 # 500 MB limit
|
10 |
|
11 |
+
# Load the Whisper model
|
12 |
+
model = whisper.load_model("base")
|
|
|
|
|
13 |
|
14 |
@app.route('/')
|
15 |
def index():
|
|
|
31 |
try:
|
32 |
# Extract audio from the video
|
33 |
audio_path = extract_audio(video_path)
|
34 |
+
# Transcribe the audio
|
35 |
+
transcript = transcribe_audio(audio_path)
|
36 |
except Exception as e:
|
37 |
return f"Error: {e}"
|
38 |
|
|
|
48 |
raise RuntimeError(f"Error extracting audio: {e}")
|
49 |
return audio_path
|
50 |
|
51 |
+
def transcribe_audio(audio_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
if not os.path.exists(audio_path):
|
53 |
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
54 |
|
55 |
try:
|
56 |
+
result = model.transcribe(audio_path)
|
57 |
+
return result["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
except Exception as e:
|
59 |
raise RuntimeError(f"Error during transcription: {e}")
|
60 |
|