Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -42,14 +42,15 @@ def translate(text, srcLang, tgtLang):
|
|
42 |
print(response)
|
43 |
return response.text
|
44 |
|
|
|
|
|
45 |
from tempfile import NamedTemporaryFile
|
46 |
from fastapi import UploadFile, Form, File
|
47 |
from pathlib import Path
|
48 |
from typing import Annotated
|
49 |
-
import
|
50 |
-
import aiofiles
|
51 |
|
52 |
-
|
53 |
|
54 |
app = FastAPI(
|
55 |
title="Real-Time Audio Processor",
|
@@ -66,26 +67,47 @@ async def test(file: UploadFile = File(...),
|
|
66 |
# content = await file.read() # async read
|
67 |
# await out_file.write(content) # async write
|
68 |
|
69 |
-
|
70 |
try:
|
71 |
-
content = await file.read()
|
72 |
-
with open(
|
73 |
f.write(content)
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
finally:
|
77 |
-
file.file
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
print(f"Successfully uploaded {file.filename}")
|
80 |
-
|
81 |
-
result = pipe("temp_audio.wav",
|
82 |
-
batch_size=8,
|
83 |
-
return_timestamps=True,
|
84 |
-
generate_kwargs={"language": "tagalog",
|
85 |
-
"return_timestamps": True,})
|
86 |
-
translatedResult = translate(result['text'], srcLang=srcLang, tgtLang=tgtLang)
|
87 |
-
os.remove("temp_audio.wav")
|
88 |
-
return {"transcribed_text": result['text'], "translated_text": translatedResult, "srcLang": srcLang, "tgtLang": tgtLang}
|
89 |
|
90 |
@app.post("/translateText/")
|
91 |
async def test(text: str,
|
|
|
42 |
print(response)
|
43 |
return response.text
|
44 |
|
45 |
+
import shutil
|
46 |
+
import aiofiles
|
47 |
from tempfile import NamedTemporaryFile
|
48 |
from fastapi import UploadFile, Form, File
|
49 |
from pathlib import Path
|
50 |
from typing import Annotated
|
51 |
+
from silero_vad import load_silero_vad, read_audio, get_speech_timestamps, save_audio, collect_chunks
|
|
|
52 |
|
53 |
+
model = load_silero_vad()
|
54 |
|
55 |
app = FastAPI(
|
56 |
title="Real-Time Audio Processor",
|
|
|
67 |
# content = await file.read() # async read
|
68 |
# await out_file.write(content) # async write
|
69 |
|
|
|
70 |
try:
|
71 |
+
content = await file.read()
|
72 |
+
with open(file.filename, 'wb') as f:
|
73 |
f.write(content)
|
74 |
+
print(f"Successfully uploaded {file.filename}")
|
75 |
+
|
76 |
+
wav = read_audio(file.filename)
|
77 |
+
speech_timestamps = get_speech_timestamps(wav, model)
|
78 |
+
save_audio(
|
79 |
+
"only_speech.wav",
|
80 |
+
collect_chunks(speech_timestamps, wav),
|
81 |
+
sampling_rate=16000
|
82 |
+
)
|
83 |
+
|
84 |
+
result = pipe(
|
85 |
+
"only_speech.wav", # Transcribe audio
|
86 |
+
batch_size=8,
|
87 |
+
return_timestamps=True,
|
88 |
+
generate_kwargs={"language": "tagalog","return_timestamps": True,}
|
89 |
+
)
|
90 |
+
translatedResult = translate(result['text'], srcLang=srcLang, tgtLang=tgtLang)
|
91 |
+
|
92 |
+
return {
|
93 |
+
"transcribed_text": result['text'],
|
94 |
+
"translated_text": translatedResult,
|
95 |
+
"srcLang": srcLang,
|
96 |
+
"tgtLang": tgtLang
|
97 |
+
}
|
98 |
+
|
99 |
+
except Exception as error:
|
100 |
+
print("Error: ", str(error)))
|
101 |
+
raise HTTPException(status_code=500, detail=str(error))
|
102 |
+
|
103 |
finally:
|
104 |
+
if file.file:
|
105 |
+
file.file.close()
|
106 |
+
if os.path.exists(file.filename)
|
107 |
+
os.remove(file.filename)
|
108 |
+
if os.path.exists("only_speech.wav")
|
109 |
+
os.remove("only_speech.wav")
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
@app.post("/translateText/")
|
113 |
async def test(text: str,
|