Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,10 @@ import os
|
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
import whisper
|
5 |
|
6 |
-
# Set a writable cache directory
|
7 |
-
os.environ['XDG_CACHE_HOME'] = '/app/.cache'
|
8 |
-
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
# Load the Whisper model
|
12 |
-
model = whisper.load_model("base")
|
13 |
|
14 |
@app.route('/')
|
15 |
def index():
|
@@ -28,45 +25,36 @@ def upload_video():
|
|
28 |
video_path = os.path.join('uploads', video_file.filename)
|
29 |
video_file.save(video_path)
|
30 |
|
31 |
-
print(f"Video saved to {video_path}")
|
32 |
-
|
33 |
-
# Extract audio from the video
|
34 |
try:
|
|
|
35 |
audio_path = extract_audio(video_path)
|
36 |
-
|
37 |
-
if not os.path.exists(audio_path):
|
38 |
-
return f"Error: Audio file not found at {audio_path}"
|
39 |
-
except Exception as e:
|
40 |
-
return f"Error extracting audio: {e}"
|
41 |
-
|
42 |
-
# Transcribe the audio
|
43 |
-
try:
|
44 |
transcript = transcribe_audio(audio_path)
|
45 |
except Exception as e:
|
46 |
-
return f"Error
|
47 |
|
48 |
return render_template('result.html', transcript=transcript)
|
49 |
|
50 |
def extract_audio(video_path):
|
51 |
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
return audio_path
|
55 |
|
56 |
def transcribe_audio(audio_path):
|
57 |
-
print(f"Transcribing audio from: {audio_path}")
|
58 |
if not os.path.exists(audio_path):
|
59 |
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
60 |
|
61 |
try:
|
62 |
result = model.transcribe(audio_path)
|
63 |
-
print(f"Transcription result: {result}")
|
64 |
return result["text"]
|
65 |
except Exception as e:
|
66 |
-
|
67 |
-
raise
|
68 |
|
69 |
if __name__ == '__main__':
|
70 |
if not os.path.exists('uploads'):
|
71 |
os.makedirs('uploads')
|
72 |
-
app.run(debug=True
|
|
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
import whisper
|
5 |
|
|
|
|
|
|
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
# Load the Whisper model
|
9 |
+
model = whisper.load_model("base")
|
10 |
|
11 |
@app.route('/')
|
12 |
def index():
|
|
|
25 |
video_path = os.path.join('uploads', video_file.filename)
|
26 |
video_file.save(video_path)
|
27 |
|
|
|
|
|
|
|
28 |
try:
|
29 |
+
# Extract audio from the video
|
30 |
audio_path = extract_audio(video_path)
|
31 |
+
# Transcribe the audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
transcript = transcribe_audio(audio_path)
|
33 |
except Exception as e:
|
34 |
+
return f"Error: {e}"
|
35 |
|
36 |
return render_template('result.html', transcript=transcript)
|
37 |
|
38 |
def extract_audio(video_path):
|
39 |
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
40 |
+
try:
|
41 |
+
video = VideoFileClip(video_path)
|
42 |
+
video.audio.write_audiofile(audio_path)
|
43 |
+
except Exception as e:
|
44 |
+
raise RuntimeError(f"Error extracting audio: {e}")
|
45 |
return audio_path
|
46 |
|
47 |
def transcribe_audio(audio_path):
|
|
|
48 |
if not os.path.exists(audio_path):
|
49 |
raise FileNotFoundError(f"Audio file not found at {audio_path}")
|
50 |
|
51 |
try:
|
52 |
result = model.transcribe(audio_path)
|
|
|
53 |
return result["text"]
|
54 |
except Exception as e:
|
55 |
+
raise RuntimeError(f"Error during transcription: {e}")
|
|
|
56 |
|
57 |
if __name__ == '__main__':
|
58 |
if not os.path.exists('uploads'):
|
59 |
os.makedirs('uploads')
|
60 |
+
app.run(debug=True)
|