Upload script.py
Browse files- Was_node_suite/script.py +123 -0
Was_node_suite/script.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import time
|
4 |
+
import runpod
|
5 |
+
|
6 |
+
def push_error_log(history_json):
|
7 |
+
api_url_error = 'http://13.213.209.35:3100/loki/api/v1/push'
|
8 |
+
timestamp_ns = str(int(time.time() * 1e9))
|
9 |
+
data_payload = {
|
10 |
+
"streams": [
|
11 |
+
{
|
12 |
+
"stream": {
|
13 |
+
"app": "ixink-comfyui-logs"
|
14 |
+
},
|
15 |
+
"values": [
|
16 |
+
[
|
17 |
+
timestamp_ns,
|
18 |
+
json.dumps(history_json)
|
19 |
+
]
|
20 |
+
]
|
21 |
+
}
|
22 |
+
]
|
23 |
+
}
|
24 |
+
|
25 |
+
response_error = requests.post(api_url_error, json=data_payload)
|
26 |
+
print(f"Push status: {response_error.status_code}")
|
27 |
+
|
28 |
+
def handler(job):
|
29 |
+
""" Handler function that will be used to process jobs. """
|
30 |
+
job_input = job['input']
|
31 |
+
print(f"------CHECK IF------")
|
32 |
+
if 'seed5' in job_input:
|
33 |
+
with open('/workplace/ComfyUI/json/jsonformatter_gen5.txt', 'r') as file:
|
34 |
+
print(f"------USE GEN 5------")
|
35 |
+
data = json.load(file)
|
36 |
+
data['prompt']['394']['inputs']['seed'] = job_input['seed5']
|
37 |
+
elif all(key in job_input for key in ['seed6', 'seed7', 'seed8', 'seed9', 'seed10']):
|
38 |
+
with open('/workplace/ComfyUI/json/jsonformatter_gen10.txt', 'r') as file:
|
39 |
+
print(f"------USE GEN 10------")
|
40 |
+
data = json.load(file)
|
41 |
+
data['prompt']['428']['inputs']['seed'] = job_input['seed10']
|
42 |
+
data['prompt']['418']['inputs']['seed'] = job_input['seed9']
|
43 |
+
data['prompt']['414']['inputs']['seed'] = job_input['seed8']
|
44 |
+
data['prompt']['410']['inputs']['seed'] = job_input['seed7']
|
45 |
+
data['prompt']['406']['inputs']['seed'] = job_input['seed6']
|
46 |
+
data['prompt']['394']['inputs']['seed'] = job_input['seed5']
|
47 |
+
else:
|
48 |
+
with open('/workplace/ComfyUI/json/jsonformatter.txt', 'r') as file:
|
49 |
+
print(f"------USE GEN 4------")
|
50 |
+
data = json.load(file)
|
51 |
+
|
52 |
+
data['prompt']['366']['inputs']['image_url'] = job_input['textture']
|
53 |
+
data['prompt']['367']['inputs']['image_url'] = job_input['input_image']
|
54 |
+
|
55 |
+
data['prompt']['361']['inputs']['seed'] = job_input['seed1']
|
56 |
+
data['prompt']['356']['inputs']['seed'] = job_input['seed2']
|
57 |
+
data['prompt']['351']['inputs']['seed'] = job_input['seed3']
|
58 |
+
data['prompt']['345']['inputs']['seed'] = job_input['seed4']
|
59 |
+
|
60 |
+
data['prompt']['75']['inputs']['inStr'] = job_input['prompt']
|
61 |
+
|
62 |
+
api_url_prompt = 'http://127.0.0.1:3000/prompt'
|
63 |
+
|
64 |
+
response_prompt = requests.post(api_url_prompt, json=data)
|
65 |
+
|
66 |
+
print(f"Status Code: {response_prompt.status_code}")
|
67 |
+
try:
|
68 |
+
|
69 |
+
response_json = response_prompt.json()
|
70 |
+
print(f"JSON Response: {json.dumps(response_json, indent=4)}")
|
71 |
+
except requests.exceptions.JSONDecodeError as e:
|
72 |
+
print(f"Failed to decode JSON: {e}")
|
73 |
+
print(f"Response Content: {response_prompt.text}")
|
74 |
+
|
75 |
+
if response_prompt.status_code == 200:
|
76 |
+
print(f"Call Prompt API success")
|
77 |
+
prompt_id = response_json.get('prompt_id')
|
78 |
+
|
79 |
+
api_url_history = f'http://127.0.0.1:3000/history/{prompt_id}'
|
80 |
+
|
81 |
+
while True:
|
82 |
+
response_history = requests.get(api_url_history)
|
83 |
+
print(f"Status Code (History): {response_history.status_code}")
|
84 |
+
|
85 |
+
if response_history.status_code == 200:
|
86 |
+
try:
|
87 |
+
history_json = response_history.json()
|
88 |
+
print("---------------------")
|
89 |
+
print(history_json)
|
90 |
+
print("---------------------")
|
91 |
+
status_str = history_json.get(prompt_id, {}).get('status', {}).get('status_str')
|
92 |
+
if status_str == 'error':
|
93 |
+
print(f"Call History API error")
|
94 |
+
push_error_log(history_json)
|
95 |
+
break
|
96 |
+
else:
|
97 |
+
print(f"Call history API success")
|
98 |
+
value = history_json.get(prompt_id, {}).get('outputs', {}).get('999', {}).get('text')
|
99 |
+
if value:
|
100 |
+
print(f"Url: {value}")
|
101 |
+
break
|
102 |
+
else:
|
103 |
+
print("Waiting...")
|
104 |
+
except json.JSONDecodeError as e:
|
105 |
+
print(f"Failed to decode JSON: {e}")
|
106 |
+
print(f"Response Content: {response_history.text}")
|
107 |
+
except KeyError as e:
|
108 |
+
print(f"Key Error: {e}")
|
109 |
+
else:
|
110 |
+
print("Failed to retrieve history. Retrying...")
|
111 |
+
|
112 |
+
time.sleep(1)
|
113 |
+
|
114 |
+
customized_url_string = value[0].replace("['", "").replace("']", "").replace("'], ['", ", ")
|
115 |
+
print(customized_url_string)
|
116 |
+
name = job_input.get('name', 'World')
|
117 |
+
|
118 |
+
return customized_url_string
|
119 |
+
else:
|
120 |
+
print(f"Call Prompt API error")
|
121 |
+
push_error_log(response_json)
|
122 |
+
|
123 |
+
runpod.serverless.start({"handler": handler})
|