views.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # # Standard Library Imports
  2. import os
  3. import time
  4. # # Third-Party Library Imports
  5. import requests
  6. # # Django Imports
  7. from django.shortcuts import render
  8. from django.conf import settings
  9. from django.http import JsonResponse
  10. from django.views.decorators.csrf import csrf_exempt
  11. from django.core.files.storage import default_storage
  12. from django.core.files.base import ContentFile
  13. from .decorators import login_required
  14. @login_required
  15. def img_process(request):
  16. return render(request, 'img-upload.html')
  17. MINIMAX_API_KEY = settings.MINIMAX_API_KEY
  18. VIDEO_GEN_URL = "https://api.minimax.io/v1/video_generation"
  19. QUERY_URL = "https://api.minimax.io/v1/query/video_generation"
  20. FILES_URL = "https://api.minimax.io/v1/files/retrieve"
  21. POLL_INTERVAL = 5 # seconds
  22. @csrf_exempt
  23. def process_images(request):
  24. if request.method != "POST":
  25. return JsonResponse({"error": "Invalid request method."}, status=405)
  26. first_frame = request.FILES.get("first_frame")
  27. last_frame = request.FILES.get("last_frame")
  28. if not first_frame or not last_frame:
  29. return JsonResponse({"error": "Both first_frame and last_frame are required."}, status=400)
  30. try:
  31. # --- Save uploaded images locally ---
  32. first_path = default_storage.save(f"uploads/{first_frame.name}", first_frame)
  33. last_path = default_storage.save(f"uploads/{last_frame.name}", last_frame)
  34. first_filename = os.path.basename(first_path)
  35. last_filename = os.path.basename(last_path)
  36. # --- Build public URLs using ngrok ---
  37. # Replace this with your current ngrok URL
  38. NGROK_URL = "https://postcartilaginous-kyler-nonrun.ngrok-free.dev"
  39. first_url = f"{NGROK_URL}{settings.MEDIA_URL}{first_filename}"
  40. last_url = f"{NGROK_URL}{settings.MEDIA_URL}{last_filename}"
  41. # --- Call MiniMax API ---
  42. headers = {"Authorization": f"Bearer {MINIMAX_API_KEY}"}
  43. payload = {
  44. "prompt": "Generate a smooth transition between two frames.",
  45. "first_frame_image": first_url,
  46. "last_frame_image": last_url,
  47. "model": "MiniMax-Hailuo-02",
  48. "duration": 6,
  49. "resolution": "1080P"
  50. }
  51. response = requests.post(VIDEO_GEN_URL, headers=headers, json=payload)
  52. response.raise_for_status()
  53. print("Response", response.json())
  54. task_id = response.json().get("task_id")
  55. if not task_id:
  56. return JsonResponse({"error": "Failed to get task_id from MiniMax API."}, status=500)
  57. # --- Poll MiniMax until task completes ---
  58. file_id = None
  59. while not file_id:
  60. time.sleep(POLL_INTERVAL)
  61. status_resp = requests.get(QUERY_URL, headers=headers, params={"task_id": task_id})
  62. status_resp.raise_for_status()
  63. status_data = status_resp.json()
  64. status = status_data.get("status")
  65. if status == "Success":
  66. file_id = status_data.get("file_id")
  67. elif status == "Fail":
  68. return JsonResponse({"error": status_data.get("error_message", "Video generation failed")}, status=500)
  69. # --- Get final video download URL ---
  70. download_resp = requests.get(FILES_URL, headers=headers, params={"file_id": file_id})
  71. download_resp.raise_for_status()
  72. download_url = download_resp.json()["file"]["download_url"]
  73. return JsonResponse({"video_url": download_url})
  74. except Exception as e:
  75. return JsonResponse({"error": str(e)}, status=500)