Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,8 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import os
|
| 4 |
import logging
|
| 5 |
-
import requests
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
-
import base64
|
| 9 |
-
import traceback
|
| 10 |
|
| 11 |
# ๋ก๊น
์ค์
|
| 12 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
@@ -18,44 +15,57 @@ api_client = Client("http://211.233.58.202:7960/")
|
|
| 18 |
# Webhook URL
|
| 19 |
WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY0MDYzMTA0MzE1MjZlNTUzYzUxM2Ei_pc"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 30 |
image = Image.open(io.BytesIO(image_data))
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Base64๋ก ์ธ์ฝ๋ฉ
|
| 37 |
-
im_b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 38 |
-
|
| 39 |
-
url = "https://api.imgur.com/3/image"
|
| 40 |
-
payload = {'image': im_b64, 'type': 'base64'}
|
| 41 |
-
headers = {'Authorization': f'Client-ID {IMGUR_CLIENT_ID}'}
|
| 42 |
-
|
| 43 |
-
logger.debug("Sending request to Imgur API")
|
| 44 |
-
response = requests.post(url, headers=headers, data=payload)
|
| 45 |
-
logger.debug(f"Imgur API response status: {response.status_code}")
|
| 46 |
-
logger.debug(f"Imgur API response content: {response.text}")
|
| 47 |
-
|
| 48 |
-
response.raise_for_status()
|
| 49 |
-
|
| 50 |
-
return response.json()['data']['link']
|
| 51 |
-
except Exception as e:
|
| 52 |
-
logger.error(f"Failed to upload image to Imgur: {str(e)}")
|
| 53 |
-
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 54 |
-
if hasattr(e, 'response'):
|
| 55 |
-
logger.error(f"Response content: {e.response.text}")
|
| 56 |
-
return None
|
| 57 |
|
| 58 |
-
# ... (๋๋จธ์ง ์ฝ๋๋ ๊ทธ๋๋ก ์ ์ง)
|
| 59 |
|
| 60 |
|
| 61 |
css = """
|
|
@@ -92,13 +102,13 @@ examples = [
|
|
| 92 |
|
| 93 |
def use_prompt(prompt):
|
| 94 |
return prompt
|
| 95 |
-
|
| 96 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 97 |
-
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
input_text = gr.Textbox(label="Enter your prompt for image generation")
|
| 101 |
output_image = gr.Image(label="Generated Image")
|
|
|
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
|
|
@@ -112,28 +122,20 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
| 112 |
guidance_scale = gr.Slider(minimum=1, maximum=20, step=0.1, label="Guidance Scale", value=5)
|
| 113 |
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
|
| 114 |
|
| 115 |
-
with gr.Row():
|
| 116 |
-
for prompt, image_file in examples:
|
| 117 |
-
with gr.Column():
|
| 118 |
-
gr.Image(image_file, label=prompt[:50] + "...") # ํ๋กฌํํธ์ ์ฒ์ 50์๋ง ํ์
|
| 119 |
-
gr.Button("Use this prompt").click(
|
| 120 |
-
fn=use_prompt,
|
| 121 |
-
inputs=[],
|
| 122 |
-
outputs=input_text,
|
| 123 |
-
api_name=False
|
| 124 |
-
).then(
|
| 125 |
-
lambda x=prompt: x,
|
| 126 |
-
inputs=[],
|
| 127 |
-
outputs=input_text
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
input_text.submit(
|
| 131 |
fn=respond,
|
| 132 |
inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 133 |
-
outputs=output_image
|
| 134 |
)
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
logger.info(f"Starting application with Imgur Client ID: {IMGUR_CLIENT_ID[:5]}...")
|
| 139 |
demo.launch()
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import os
|
| 4 |
import logging
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# ๋ก๊น
์ค์
|
| 9 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
| 15 |
# Webhook URL
|
| 16 |
WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY0MDYzMTA0MzE1MjZlNTUzYzUxM2Ei_pc"
|
| 17 |
|
| 18 |
+
def generate_image(prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 19 |
+
logger.info(f"Received message: {prompt}, seed: {seed}, randomize_seed: {randomize_seed}, "
|
| 20 |
+
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
| 21 |
+
f"num_inference_steps: {num_inference_steps}")
|
| 22 |
+
|
| 23 |
try:
|
| 24 |
+
# ์ด๋ฏธ์ง ์์ฑ ์์ฒญ
|
| 25 |
+
result = api_client.predict(
|
| 26 |
+
prompt=prompt,
|
| 27 |
+
seed=seed,
|
| 28 |
+
randomize_seed=randomize_seed,
|
| 29 |
+
width=width,
|
| 30 |
+
height=height,
|
| 31 |
+
guidance_scale=guidance_scale,
|
| 32 |
+
num_inference_steps=num_inference_steps,
|
| 33 |
+
api_name="/infer_t2i"
|
| 34 |
+
)
|
| 35 |
+
logger.info("API response received: %s", result)
|
| 36 |
|
| 37 |
+
# ๊ฒฐ๊ณผ ํ์ธ ๋ฐ ์ฒ๋ฆฌ
|
| 38 |
+
if isinstance(result, tuple) and len(result) == 3:
|
| 39 |
+
image_path, used_seed, translated_prompt = result
|
| 40 |
+
logger.info(f"Image generated at: {image_path}")
|
| 41 |
+
logger.info(f"Used seed: {used_seed}")
|
| 42 |
+
logger.info(f"Translated prompt: {translated_prompt}")
|
| 43 |
+
|
| 44 |
+
# ์ด๋ฏธ์ง ํ์ผ์ ์ด์ด์ PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 45 |
+
with Image.open(image_path) as img:
|
| 46 |
+
# ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ
|
| 47 |
+
img_byte_arr = io.BytesIO()
|
| 48 |
+
img.save(img_byte_arr, format='PNG')
|
| 49 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 50 |
+
|
| 51 |
+
return img_byte_arr, used_seed, translated_prompt
|
| 52 |
+
else:
|
| 53 |
+
raise ValueError("Unexpected API response format")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logger.error("Error during API request: %s", str(e), exc_info=True)
|
| 56 |
+
return "Failed to generate image due to an error."
|
| 57 |
+
|
| 58 |
+
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 59 |
+
image_data, used_seed, translated_prompt = generate_image(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps)
|
| 60 |
+
|
| 61 |
+
if isinstance(image_data, bytes):
|
| 62 |
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 63 |
image = Image.open(io.BytesIO(image_data))
|
| 64 |
+
return image, f"Used seed: {used_seed}, Translated prompt: {translated_prompt}"
|
| 65 |
+
else:
|
| 66 |
+
return image_data, "Error occurred"
|
| 67 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
css = """
|
|
|
|
| 102 |
|
| 103 |
def use_prompt(prompt):
|
| 104 |
return prompt
|
|
|
|
| 105 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 106 |
+
gr.Markdown("# Image Generation")
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
input_text = gr.Textbox(label="Enter your prompt for image generation")
|
| 110 |
output_image = gr.Image(label="Generated Image")
|
| 111 |
+
output_text = gr.Textbox(label="Image Info")
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
|
|
|
|
| 122 |
guidance_scale = gr.Slider(minimum=1, maximum=20, step=0.1, label="Guidance Scale", value=5)
|
| 123 |
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
input_text.submit(
|
| 126 |
fn=respond,
|
| 127 |
inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 128 |
+
outputs=[output_image, output_text]
|
| 129 |
)
|
| 130 |
|
| 131 |
+
# Examples ์น์
์ถ๊ฐ
|
| 132 |
+
gr.Examples(
|
| 133 |
+
examples=[[ex[0]] for ex in examples], # ํ๋กฌํํธ๋ง ์ฌ์ฉ
|
| 134 |
+
inputs=input_text,
|
| 135 |
+
outputs=[output_image, output_text],
|
| 136 |
+
fn=respond,
|
| 137 |
+
cache_examples=False
|
| 138 |
+
)
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
|
|
|
| 141 |
demo.launch()
|