-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranscribe_api.py
62 lines (44 loc) · 1.9 KB
/
transcribe_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import asyncio
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from concurrent.futures import ThreadPoolExecutor, as_completed
from utils.measure_time import measure_execution_time
import uvicorn
import requests
from services.audio_transcription_service import AudioTranscription
from services.video_transcription_service import Video2AudioConverter
app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_headers=['*'], allow_methods=['*'])
output_path = './data'
n8n_webhook = "http://localhost:5678/webhook-test/8895e90e-a0d7-42b9-8aed-db2a21fe1e29"
class MessagePayload(BaseModel):
url: str
def invoke_transcription(url):
# download the audio from the url
Video2AudioConverter().download_youtube_audio(url, output_path)
# transcribe the audio into text
is_audio_transcribed = AudioTranscription().transcribe(audio_file_dir=output_path, is_log_enabled=True)
if is_audio_transcribed:
return {'is_audio_transcribed': True}
else:
return {'is_audio_transcribed': False}
def invoke_n8n_workflow():
# invoke n8n workflow using webhook
requests.get(url=n8n_webhook)
@app.post("/api/v1/transcribe")
async def invoke_agents(payload: MessagePayload):
@measure_execution_time
async def process_transcription():
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(invoke_transcription, url=payload.url)]
for future in as_completed(futures):
result = future.result()
if result["is_audio_transcribed"]:
invoke_n8n_workflow()
# Schedule the transcription task to run asynchronously
asyncio.create_task(process_transcription())
# Respond immediately
return {"is_transcription_started": True}
if __name__ == "__main__":
uvicorn.run(app=app, host="127.0.0.1", port=8000)