Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse, FileResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import numpy as np
|
|
@@ -12,6 +12,8 @@ from transformers import Wav2Vec2ForCTC, AutoProcessor
|
|
| 12 |
from pathlib import Path
|
| 13 |
from moviepy.editor import VideoFileClip
|
| 14 |
import magic # For MIME type detection
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Import functions from other modules
|
| 17 |
from asr import transcribe, ASR_LANGUAGES
|
|
@@ -45,12 +47,19 @@ def extract_audio(input_bytes):
|
|
| 45 |
if mime_type.startswith('audio/'):
|
| 46 |
return sf.read(io.BytesIO(input_bytes))
|
| 47 |
elif mime_type.startswith('video/'):
|
| 48 |
-
with
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
audio = video.audio
|
| 51 |
audio_array = audio.to_soundarray()
|
| 52 |
sample_rate = audio.fps
|
|
|
|
| 53 |
return audio_array, sample_rate
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
raise ValueError(f"Unsupported MIME type: {mime_type}")
|
| 56 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse, FileResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import numpy as np
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
from moviepy.editor import VideoFileClip
|
| 14 |
import magic # For MIME type detection
|
| 15 |
+
import tempfile
|
| 16 |
+
import os
|
| 17 |
|
| 18 |
# Import functions from other modules
|
| 19 |
from asr import transcribe, ASR_LANGUAGES
|
|
|
|
| 47 |
if mime_type.startswith('audio/'):
|
| 48 |
return sf.read(io.BytesIO(input_bytes))
|
| 49 |
elif mime_type.startswith('video/'):
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.webm') as temp_file:
|
| 51 |
+
temp_file.write(input_bytes)
|
| 52 |
+
temp_file_path = temp_file.name
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
video = VideoFileClip(temp_file_path)
|
| 56 |
audio = video.audio
|
| 57 |
audio_array = audio.to_soundarray()
|
| 58 |
sample_rate = audio.fps
|
| 59 |
+
video.close()
|
| 60 |
return audio_array, sample_rate
|
| 61 |
+
finally:
|
| 62 |
+
os.unlink(temp_file_path)
|
| 63 |
else:
|
| 64 |
raise ValueError(f"Unsupported MIME type: {mime_type}")
|
| 65 |
|