Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,24 +2,12 @@ from flask import Flask, request, render_template, redirect, url_for
|
|
2 |
import os
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
import whisper
|
5 |
-
import hashlib
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
# Configure the maximum content length for uploads (500 MB)
|
10 |
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 500 # 500 MB limit
|
11 |
|
12 |
-
# Create directories for uploads and cache
|
13 |
-
UPLOAD_FOLDER = 'uploads'
|
14 |
-
AUDIO_FOLDER = 'audio_cache'
|
15 |
-
TRANSCRIPT_FOLDER = 'transcript_cache'
|
16 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
17 |
-
os.makedirs(AUDIO_FOLDER, exist_ok=True)
|
18 |
-
os.makedirs(TRANSCRIPT_FOLDER, exist_ok=True)
|
19 |
-
|
20 |
-
# Set environment variable for Whisper cache
|
21 |
-
os.environ["XDG_CACHE_HOME"] = "/app/.cache"
|
22 |
-
|
23 |
# Load the Whisper model
|
24 |
model = whisper.load_model("base")
|
25 |
|
@@ -37,38 +25,21 @@ def upload_video():
|
|
37 |
return redirect(url_for('index'))
|
38 |
|
39 |
# Save the video file
|
40 |
-
video_path = os.path.join(
|
41 |
video_file.save(video_path)
|
42 |
|
43 |
try:
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
audio_path = os.path.join(AUDIO_FOLDER, f"{video_hash}.wav")
|
49 |
-
transcript_path = os.path.join(TRANSCRIPT_FOLDER, f"{video_hash}.txt")
|
50 |
-
|
51 |
-
if not os.path.exists(audio_path):
|
52 |
-
# Extract audio from the video if not cached
|
53 |
-
audio_path = extract_audio(video_path, audio_path)
|
54 |
-
|
55 |
-
if not os.path.exists(transcript_path):
|
56 |
-
# Transcribe the audio if not cached
|
57 |
-
transcript = transcribe_audio(audio_path)
|
58 |
-
# Cache the transcript
|
59 |
-
with open(transcript_path, 'w') as f:
|
60 |
-
f.write(transcript)
|
61 |
-
else:
|
62 |
-
# Load cached transcript
|
63 |
-
with open(transcript_path, 'r') as f:
|
64 |
-
transcript = f.read()
|
65 |
-
|
66 |
except Exception as e:
|
67 |
return f"Error: {e}"
|
68 |
|
69 |
return render_template('result.html', transcript=transcript)
|
70 |
|
71 |
-
def extract_audio(video_path
|
|
|
72 |
try:
|
73 |
# Use a temporary file to reduce the load on memory
|
74 |
with VideoFileClip(video_path) as video:
|
@@ -88,4 +59,4 @@ def transcribe_audio(audio_path):
|
|
88 |
raise RuntimeError(f"Error during transcription: {e}")
|
89 |
|
90 |
if __name__ == '__main__':
|
91 |
-
app.run(debug=False, host='0.0.0.0', port=7860)
|
|
|
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 |
|
|
|
25 |
return redirect(url_for('index'))
|
26 |
|
27 |
# Save the video file
|
28 |
+
video_path = os.path.join('uploads', video_file.filename)
|
29 |
video_file.save(video_path)
|
30 |
|
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 |
|
39 |
return render_template('result.html', transcript=transcript)
|
40 |
|
41 |
+
def extract_audio(video_path):
|
42 |
+
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
43 |
try:
|
44 |
# Use a temporary file to reduce the load on memory
|
45 |
with VideoFileClip(video_path) as video:
|
|
|
59 |
raise RuntimeError(f"Error during transcription: {e}")
|
60 |
|
61 |
if __name__ == '__main__':
|
62 |
+
app.run(debug=False, host='0.0.0.0', port=7860)
|