import requests import json import time import runpod def push_error_log(history_json): api_url_error = 'http://13.213.209.35:3100/loki/api/v1/push' timestamp_ns = str(int(time.time() * 1e9)) data_payload = { "streams": [ { "stream": { "app": "ixink-comfyui-logs" }, "values": [ [ timestamp_ns, json.dumps(history_json) ] ] } ] } response_error = requests.post(api_url_error, json=data_payload) print(f"Push status: {response_error.status_code}") def handler(job): """ Handler function that will be used to process jobs. """ job_input = job['input'] print(f"------CHECK IF------") if 'seed5' in job_input: with open('/workplace/ComfyUI/json/jsonformatter_gen5.txt', 'r') as file: print(f"------USE GEN 5------") data = json.load(file) data['prompt']['394']['inputs']['seed'] = job_input['seed5'] elif all(key in job_input for key in ['seed6', 'seed7', 'seed8', 'seed9', 'seed10']): with open('/workplace/ComfyUI/json/jsonformatter_gen10.txt', 'r') as file: print(f"------USE GEN 10------") data = json.load(file) data['prompt']['428']['inputs']['seed'] = job_input['seed10'] data['prompt']['418']['inputs']['seed'] = job_input['seed9'] data['prompt']['414']['inputs']['seed'] = job_input['seed8'] data['prompt']['410']['inputs']['seed'] = job_input['seed7'] data['prompt']['406']['inputs']['seed'] = job_input['seed6'] data['prompt']['394']['inputs']['seed'] = job_input['seed5'] else: with open('/workplace/ComfyUI/json/jsonformatter.txt', 'r') as file: print(f"------USE GEN 4------") data = json.load(file) data['prompt']['366']['inputs']['image_url'] = job_input['textture'] data['prompt']['367']['inputs']['image_url'] = job_input['input_image'] data['prompt']['361']['inputs']['seed'] = job_input['seed1'] data['prompt']['356']['inputs']['seed'] = job_input['seed2'] data['prompt']['351']['inputs']['seed'] = job_input['seed3'] data['prompt']['345']['inputs']['seed'] = job_input['seed4'] data['prompt']['75']['inputs']['inStr'] = job_input['prompt'] api_url_prompt = 'http://127.0.0.1:3000/prompt' response_prompt = requests.post(api_url_prompt, json=data) print(f"Status Code: {response_prompt.status_code}") try: response_json = response_prompt.json() print(f"JSON Response: {json.dumps(response_json, indent=4)}") except requests.exceptions.JSONDecodeError as e: print(f"Failed to decode JSON: {e}") print(f"Response Content: {response_prompt.text}") if response_prompt.status_code == 200: print(f"Call Prompt API success") prompt_id = response_json.get('prompt_id') api_url_history = f'http://127.0.0.1:3000/history/{prompt_id}' while True: response_history = requests.get(api_url_history) print(f"Status Code (History): {response_history.status_code}") if response_history.status_code == 200: try: history_json = response_history.json() print("---------------------") print(history_json) print("---------------------") status_str = history_json.get(prompt_id, {}).get('status', {}).get('status_str') if status_str == 'error': print(f"Call History API error") push_error_log(history_json) break else: print(f"Call history API success") value = history_json.get(prompt_id, {}).get('outputs', {}).get('999', {}).get('text') if value: print(f"Url: {value}") break else: print("Waiting...") except json.JSONDecodeError as e: print(f"Failed to decode JSON: {e}") print(f"Response Content: {response_history.text}") except KeyError as e: print(f"Key Error: {e}") else: print("Failed to retrieve history. Retrying...") time.sleep(1) customized_url_string = value[0].replace("['", "").replace("']", "").replace("'], ['", ", ") print(customized_url_string) name = job_input.get('name', 'World') return customized_url_string else: print(f"Call Prompt API error") push_error_log(response_json) runpod.serverless.start({"handler": handler})