first commit
This commit is contained in:
142
WebAdmin/create_text_video.py
Executable file
142
WebAdmin/create_text_video.py
Executable file
@@ -0,0 +1,142 @@
|
||||
import requests
|
||||
from django.http import JsonResponse
|
||||
from .models import VideoGeneration
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
def text_to_video(request):
|
||||
"""
|
||||
接口:根据文本描述生成视频
|
||||
"""
|
||||
if request.method == "POST":
|
||||
try:
|
||||
user = request.user
|
||||
data = request.POST
|
||||
|
||||
# 生成视频ID
|
||||
video_id = generate_video_id(user.id)
|
||||
|
||||
# 获取参数
|
||||
text_prompt = data.get('text_prompt')
|
||||
model = data.get('model', 'gen3')
|
||||
width = int(data.get('width', 640))
|
||||
height = int(data.get('height', 480))
|
||||
motion = int(data.get('motion', 5))
|
||||
seed = int(data.get('seed', 0))
|
||||
upscale = data.get('upscale', 'false') == 'true'
|
||||
interpolate = data.get('interpolate', 'false') == 'true'
|
||||
callback_url = data.get('callback_url', '')
|
||||
time_duration = int(data.get('time', 5))
|
||||
|
||||
# 准备POST请求数据
|
||||
payload = {
|
||||
"text_prompt": text_prompt,
|
||||
"model": model,
|
||||
"width": width,
|
||||
"height": height,
|
||||
"motion": motion,
|
||||
"seed": seed,
|
||||
"upscale": upscale,
|
||||
"interpolate": interpolate,
|
||||
"callback_url": callback_url,
|
||||
"time": time_duration
|
||||
}
|
||||
|
||||
# 发送请求到API
|
||||
response = requests.post('https://api.aivideoapi.com/runway/generate/text', json=payload)
|
||||
|
||||
# 解析响应数据
|
||||
if response.status_code == 200:
|
||||
res_data = response.json()
|
||||
uuid = res_data.get('uuid')
|
||||
|
||||
# 保存生成记录到数据库
|
||||
video_generation = VideoGeneration.objects.create(
|
||||
video_id=video_id,
|
||||
user=user,
|
||||
text=text_prompt,
|
||||
status='in_progress',
|
||||
pid=uuid,
|
||||
ratio=f"{width}:{height}"
|
||||
)
|
||||
video_generation.save()
|
||||
|
||||
return JsonResponse({'code': 200, 'message': '请求成功', 'data': {'pid': uuid}})
|
||||
|
||||
else:
|
||||
return JsonResponse({'code': response.status_code, 'message': 'API请求失败', 'data': {}})
|
||||
|
||||
except Exception as e:
|
||||
return JsonResponse({'code': 500, 'message': f'内部错误: {str(e)}', 'data': {}})
|
||||
|
||||
return JsonResponse({'code': 405, 'message': '请求方法错误', 'data': {}})
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
def image_to_video(request):
|
||||
"""
|
||||
接口:根据图片生成视频
|
||||
"""
|
||||
if request.method == "POST":
|
||||
try:
|
||||
user = request.user
|
||||
data = request.POST
|
||||
|
||||
# 生成视频ID
|
||||
video_id = generate_video_id(user.id)
|
||||
|
||||
# 获取参数
|
||||
text_prompt = data.get('text_prompt')
|
||||
model = data.get('model', 'gen3')
|
||||
img_prompt = data.get('img_prompt')
|
||||
image_as_end_frame = data.get('image_as_end_frame', 'false') == 'true'
|
||||
motion = int(data.get('motion', 5))
|
||||
seed = int(data.get('seed', 0))
|
||||
upscale = data.get('upscale', 'false') == 'true'
|
||||
interpolate = data.get('interpolate', 'false') == 'true'
|
||||
callback_url = data.get('callback_url', '')
|
||||
time_duration = int(data.get('time', 5))
|
||||
|
||||
# 准备POST请求数据
|
||||
payload = {
|
||||
"text_prompt": text_prompt,
|
||||
"model": model,
|
||||
"img_prompt": img_prompt,
|
||||
"image_as_end_frame": image_as_end_frame,
|
||||
"motion": motion,
|
||||
"seed": seed,
|
||||
"upscale": upscale,
|
||||
"interpolate": interpolate,
|
||||
"callback_url": callback_url,
|
||||
"time": time_duration
|
||||
}
|
||||
|
||||
# 发送请求到API
|
||||
response = requests.post('https://api.aivideoapi.com/runway/generate/imageDescription', json=payload)
|
||||
|
||||
# 解析响应数据
|
||||
if response.status_code == 200:
|
||||
res_data = response.json()
|
||||
uuid = res_data.get('uuid')
|
||||
|
||||
# 保存生成记录到数据库
|
||||
video_generation = VideoGeneration.objects.create(
|
||||
video_id=video_id,
|
||||
user=user,
|
||||
text=text_prompt,
|
||||
status='in_progress',
|
||||
pid=uuid,
|
||||
ratio="auto"
|
||||
)
|
||||
video_generation.save()
|
||||
|
||||
return JsonResponse({'code': 200, 'message': '请求成功', 'data': {'pid': uuid}})
|
||||
|
||||
else:
|
||||
return JsonResponse({'code': response.status_code, 'message': 'API请求失败', 'data': {}})
|
||||
|
||||
except Exception as e:
|
||||
return JsonResponse({'code': 500, 'message': f'内部错误: {str(e)}', 'data': {}})
|
||||
|
||||
return JsonResponse({'code': 405, 'message': '请求方法错误', 'data': {}})
|
||||
Reference in New Issue
Block a user