Create Comp
Browse files
Comp
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
api_key = "Fill in the API Key"
|
| 7 |
+
|
| 8 |
+
prompt = "Description of your video"
|
| 9 |
+
model = "video-01"
|
| 10 |
+
output_file_name = "output.mp4" #Please enter the save path for the generated video here
|
| 11 |
+
|
| 12 |
+
def invoke_video_generation()->str:
|
| 13 |
+
print("-----------------Submit video generation task-----------------")
|
| 14 |
+
url = "https://api.minimaxi.chat/v1/video_generation"
|
| 15 |
+
payload = json.dumps({
|
| 16 |
+
"prompt": prompt,
|
| 17 |
+
"model": model
|
| 18 |
+
})
|
| 19 |
+
headers = {
|
| 20 |
+
'authorization': 'Bearer ' + api_key,
|
| 21 |
+
'content-type': 'application/json',
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
| 25 |
+
print(response.text)
|
| 26 |
+
task_id = response.json()['task_id']
|
| 27 |
+
print("Video generation task submitted successfully, task ID.:"+task_id)
|
| 28 |
+
return task_id
|
| 29 |
+
|
| 30 |
+
def query_video_generation(task_id: str):
|
| 31 |
+
url = "https://api.minimaxi.chat/v1/query/video_generation?task_id="+task_id
|
| 32 |
+
headers = {
|
| 33 |
+
'authorization': 'Bearer ' + api_key
|
| 34 |
+
}
|
| 35 |
+
response = requests.request("GET", url, headers=headers)
|
| 36 |
+
status = response.json()['status']
|
| 37 |
+
if status == 'Preparing':
|
| 38 |
+
print("...Preparing...")
|
| 39 |
+
return "", 'Preparing'
|
| 40 |
+
elif status == 'Queueing':
|
| 41 |
+
print("...In the queue...")
|
| 42 |
+
return "", 'Queueing'
|
| 43 |
+
elif status == 'Processing':
|
| 44 |
+
print("...Generating...")
|
| 45 |
+
return "", 'Processing'
|
| 46 |
+
elif status == 'Success':
|
| 47 |
+
return response.json()['file_id'], "Finished"
|
| 48 |
+
elif status == 'Fail':
|
| 49 |
+
return "", "Fail"
|
| 50 |
+
else:
|
| 51 |
+
return "", "Unknown"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def fetch_video_result(file_id: str):
|
| 55 |
+
print("---------------Video generated successfully, downloading now---------------")
|
| 56 |
+
url = "https://api.minimaxi.chat/v1/files/retrieve?file_id="+file_id
|
| 57 |
+
headers = {
|
| 58 |
+
'authorization': 'Bearer '+api_key,
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
response = requests.request("GET", url, headers=headers)
|
| 62 |
+
print(response.text)
|
| 63 |
+
|
| 64 |
+
download_url = response.json()['file']['download_url']
|
| 65 |
+
print("Video download link:" + download_url)
|
| 66 |
+
with open(output_file_name, 'wb') as f:
|
| 67 |
+
f.write(requests.get(download_url).content)
|
| 68 |
+
print("THe video has been downloaded in:"+os.getcwd()+'/'+output_file_name)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
task_id = invoke_video_generation()
|
| 73 |
+
print("-----------------Video generation task submitted -----------------")
|
| 74 |
+
while True:
|
| 75 |
+
time.sleep(10)
|
| 76 |
+
|
| 77 |
+
file_id, status = query_video_generation(task_id)
|
| 78 |
+
if file_id != "":
|
| 79 |
+
fetch_video_result(file_id)
|
| 80 |
+
print("---------------Successful---------------")
|
| 81 |
+
break
|
| 82 |
+
elif status == "Fail" or status == "Unknown":
|
| 83 |
+
print("---------------Failed---------------")
|
| 84 |
+
break
|