Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- main.py +13 -46
- requirements.txt +1 -1
main.py
CHANGED
|
@@ -8,31 +8,26 @@ from pathlib import Path
|
|
| 8 |
|
| 9 |
app = FastAPI(title="VocalizeAPI")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def get_audio_info(file_path):
|
| 13 |
-
"""Returns size in MB and duration in seconds."""
|
| 14 |
size_mb = os.path.getsize(file_path) / (1024 * 1024)
|
| 15 |
-
# pydub will automatically find the FFmpeg installed by Docker
|
| 16 |
audio = AudioSegment.from_file(file_path)
|
| 17 |
duration_sec = len(audio) / 1000.0
|
| 18 |
return size_mb, duration_sec
|
| 19 |
|
| 20 |
@app.post("/audio-to-text")
|
| 21 |
async def audio_to_text(file: UploadFile = File(...)):
|
| 22 |
-
# 1. Create a temporary directory for processing
|
| 23 |
temp_dir = tempfile.mkdtemp()
|
| 24 |
-
|
| 25 |
-
# Define file paths inside temp folder
|
| 26 |
input_ext = Path(file.filename).suffix.lower() or ".ogg"
|
| 27 |
input_path = os.path.join(temp_dir, f"input{input_ext}")
|
| 28 |
wav_path = os.path.join(temp_dir, "converted.wav")
|
| 29 |
|
| 30 |
try:
|
| 31 |
-
#
|
| 32 |
with open(input_path, "wb") as buffer:
|
| 33 |
shutil.copyfileobj(file.file, buffer)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
if input_ext == ".wav":
|
| 37 |
target_path = input_path
|
| 38 |
else:
|
|
@@ -40,62 +35,34 @@ async def audio_to_text(file: UploadFile = File(...)):
|
|
| 40 |
audio.export(wav_path, format="wav")
|
| 41 |
target_path = wav_path
|
| 42 |
|
| 43 |
-
#
|
| 44 |
size_mb, duration = get_audio_info(target_path)
|
| 45 |
|
| 46 |
if duration > 60:
|
| 47 |
-
return {
|
| 48 |
-
"status": False,
|
| 49 |
-
"error": "File Rejected: It's longer than 60s (Google Free Limit).",
|
| 50 |
-
"result": ""
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
if size_mb > 10:
|
| 54 |
-
return {
|
| 55 |
-
"status": False,
|
| 56 |
-
"error": "Error: File is too large (>10MB).",
|
| 57 |
-
"result": ""
|
| 58 |
-
}
|
| 59 |
|
| 60 |
-
#
|
| 61 |
r = sr.Recognizer()
|
| 62 |
with sr.AudioFile(target_path) as source:
|
| 63 |
audio_data = r.record(source)
|
| 64 |
try:
|
| 65 |
-
# Use Google free API
|
| 66 |
text = r.recognize_google(audio_data)
|
| 67 |
-
return {
|
| 68 |
-
"status": True,
|
| 69 |
-
"error": None,
|
| 70 |
-
"result": text
|
| 71 |
-
}
|
| 72 |
except sr.UnknownValueError:
|
| 73 |
-
return {
|
| 74 |
-
"status": False,
|
| 75 |
-
"error": "I could not understand the audio (Low quality or silent).",
|
| 76 |
-
"result": ""
|
| 77 |
-
}
|
| 78 |
except sr.RequestError:
|
| 79 |
-
return {
|
| 80 |
-
"status": False,
|
| 81 |
-
"error": "API-Key Error: Your internet is down or account limited.",
|
| 82 |
-
"result": ""
|
| 83 |
-
}
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
return {
|
| 89 |
-
"status": False,
|
| 90 |
-
"error": "System Error: Processing failed.",
|
| 91 |
-
"result": ""
|
| 92 |
-
}
|
| 93 |
|
| 94 |
finally:
|
| 95 |
-
# 6. Cleanup temp folder completely
|
| 96 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
import uvicorn
|
| 100 |
-
# 7860 is
|
| 101 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI(title="VocalizeAPI")
|
| 10 |
|
| 11 |
+
# Helper to get audio info (pydub uses Linux ffmpeg automatically)
|
| 12 |
def get_audio_info(file_path):
|
|
|
|
| 13 |
size_mb = os.path.getsize(file_path) / (1024 * 1024)
|
|
|
|
| 14 |
audio = AudioSegment.from_file(file_path)
|
| 15 |
duration_sec = len(audio) / 1000.0
|
| 16 |
return size_mb, duration_sec
|
| 17 |
|
| 18 |
@app.post("/audio-to-text")
|
| 19 |
async def audio_to_text(file: UploadFile = File(...)):
|
|
|
|
| 20 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
| 21 |
input_ext = Path(file.filename).suffix.lower() or ".ogg"
|
| 22 |
input_path = os.path.join(temp_dir, f"input{input_ext}")
|
| 23 |
wav_path = os.path.join(temp_dir, "converted.wav")
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
# Save upload
|
| 27 |
with open(input_path, "wb") as buffer:
|
| 28 |
shutil.copyfileobj(file.file, buffer)
|
| 29 |
|
| 30 |
+
# Convert to WAV if needed
|
| 31 |
if input_ext == ".wav":
|
| 32 |
target_path = input_path
|
| 33 |
else:
|
|
|
|
| 35 |
audio.export(wav_path, format="wav")
|
| 36 |
target_path = wav_path
|
| 37 |
|
| 38 |
+
# Validate size and duration
|
| 39 |
size_mb, duration = get_audio_info(target_path)
|
| 40 |
|
| 41 |
if duration > 60:
|
| 42 |
+
return {"status": False, "error": "File Rejected: It's longer than 60s (Google Free Limit).", "result": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if size_mb > 10:
|
| 44 |
+
return {"status": False, "error": "Error: File is too large (>10MB).", "result": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Transcribe
|
| 47 |
r = sr.Recognizer()
|
| 48 |
with sr.AudioFile(target_path) as source:
|
| 49 |
audio_data = r.record(source)
|
| 50 |
try:
|
|
|
|
| 51 |
text = r.recognize_google(audio_data)
|
| 52 |
+
return {"status": True, "error": None, "result": text}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
except sr.UnknownValueError:
|
| 54 |
+
return {"status": False, "error": "I could not understand the audio (Low quality or silent).", "result": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except sr.RequestError:
|
| 56 |
+
return {"status": False, "error": "API-Key Error: Your internet is down or account limited.", "result": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"Server Log Error: {e}")
|
| 60 |
+
return {"status": False, "error": "System Error: Processing failed.", "result": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
finally:
|
|
|
|
| 63 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
import uvicorn
|
| 67 |
+
# Port 7860 is mandatory for Hugging Face
|
| 68 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
|
| 4 |
pydub
|
| 5 |
python-multipart
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
+
SpeechRecognition
|
| 4 |
pydub
|
| 5 |
python-multipart
|