Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,88 @@ from moviepy.editor import VideoFileClip
|
|
4 |
import whisper
|
5 |
|
6 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Add ffmpeg to the PATH
|
9 |
ffmpeg_path = r'/usr/bin/ffmpeg' # Path for ffmpeg in the Docker container
|
|
|
4 |
import whisper
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
+
from flask import Flask, request, render_template, redirect, url_for
|
8 |
+
import os
|
9 |
+
from moviepy.editor import VideoFileClip
|
10 |
+
import whisper
|
11 |
+
|
12 |
+
app = Flask(__name__)
|
13 |
+
|
14 |
+
# Set ffmpeg path for the Docker container environment
|
15 |
+
ffmpeg_path = r'/usr/bin/ffmpeg'
|
16 |
+
if not os.path.isdir(ffmpeg_path):
|
17 |
+
raise FileNotFoundError(f"FFmpeg directory not found: {ffmpeg_path}")
|
18 |
+
os.environ['PATH'] += os.pathsep + ffmpeg_path
|
19 |
+
|
20 |
+
# Load the Whisper model
|
21 |
+
model = whisper.load_model("medium") # Change to "large" for the most accurate model
|
22 |
+
|
23 |
+
@app.route('/')
|
24 |
+
def index():
|
25 |
+
return render_template('index.html')
|
26 |
+
|
27 |
+
@app.route('/upload', methods=['POST'])
|
28 |
+
def upload_video():
|
29 |
+
if 'video' not in request.files:
|
30 |
+
return redirect(url_for('index'))
|
31 |
+
|
32 |
+
video_file = request.files['video']
|
33 |
+
if video_file.filename == '':
|
34 |
+
return redirect(url_for('index'))
|
35 |
+
|
36 |
+
# Save the video file
|
37 |
+
video_path = os.path.join('uploads', video_file.filename)
|
38 |
+
video_file.save(video_path)
|
39 |
+
|
40 |
+
print(f"Video saved to {video_path}")
|
41 |
+
|
42 |
+
# Extract audio from the video
|
43 |
+
try:
|
44 |
+
audio_path = extract_audio(video_path)
|
45 |
+
print(f"Audio extracted to {audio_path}")
|
46 |
+
if not os.path.exists(audio_path):
|
47 |
+
return f"Error: Audio file not found at {audio_path}"
|
48 |
+
except Exception as e:
|
49 |
+
return f"Error extracting audio: {e}"
|
50 |
+
|
51 |
+
# Transcribe the audio
|
52 |
+
try:
|
53 |
+
transcript = transcribe_audio(audio_path)
|
54 |
+
except Exception as e:
|
55 |
+
return f"Error transcribing audio: {e}"
|
56 |
+
|
57 |
+
return render_template('result.html', transcript=transcript)
|
58 |
+
|
59 |
+
def extract_audio(video_path):
|
60 |
+
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
61 |
+
video = VideoFileClip(video_path)
|
62 |
+
video.audio.write_audiofile(audio_path)
|
63 |
+
return audio_path
|
64 |
+
|
65 |
+
def transcribe_audio(audio_path):
|
66 |
+
print(f"Transcribing audio from: {audio_path}")
|
67 |
+
if not os.path.exists(audio_path):
|
68 |
+
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
69 |
+
|
70 |
+
try:
|
71 |
+
os.system("ffmpeg -version")
|
72 |
+
print("FFmpeg is available")
|
73 |
+
except Exception as e:
|
74 |
+
print(f"FFmpeg is not available: {e}")
|
75 |
+
raise
|
76 |
+
|
77 |
+
try:
|
78 |
+
result = model.transcribe(audio_path)
|
79 |
+
print(f"Transcription result: {result}")
|
80 |
+
return result["text"]
|
81 |
+
except Exception as e:
|
82 |
+
print(f"Error during transcription: {e}")
|
83 |
+
raise
|
84 |
+
|
85 |
+
if __name__ == '__main__':
|
86 |
+
if not os.path.exists('uploads'):
|
87 |
+
os.makedirs('uploads')
|
88 |
+
app.run(debug=True)
|
89 |
|
90 |
# Add ffmpeg to the PATH
|
91 |
ffmpeg_path = r'/usr/bin/ffmpeg' # Path for ffmpeg in the Docker container
|