-
Notifications
You must be signed in to change notification settings - Fork 26
/
util.py
112 lines (87 loc) · 3.3 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import requests
# proxies = {
# "http": "http://127.0.0.1:7890",
# "https": "http://127.0.0.1:7890"
# }
proxies = {}
def dreamMachineMake(prompt, access_token, img_file=None):
url = "https://internal-api.virginia.labs.lumalabs.ai/api/photon/v1/generations/"
if img_file:
print("uploading image")
img_url = upload_file(access_token, img_file)
# print(img_url)
payload = {
"aspect_ratio": "16:9",
"expand_prompt": True,
"image_url": img_url,
"user_prompt": prompt
}
else:
payload = {
"user_prompt": prompt,
"aspect_ratio": "16:9",
"expand_prompt": True
}
headers = {
"Cookie": "access_token=" + access_token,
"Origin": "https://lumalabs.ai",
"Referer": "https://lumalabs.ai",
"content-type": "application/json"
}
response = requests.post(url, json=payload, headers=headers, proxies=proxies)
response_json = json.loads(response.text)
return response_json
def refreshDreamMachine(access_token):
url = "https://internal-api.virginia.labs.lumalabs.ai/api/photon/v1/user/generations/"
querystring = {"offset": "0", "limit": "10"}
headers = {
"Cookie": "access_token=" + access_token,
}
response = requests.get(url, headers=headers, params=querystring, proxies=proxies)
response_text = response.text
response_json = json.loads(response_text)
return response_json
def get_signed_upload(access_token):
url = "https://internal-api.virginia.labs.lumalabs.ai/api/photon/v1/generations/file_upload"
params = {
'file_type': 'image',
'filename': 'file.jpg'
}
headers = {
"Cookie": "access_token=" + access_token,
}
response = requests.post(url, params=params, headers=headers, proxies=proxies)
response.raise_for_status()
return response.json()
def upload_file(access_token, file_path):
try:
signed_upload = get_signed_upload(access_token)
presigned_url = signed_upload['presigned_url']
public_url = signed_upload['public_url']
with open(file_path, 'rb') as file:
response = requests.put(presigned_url, data=file,
headers={'Content-Type': "image/*", "Referer": "https://lumalabs.ai/",
"origin": "https://lumalabs.ai"}, proxies=proxies)
if response.status_code == 200:
print("Upload successful:", public_url)
return public_url
else:
print("Upload failed.")
except Exception as e:
print("Upload failed.")
print("Error uploading image:", e)
def uploadImage(access_token, file_path):
url = "https://internal-api.virginia.labs.lumalabs.ai/api/photon/v1/generations/file_upload?file_type=image" \
"&filename=file.jpg"
with open(file_path, 'rb') as file:
files = {"file": file}
headers = {
"Cookie": "access_token=" + access_token,
"User-Agent": "Apipost/8 (https://www.apipost.cn)"
}
response = requests.post(url, headers=headers, files=files, proxies=proxies)
print(response.text)
img_url = json.loads(response.text)["public_url"]
print(img_url)
return img_url