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()
|