TInkDD commited on
Commit
31d3041
·
verified ·
1 Parent(s): 439132c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import gradio as gr
4
+ import requests
5
+ import time
6
+ import numpy as np
7
+ import random
8
+
9
+ from PIL import Image
10
+
11
+ URL = "http://127.0.0.1:8188/prompt"
12
+ OUTPUT_DIR = "E:\\SD\\ComfyUI-aki-v1.3\\ComfyUI-aki-v1.3\\output"
13
+
14
+ def get_latest_image(folder):
15
+ files = os.listdir(folder)
16
+ image_files = [f for f in files if f.lower().endswith(('.png','.jpg','.jpeg'))]
17
+ image_files.sort(key=lambda x: os.path.getmtime(os.path.join(folder,x)))
18
+ latest_image = os.path.join(folder,image_files[-1]) if image_files else None
19
+ return latest_image
20
+
21
+ def update_seed():
22
+ seed_value = random.randint(1, 1000000)
23
+ json_file_path = "workflow_api.json"
24
+ with open(json_file_path, 'r', encoding='utf-8') as file:
25
+ data = json.load(file)
26
+
27
+ data['10']['inputs']['noise_seed'] = seed_value
28
+
29
+ with open(json_file_path, 'w', encoding='utf-8') as file:
30
+ json.dump(data, file, indent=4)
31
+
32
+ return seed_value
33
+
34
+ def start_queue(prompt_workflow):
35
+ p = {"prompt":prompt_workflow}
36
+ data = json.dumps(p).encode('utf-8')
37
+ requests.post(URL,data=data)
38
+
39
+ def generate_image(prompt_text,prompt_text2,cfg_count):
40
+ with open("workflow_api.json","r",encoding="utf-8") as file_json:
41
+ prompt = json.load(file_json)
42
+ prompt["6"]["inputs"]["text"] = f"digital artwork of a {prompt_text}"
43
+ prompt["7"]["inputs"]["text"] = f"digital artwork of a {prompt_text2}"
44
+ prompt["10"]["inputs"]["cfg"] = cfg_count
45
+
46
+ previous_image = get_latest_image(OUTPUT_DIR)
47
+ random_seed = update_seed()
48
+ print(f"New seed value generated:{random_seed}")
49
+ start_queue(prompt)
50
+
51
+ while True:
52
+ latest_image = get_latest_image(OUTPUT_DIR)
53
+ if latest_image != previous_image:
54
+ return latest_image
55
+
56
+ time.sleep(1)
57
+ demo = gr.Interface(fn=generate_image,inputs=["text","text","text"],outputs=["image"])
58
+
59
+ demo.launch(share=True)