-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fe6878
commit e57cc13
Showing
3 changed files
with
85 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,82 @@ | ||
from flask import Blueprint, request, jsonify | ||
from flask import Blueprint, request, jsonify, after_this_request | ||
import uuid | ||
import threading | ||
import logging | ||
from services.transcription import process_transcription | ||
from services.authentication import authenticate | ||
from services.webhook import send_webhook | ||
|
||
transcribe_bp = Blueprint('transcribe', __name__) | ||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
@transcribe_bp.route('/transcribe', methods=['POST']) | ||
@authenticate | ||
def transcribe_media(): | ||
def transcribe(): | ||
data = request.json | ||
job_id = str(uuid.uuid4()) # Generate a job ID for tracking (optional) | ||
print(f"Processing Job ID: {job_id}") | ||
|
||
try: | ||
# Call the transcription process directly | ||
result = process_transcription(data['media_url'], data['output']) | ||
return jsonify({"job_id": job_id, "result": result}), 200 | ||
except Exception as e: | ||
return jsonify({"job_id": job_id, "error": str(e)}), 500 | ||
media_url = data.get('media_url') | ||
output = data.get('output', 'transcript').lower() | ||
webhook_url = data.get('webhook_url') | ||
id = data.get('id') | ||
|
||
logger.info(f"Received transcription request: media_url={media_url}, output={output}, webhook_url={webhook_url}, id={id}") | ||
|
||
if not media_url: | ||
logger.error("Missing media_url parameter in request") | ||
return jsonify({"error": "Missing media_url parameter"}), 400 | ||
|
||
# Check if either webhook_url or id is provided without the other | ||
if (webhook_url and not id) or (id and not webhook_url): | ||
logger.warning("Either webhook_url or id is missing in the request") | ||
return jsonify({"response": "transcript or srt text is missing"}), 200 | ||
|
||
job_id = str(uuid.uuid4()) | ||
logger.info(f"Generated job_id: {job_id}") | ||
|
||
def process_and_notify(media_url, output, webhook_url, id, job_id): | ||
try: | ||
logger.info(f"Job {job_id}: Starting transcription process for {media_url}") | ||
result = process_transcription(media_url, output) | ||
logger.info(f"Job {job_id}: Transcription process completed successfully") | ||
|
||
if webhook_url: | ||
logger.info(f"Job {job_id}: Sending success webhook to {webhook_url}") | ||
send_webhook(webhook_url, { | ||
"endpoint": "/transcribe", | ||
"id": id, | ||
"response": result, | ||
"code": 200, | ||
"message": "success" | ||
}) | ||
except Exception as e: | ||
logger.error(f"Job {job_id}: Error during transcription - {e}") | ||
if webhook_url: | ||
logger.info(f"Job {job_id}: Sending failure webhook to {webhook_url}") | ||
send_webhook(webhook_url, { | ||
"endpoint": "/transcribe", | ||
"id": id, | ||
"response": None, | ||
"code": 500, | ||
"message": str(e) | ||
}) | ||
|
||
@after_this_request | ||
def start_background_processing(response): | ||
logger.info(f"Job {job_id}: Starting background processing thread") | ||
thread = threading.Thread(target=process_and_notify, args=(media_url, output, webhook_url, id, job_id)) | ||
thread.start() | ||
return response | ||
|
||
# If webhook_url and id are provided, return 202 Accepted | ||
if webhook_url and id: | ||
logger.info(f"Job {job_id}: Returning 202 Accepted response and processing in background") | ||
return jsonify({"message": "processing"}), 202 | ||
else: | ||
try: | ||
logger.info(f"Job {job_id}: No webhook provided, processing synchronously") | ||
result = process_transcription(media_url, output) | ||
logger.info(f"Job {job_id}: Returning transcription result") | ||
return jsonify({"response": result}), 200 | ||
except Exception as e: | ||
logger.error(f"Job {job_id}: Error during synchronous transcription - {e}") | ||
return jsonify({"message": str(e)}), 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters