Update app.py
Browse files
app.py
CHANGED
|
@@ -2,31 +2,38 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import os
|
| 4 |
import logging
|
| 5 |
-
|
| 6 |
-
import io
|
| 7 |
-
import requests # ์ด ์ค์ ์ถ๊ฐํฉ๋๋ค
|
| 8 |
|
| 9 |
# ๋ก๊น
์ค์
|
| 10 |
-
logging.basicConfig(level=logging.
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
# API ํด๋ผ์ด์ธํธ ์ค์
|
| 14 |
api_client = Client("http://211.233.58.202:7960/")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
WEBHOOK_URL = "https://
|
| 18 |
-
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
| 24 |
f"num_inference_steps: {num_inference_steps}")
|
| 25 |
|
| 26 |
try:
|
| 27 |
# ์ด๋ฏธ์ง ์์ฑ ์์ฒญ
|
| 28 |
result = api_client.predict(
|
| 29 |
-
prompt=
|
| 30 |
seed=seed,
|
| 31 |
randomize_seed=randomize_seed,
|
| 32 |
width=width,
|
|
@@ -35,60 +42,25 @@ def generate_image(prompt, seed, randomize_seed, width, height, guidance_scale,
|
|
| 35 |
num_inference_steps=num_inference_steps,
|
| 36 |
api_name="/infer_t2i"
|
| 37 |
)
|
| 38 |
-
|
| 39 |
|
| 40 |
# ๊ฒฐ๊ณผ ํ์ธ ๋ฐ ์ฒ๋ฆฌ
|
| 41 |
-
if isinstance(result, tuple) and len(result)
|
| 42 |
-
image_path
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
logger.info(f"Translated prompt: {translated_prompt}")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
# ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ
|
| 50 |
-
img_byte_arr = io.BytesIO()
|
| 51 |
-
img.save(img_byte_arr, format='PNG')
|
| 52 |
-
img_byte_arr = img_byte_arr.getvalue()
|
| 53 |
|
| 54 |
-
return
|
| 55 |
else:
|
| 56 |
raise ValueError("Unexpected API response format")
|
| 57 |
except Exception as e:
|
| 58 |
-
|
| 59 |
return "Failed to generate image due to an error."
|
| 60 |
|
| 61 |
|
| 62 |
-
|
| 63 |
-
def send_to_webhook(prompt, image_url):
|
| 64 |
-
payload = {
|
| 65 |
-
"prompt": prompt,
|
| 66 |
-
"image": image_url
|
| 67 |
-
}
|
| 68 |
-
try:
|
| 69 |
-
response = requests.post(WEBHOOK_URL, json=payload)
|
| 70 |
-
response.raise_for_status()
|
| 71 |
-
logger.info(f"Successfully sent data to webhook. Status code: {response.status_code}")
|
| 72 |
-
except requests.exceptions.RequestException as e:
|
| 73 |
-
logger.error(f"Failed to send data to webhook: {e}")
|
| 74 |
-
|
| 75 |
-
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 76 |
-
image_data, used_seed, translated_prompt = generate_image(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps)
|
| 77 |
-
|
| 78 |
-
if isinstance(image_data, bytes):
|
| 79 |
-
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 80 |
-
image = Image.open(io.BytesIO(image_data))
|
| 81 |
-
|
| 82 |
-
# ์ด๋ฏธ์ง URL ์์ฑ (์ค์ ํ๊ฒฝ์ ๋ง๊ฒ ์์ ํ์)
|
| 83 |
-
image_url = f"http://your_server_url/images/{used_seed}.png"
|
| 84 |
-
|
| 85 |
-
# Webhook ํธ์ถ
|
| 86 |
-
send_to_webhook(message, image_url)
|
| 87 |
-
|
| 88 |
-
return image, f"Used seed: {used_seed}, Translated prompt: {translated_prompt}"
|
| 89 |
-
else:
|
| 90 |
-
return image_data, "Error occurred"
|
| 91 |
-
|
| 92 |
css = """
|
| 93 |
footer {
|
| 94 |
visibility: hidden;
|
|
@@ -123,13 +95,13 @@ examples = [
|
|
| 123 |
|
| 124 |
def use_prompt(prompt):
|
| 125 |
return prompt
|
|
|
|
| 126 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 127 |
-
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
input_text = gr.Textbox(label="Enter your prompt for image generation")
|
| 131 |
output_image = gr.Image(label="Generated Image")
|
| 132 |
-
output_text = gr.Textbox(label="Image Info")
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
|
|
@@ -143,19 +115,25 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
| 143 |
guidance_scale = gr.Slider(minimum=1, maximum=20, step=0.1, label="Guidance Scale", value=5)
|
| 144 |
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
input_text.submit(
|
| 147 |
fn=respond,
|
| 148 |
inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 149 |
-
outputs=
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
# Examples ์น์
์ถ๊ฐ
|
| 153 |
-
gr.Examples(
|
| 154 |
-
examples=[[ex[0]] for ex in examples], # ํ๋กฌํํธ๋ง ์ฌ์ฉ
|
| 155 |
-
inputs=input_text,
|
| 156 |
-
outputs=[output_image, output_text],
|
| 157 |
-
fn=respond,
|
| 158 |
-
cache_examples=False
|
| 159 |
)
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import os
|
| 4 |
import logging
|
| 5 |
+
import requests
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ๋ก๊น
์ค์
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 9 |
|
| 10 |
# API ํด๋ผ์ด์ธํธ ์ค์
|
| 11 |
api_client = Client("http://211.233.58.202:7960/")
|
| 12 |
|
| 13 |
+
# Zapier ์นํ
URL
|
| 14 |
+
WEBHOOK_URL = "https://hooks.zapier.com/hooks/catch/14523965/264pyhj/"
|
|
|
|
| 15 |
|
| 16 |
+
def send_to_webhook(prompt, image_url):
|
| 17 |
+
payload = {
|
| 18 |
+
"prompt": prompt,
|
| 19 |
+
"image_url": image_url
|
| 20 |
+
}
|
| 21 |
+
try:
|
| 22 |
+
response = requests.post(WEBHOOK_URL, json=payload)
|
| 23 |
+
response.raise_for_status()
|
| 24 |
+
logging.info(f"Successfully sent data to webhook. Status code: {response.status_code}")
|
| 25 |
+
except requests.exceptions.RequestException as e:
|
| 26 |
+
logging.error(f"Failed to send data to webhook: {e}")
|
| 27 |
|
| 28 |
+
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 29 |
+
logging.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
|
| 30 |
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
| 31 |
f"num_inference_steps: {num_inference_steps}")
|
| 32 |
|
| 33 |
try:
|
| 34 |
# ์ด๋ฏธ์ง ์์ฑ ์์ฒญ
|
| 35 |
result = api_client.predict(
|
| 36 |
+
prompt=message,
|
| 37 |
seed=seed,
|
| 38 |
randomize_seed=randomize_seed,
|
| 39 |
width=width,
|
|
|
|
| 42 |
num_inference_steps=num_inference_steps,
|
| 43 |
api_name="/infer_t2i"
|
| 44 |
)
|
| 45 |
+
logging.info("API response received: %s", result)
|
| 46 |
|
| 47 |
# ๊ฒฐ๊ณผ ํ์ธ ๋ฐ ์ฒ๋ฆฌ
|
| 48 |
+
if isinstance(result, tuple) and len(result) >= 1:
|
| 49 |
+
image_path = result[0]
|
| 50 |
+
# ์ด๋ฏธ์ง URL ์์ฑ (์ค์ ์๋ฒ URL๋ก ๋ณ๊ฒฝ ํ์)
|
| 51 |
+
image_url = f"http://211.233.58.202:7960/file={image_path}"
|
|
|
|
| 52 |
|
| 53 |
+
# ์นํ
์ผ๋ก ๋ฐ์ดํฐ ์ ์ก
|
| 54 |
+
send_to_webhook(message, image_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
return image_url
|
| 57 |
else:
|
| 58 |
raise ValueError("Unexpected API response format")
|
| 59 |
except Exception as e:
|
| 60 |
+
logging.error("Error during API request: %s", str(e))
|
| 61 |
return "Failed to generate image due to an error."
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
css = """
|
| 65 |
footer {
|
| 66 |
visibility: hidden;
|
|
|
|
| 95 |
|
| 96 |
def use_prompt(prompt):
|
| 97 |
return prompt
|
| 98 |
+
|
| 99 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 100 |
+
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
input_text = gr.Textbox(label="Enter your prompt for image generation")
|
| 104 |
output_image = gr.Image(label="Generated Image")
|
|
|
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
|
|
|
|
| 115 |
guidance_scale = gr.Slider(minimum=1, maximum=20, step=0.1, label="Guidance Scale", value=5)
|
| 116 |
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
|
| 117 |
|
| 118 |
+
with gr.Row():
|
| 119 |
+
for prompt, image_file in examples:
|
| 120 |
+
with gr.Column():
|
| 121 |
+
gr.Image(image_file, label=prompt[:50] + "...") # ํ๋กฌํํธ์ ์ฒ์ 50์๋ง ํ์
|
| 122 |
+
gr.Button("Use this prompt").click(
|
| 123 |
+
fn=use_prompt,
|
| 124 |
+
inputs=[],
|
| 125 |
+
outputs=input_text,
|
| 126 |
+
api_name=False
|
| 127 |
+
).then(
|
| 128 |
+
lambda x=prompt: x,
|
| 129 |
+
inputs=[],
|
| 130 |
+
outputs=input_text
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
input_text.submit(
|
| 134 |
fn=respond,
|
| 135 |
inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 136 |
+
outputs=output_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|