Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,30 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
-
import random
|
| 4 |
-
import time
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
from PIL import Image
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
BASE_FOLDER = Path("")
|
| 14 |
-
OUTPUT_DIR = Path("")
|
| 15 |
-
URL = "https://c253-2405-201-300a-51fe-6553-e08f-1bce-578f.ngrok-free.app/prompt"
|
| 16 |
-
TXT2IMG = "workflow_api_text2img.json"
|
| 17 |
-
IMG2IMG = "workflow_api_img2img.json"
|
| 18 |
-
HIRES = "workflow_api_HiRes.json"
|
| 19 |
-
|
| 20 |
-
KSAMPLER_NAMES = [
|
| 21 |
-
"euler",
|
| 22 |
-
"euler_ancestral",
|
| 23 |
-
"dpmpp_sde",
|
| 24 |
-
"dpmpp_2m",
|
| 25 |
-
"dpmpp_2m_sde",
|
| 26 |
-
"dpmpp_3m_sde",
|
| 27 |
-
"ddpm",
|
| 28 |
-
"lcm",
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
-
SCHEDULER_NAMES = ["normal", "karras", "exponential", "sgm_uniform", "simple", "ddim_uniform"]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
css = """
|
| 35 |
-
.green {
|
| 36 |
-
border: 3px solid green !important;
|
| 37 |
-
}
|
| 38 |
-
.red {
|
| 39 |
-
border: 3px solid red !important;
|
| 40 |
-
}
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def checkpoint_list():
|
| 45 |
-
return [model.name for model in BASE_FOLDER.glob("*.ckpt")]
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def get_latest_image():
|
| 50 |
-
image_files = list(OUTPUT_DIR.glob("*.png"))
|
| 51 |
-
image_files.sort(key=lambda x: os.path.getmtime(x))
|
| 52 |
-
latest_image = image_files[-1] if image_files else None
|
| 53 |
-
return latest_image
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def start_queue(prompt_workflow):
|
| 57 |
-
p = {"prompt": prompt_workflow}
|
| 58 |
-
data = json.dumps(p).encode("utf-8")
|
| 59 |
-
requests.post(URL, data=data)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def txt2img_workflow(checkpoint_name, positive_prompt):
|
| 63 |
-
with open(TXT2IMG, "r") as file_json:
|
| 64 |
-
prompt = json.load(file_json)
|
| 65 |
-
|
| 66 |
-
prompt["1"]["inputs"]["ckpt_name"] = checkpoint_name
|
| 67 |
-
prompt["2"]["inputs"]["noise_seed"] = random.randint(1, 999999999999)
|
| 68 |
-
prompt["3"]["inputs"]["text"] = positive_prompt
|
| 69 |
-
|
| 70 |
-
print( prompt["2"]["inputs"]["noise_seed"])
|
| 71 |
-
previous_image = get_latest_image()
|
| 72 |
-
|
| 73 |
-
start_queue(prompt)
|
| 74 |
-
return True
|
| 75 |
-
"""while True:
|
| 76 |
-
latest_image = get_latest_image()
|
| 77 |
-
if latest_image != previous_image:
|
| 78 |
-
return latest_image
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
def main():
|
| 85 |
-
with gr.Blocks(
|
| 86 |
-
models = checkpoint_list()
|
| 87 |
-
|
| 88 |
with gr.Row():
|
| 89 |
-
with gr.Column():
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
with gr.Column(scale=1):
|
| 99 |
-
generate_btn_txt2img = gr.Button("Generate")
|
| 100 |
-
|
| 101 |
-
with gr.Row():
|
| 102 |
-
output_img = gr.Image(label="Output", interactive=False)
|
| 103 |
-
|
| 104 |
-
generate_btn_txt2img.click(fn=txt2img_workflow, inputs=[base_checkpoint, positive], outputs=output_img)
|
| 105 |
-
|
| 106 |
demo.launch()
|
| 107 |
|
| 108 |
-
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
main()
|
|
|
|
| 1 |
+
import requests
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Replace the empty string with your model id below
|
| 7 |
+
model_id = "7qrmoo93"
|
| 8 |
+
baseten_api_key = "gN3ksgBu.ayDr4GN8iTBEzoQu5pQjmNz6zuj0iL8t"
|
| 9 |
|
| 10 |
+
def res(data):
|
| 11 |
+
print(data)
|
| 12 |
+
resp = requests.post("https://model-7qrmoo93.api.baseten.co/production/predict",headers={"Authorization": "Api-Key V5r36lUR.s8F3b2Amna9PnQRyP3lFswULHYVF86ki"},json={'top_p': 0.75, 'prompt': data, 'num_beams': 4, 'temperature': 0.4},)
|
| 13 |
+
print(resp.json())
|
| 14 |
+
return resp.json()
|
| 15 |
|
| 16 |
def main():
|
| 17 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 18 |
with gr.Row():
|
| 19 |
+
with gr.Column(scale=3):
|
| 20 |
+
text_input = gr.Textbox()
|
| 21 |
+
with gr.Row():
|
| 22 |
+
with gr.Column(scale=1):
|
| 23 |
+
text_output = gr.Textbox()
|
| 24 |
+
with gr.Row():
|
| 25 |
+
text_button = gr.Button("Send")
|
| 26 |
+
text_button.click(res, text_input, text_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
demo.launch()
|
| 28 |
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
+
main()
|