Spaces:
Runtime error
Runtime error
File size: 1,879 Bytes
f53e6d4 56a2aa6 f53e6d4 8f70b42 2cc55c1 f53e6d4 8f70b42 56a2aa6 f53e6d4 8f70b42 56a2aa6 f53e6d4 8f70b42 f53e6d4 8f70b42 f53e6d4 8f70b42 f53e6d4 8f70b42 f53e6d4 8f70b42 f53e6d4 8f70b42 f53e6d4 8f70b42 |
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 |
import json
import os
import time
import random
import gradio as gr
import numpy as np
import requests
from PIL import Image
print ("1")
URL = "http://0.0.0.0:8188/prompt"
INPUT_DIR = "input"
OUTPUT_DIR = "output"
cached_seed = 0
def get_latest_image(folder):
files = os.listdir(folder)
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
image_files.sort(key=lambda x: os.path.getmtime(os.path.join(folder, x)))
latest_image = os.path.join(folder, image_files[-1]) if image_files else None
return latest_image
print ("2")
def start_queue(prompt_workflow):
p = {"prompt": prompt_workflow}
data = json.dumps(p).encode('utf-8')
requests.post(URL, data=data)
print ("3")
def generate_image(input_image):
with open("workflow.json", "r") as file_json:
prompt = json.load(file_json)
print ("4")
prompt["3"]["inputs"]["seed"] = random.randint(1, 1500000)
global cached_seed
if cached_seed == prompt["3"]["inputs"]["seed"]:
return get_latest_image(OUTPUT_DIR)
cached_seed = prompt["3"]["inputs"]["seed"]
print ("5")
image = Image.fromarray(input_image)
min_side = min(image.size)
scale_factor = 512 / min_side
new_size = (round(image.size[0] * scale_factor), round(image.size[1] * scale_factor))
resized_image = image.resize(new_size)
print ("6")
resized_image.save(os.path.join(INPUT_DIR, "test_api.jpg"))
previous_image = get_latest_image(OUTPUT_DIR)
start_queue(prompt)
print ("7")
while True:
print ("8")
latest_image = get_latest_image(OUTPUT_DIR)
if latest_image != previous_image:
return latest_image
print ("9")
time.sleep(1)
demo = gr.Interface(fn=generate_image, inputs=["image"], outputs=["image"])
demo.launch(share=True)
print ("10")
|