-
Notifications
You must be signed in to change notification settings - Fork 981
Implement Runpod API integration for transcription and add webhook ha… #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hvignesh18197
wants to merge
4
commits into
stephengpope:main
Choose a base branch
from
hvignesh18197:Vignesh/Runpod-whisper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
42f463e
Implement Runpod API integration for transcription and add webhook ha…
hvignesh18197 c04d346
Remove libsvtav1-dev from Dockerfile dependencies
hvignesh18197 8b71d8d
Update environment configuration for S3 and RunPod integration in .en…
hvignesh18197 abe5230
Remove unused Whisper model configuration from environment files
hvignesh18197 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| version: '3.8' | ||
|
|
||
| services: | ||
| nca-toolkit: | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile | ||
| environment: | ||
| - API_KEY=${API_KEY} | ||
| - PYTHONUNBUFFERED=1 | ||
| - WHISPER_CACHE_DIR=/app/whisper_cache | ||
| - S3_SECRET_KEY=${S3_SECRET_KEY} | ||
| - RUNPOD_API_KEY=${RUNPOD_API_KEY} | ||
| - S3_BUCKET_NAME=${S3_BUCKET_NAME} | ||
| - S3_ENDPOINT_URL=${S3_ENDPOINT_URL} | ||
| - S3_REGION=${S3_REGION} | ||
| - S3_ACCESS_KEY=${S3_ACCESS_KEY} | ||
| - USE_RUNPOD=true | ||
| working_dir: /app | ||
| ports: | ||
| - "8080:8080" | ||
| restart: unless-stopped |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| # Copyright (c) 2025 Stephen G. Pope | ||
| # | ||
| # This program is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License along | ||
| # with this program; if not, write to the Free Software Foundation, Inc., | ||
| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
|
||
| from flask import Blueprint, request, jsonify | ||
| import logging | ||
| import json | ||
| from typing import Dict, Any | ||
|
|
||
| # Set up logging | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Create the blueprint | ||
| webhook_bp = Blueprint('webhook', __name__, url_prefix='/v1/webhook') | ||
|
|
||
| # In-memory storage for webhook results (in production, use Redis or database) | ||
| webhook_results = {} | ||
|
|
||
| @webhook_bp.route('/runpod', methods=['POST']) | ||
| def handle_runpod_webhook(): | ||
| """ | ||
| Handle webhook callbacks from Runpod API. | ||
|
|
||
| This endpoint receives the results of async Runpod transcription jobs | ||
| and stores them for later retrieval. | ||
| """ | ||
| try: | ||
| # Get the JSON payload from Runpod | ||
| data = request.get_json() | ||
|
|
||
| if not data: | ||
| logger.error("No JSON data received in webhook") | ||
| return jsonify({"error": "No JSON data received"}), 400 | ||
|
|
||
| # Extract job information | ||
| job_id = data.get('id') | ||
| status = data.get('status') | ||
| output = data.get('output') | ||
| error = data.get('error') | ||
|
|
||
| logger.info(f"Received webhook for job {job_id} with status: {status}") | ||
|
|
||
| if not job_id: | ||
| logger.error("No job ID in webhook data") | ||
| return jsonify({"error": "No job ID provided"}), 400 | ||
|
|
||
| # Store the result | ||
| webhook_results[job_id] = { | ||
| 'job_id': job_id, | ||
| 'status': status, | ||
| 'output': output, | ||
| 'error': error, | ||
| 'timestamp': data.get('timestamp'), | ||
| 'raw_data': data | ||
| } | ||
|
|
||
| if status == 'COMPLETED': | ||
| logger.info(f"Job {job_id} completed successfully") | ||
| elif status == 'FAILED': | ||
| logger.error(f"Job {job_id} failed: {error}") | ||
| else: | ||
| logger.info(f"Job {job_id} status update: {status}") | ||
|
|
||
| # Return success response | ||
| return jsonify({ | ||
| "status": "received", | ||
| "job_id": job_id, | ||
| "message": f"Webhook processed for job {job_id}" | ||
| }), 200 | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error processing webhook: {str(e)}") | ||
| return jsonify({"error": "Internal server error"}), 500 | ||
|
|
||
| @webhook_bp.route('/runpod/status/<job_id>', methods=['GET']) | ||
| def get_webhook_result(job_id: str): | ||
| """ | ||
| Get the result of a Runpod job from webhook storage. | ||
|
|
||
| Args: | ||
| job_id: The job ID to get results for | ||
|
|
||
| Returns: | ||
| JSON response with job results or status | ||
| """ | ||
| try: | ||
| if job_id not in webhook_results: | ||
| return jsonify({ | ||
| "error": "Job not found", | ||
| "job_id": job_id, | ||
| "message": "Job ID not found in webhook results" | ||
| }), 404 | ||
|
|
||
| result = webhook_results[job_id] | ||
|
|
||
| if result['status'] == 'COMPLETED': | ||
| # Transform the output to Whisper format if needed | ||
| from services.runpod_whisper import runpod_client | ||
|
|
||
| try: | ||
| transformed_result = runpod_client._transform_runpod_response(result['output']) | ||
| return jsonify({ | ||
| "status": "completed", | ||
| "job_id": job_id, | ||
| "result": transformed_result | ||
| }), 200 | ||
| except Exception as transform_error: | ||
| logger.error(f"Error transforming result for job {job_id}: {str(transform_error)}") | ||
| return jsonify({ | ||
| "status": "completed", | ||
| "job_id": job_id, | ||
| "raw_output": result['output'], | ||
| "transform_error": str(transform_error) | ||
| }), 200 | ||
|
|
||
| elif result['status'] == 'FAILED': | ||
| return jsonify({ | ||
| "status": "failed", | ||
| "job_id": job_id, | ||
| "error": result['error'] | ||
| }), 200 | ||
|
|
||
| else: | ||
| return jsonify({ | ||
| "status": result['status'], | ||
| "job_id": job_id, | ||
| "message": f"Job is {result['status']}" | ||
| }), 200 | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error getting webhook result for job {job_id}: {str(e)}") | ||
| return jsonify({"error": "Internal server error"}), 500 | ||
|
|
||
| @webhook_bp.route('/runpod/jobs', methods=['GET']) | ||
| def list_webhook_jobs(): | ||
| """ | ||
| List all jobs stored in webhook results. | ||
|
|
||
| Returns: | ||
| JSON response with list of job IDs and their statuses | ||
| """ | ||
| try: | ||
| jobs = [] | ||
| for job_id, result in webhook_results.items(): | ||
| jobs.append({ | ||
| "job_id": job_id, | ||
| "status": result['status'], | ||
| "timestamp": result['timestamp'] | ||
| }) | ||
|
|
||
| return jsonify({ | ||
| "jobs": jobs, | ||
| "total": len(jobs) | ||
| }), 200 | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error listing webhook jobs: {str(e)}") | ||
| return jsonify({"error": "Internal server error"}), 500 | ||
|
|
||
| @webhook_bp.route('/runpod/cleanup', methods=['POST']) | ||
| def cleanup_webhook_results(): | ||
| """ | ||
| Clean up completed or failed jobs from webhook storage. | ||
|
|
||
| Returns: | ||
| JSON response with cleanup results | ||
| """ | ||
| try: | ||
| data = request.get_json() or {} | ||
| cleanup_completed = data.get('cleanup_completed', True) | ||
| cleanup_failed = data.get('cleanup_failed', True) | ||
|
|
||
| cleaned_jobs = [] | ||
| for job_id in list(webhook_results.keys()): | ||
| result = webhook_results[job_id] | ||
| should_clean = False | ||
|
|
||
| if cleanup_completed and result['status'] == 'COMPLETED': | ||
| should_clean = True | ||
| elif cleanup_failed and result['status'] == 'FAILED': | ||
| should_clean = True | ||
|
|
||
| if should_clean: | ||
| del webhook_results[job_id] | ||
| cleaned_jobs.append(job_id) | ||
|
|
||
| return jsonify({ | ||
| "cleaned_jobs": cleaned_jobs, | ||
| "count": len(cleaned_jobs), | ||
| "remaining_jobs": len(webhook_results) | ||
| }), 200 | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error cleaning up webhook results: {str(e)}") | ||
| return jsonify({"error": "Internal server error"}), 500 | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using in-memory storage for webhook results will lose data on application restart. The comment mentions using Redis or database in production, but this should be implemented or at least have proper warning documentation.