Download backend/api/api.py from peter288/django: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/spaces/peter288/django/resolve/main/backend/api/api.py
- Command line
-
hf download hf://spaces/peter288/django/backend/api/api.py
-
curl -L -o api.py https://huggingface.co/spaces/peter288/django/resolve/main/backend/api/api.py
1.51 kB
| from ninja import NinjaAPI, File | |
| from ninja.files import UploadedFile | |
| from django.http import HttpResponse | |
| from .midi_utils import process_midi # Import from the same directory | |
| api = NinjaAPI( | |
| title="MIDI Processor API (Django Ninja)", | |
| version="2.0.0", | |
| description="A robust MIDI processing API built with Django Ninja." | |
| ) | |
| def home(request): | |
| """ | |
| A simple welcome endpoint. | |
| The interactive docs will be at /api/docs | |
| """ | |
| return {"message": "Welcome to the Django Ninja MIDI API!", "docs_url": "/api/docs"} | |
| def process_midi_file(request, file: UploadedFile = File(...)): | |
| """ | |
| Receives a MIDI file, transposes it, and returns the processed file. | |
| """ | |
| try: | |
| # Check filename | |
| if not file.name.lower().endswith(('.mid', '.midi')): | |
| return api.create_response(request, {"error": "Invalid file type"}, status=400) | |
| # Read file bytes | |
| midi_bytes = file.read() | |
| # Process the midi data | |
| processed_buffer = process_midi(midi_bytes) | |
| # Create an HTTP response with the processed file | |
| response = HttpResponse( | |
| processed_buffer.getvalue(), | |
| content_type='audio/midi' | |
| ) | |
| response['Content-Disposition'] = f'attachment; filename="processed_{file.name}"' | |
| return response | |
| except Exception as e: | |
| return api.create_response(request, {"error": str(e)}, status=500) |