Elalimy commited on
Commit
8625430
·
verified ·
1 Parent(s): 8f8ab75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -77
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'C:\Users\M&G\Downloads\ffmpeg-7.0.1-essentials_build\bin' # Replace with your actual path
10
-
11
- if not os.path.isdir(ffmpeg_path):
12
- raise FileNotFoundError(f"FFmpeg directory not found: {ffmpeg_path}")
13
- os.environ['PATH'] += os.pathsep + ffmpeg_path
14
-
15
- # Verify that ffmpeg is available
16
- try:
17
- from subprocess import check_output
18
- ffmpeg_version = check_output(['ffmpeg', '-version'], text=True)
19
- print("FFmpeg is available. Version details:\n", ffmpeg_version)
20
- except FileNotFoundError:
21
- raise RuntimeError("FFmpeg executable not found in the specified PATH.")
22
- except Exception as e:
23
- raise RuntimeError(f"Error checking FFmpeg version: {e}")
24
-
25
- # Load the Whisper model
26
- model = whisper.load_model("medium")
27
-
28
- @app.route('/')
29
- def index():
30
- return render_template('index.html')
31
-
32
- @app.route('/upload', methods=['POST'])
33
- def upload_video():
34
- if 'video' not in request.files:
35
- return redirect(url_for('index'))
36
-
37
- video_file = request.files['video']
38
- if video_file.filename == '':
39
- return redirect(url_for('index'))
40
-
41
- # Save the video file
42
- video_path = os.path.join('uploads', video_file.filename)
43
- video_file.save(video_path)
44
-
45
- try:
46
- # Extract audio from the video
47
- audio_path = extract_audio(video_path)
48
- # Transcribe the audio
49
- transcript = transcribe_audio(audio_path)
50
- except Exception as e:
51
- return f"Error: {e}"
52
-
53
- return render_template('result.html', transcript=transcript)
54
-
55
- def extract_audio(video_path):
56
- audio_path = os.path.splitext(video_path)[0] + ".wav"
57
- try:
58
- video = VideoFileClip(video_path)
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}")