Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,58 @@
|
|
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 |
-
# Add ffmpeg to the PATH
|
9 |
-
ffmpeg_path = r'
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
video.audio.write_audiofile(audio_path)
|
60 |
-
except Exception as e:
|
61 |
-
raise RuntimeError(f"Error extracting audio: {e}")
|
62 |
-
return audio_path
|
63 |
-
|
64 |
-
def transcribe_audio(audio_path):
|
65 |
-
if not os.path.exists(audio_path):
|
66 |
-
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
67 |
-
|
68 |
-
try:
|
69 |
-
result = model.transcribe(audio_path)
|
70 |
-
return result["text"]
|
71 |
-
except Exception as e:
|
72 |
-
raise RuntimeError(f"Error during transcription: {e}")
|
73 |
-
|
74 |
-
if __name__ == '__main__':
|
75 |
-
if not os.path.exists('uploads'):
|
76 |
-
os.makedirs('uploads')
|
77 |
-
app.run(debug=True)
|
|
|
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 |
+
# Add ffmpeg to the PATH
|
9 |
+
ffmpeg_path = r'/usr/bin/ffmpeg' # Path for ffmpeg in the Docker container
|
10 |
+
|
11 |
+
# Load the Whisper model
|
12 |
+
model = whisper.load_model("medium") # Change to "large" for the most accurate model
|
13 |
+
|
14 |
+
@app.route('/')
|
15 |
+
def index():
|
16 |
+
return render_template('index.html')
|
17 |
+
|
18 |
+
@app.route('/upload', methods=['POST'])
|
19 |
+
def upload_video():
|
20 |
+
if 'video' not in request.files:
|
21 |
+
return redirect(url_for('index'))
|
22 |
+
|
23 |
+
video_file = request.files['video']
|
24 |
+
if video_file.filename == '':
|
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, model_name="medium") # Adjust model name accordingly
|
40 |
+
|
41 |
+
def extract_audio(video_path):
|
42 |
+
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
43 |
+
try:
|
44 |
+
video = VideoFileClip(video_path)
|
45 |
+
video.audio.write_audiofile(audio_path)
|
46 |
+
except Exception as e:
|
47 |
+
raise RuntimeError(f"Error extracting audio: {e}")
|
48 |
+
return audio_path
|
49 |
+
|
50 |
+
def transcribe_audio(audio_path):
|
51 |
+
if not os.path.exists(audio_path):
|
52 |
+
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
53 |
+
|
54 |
+
try:
|
55 |
+
result = model.transcribe(audio_path)
|
56 |
+
return result["text"]
|
57 |
+
except Exception as e:
|
58 |
+
raise RuntimeError(f"Error during transcription: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|