fantaxy commited on
Commit
7a327e7
โ€ข
1 Parent(s): 216c05e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -67
app.py CHANGED
@@ -2,31 +2,38 @@ import gradio as gr
2
  from gradio_client import Client
3
  import os
4
  import logging
5
- from PIL import Image
6
- import io
7
- import requests # ์ด ์ค„์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
8
 
9
  # ๋กœ๊น… ์„ค์ •
10
- logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
11
- logger = logging.getLogger(__name__)
12
 
13
  # API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
14
  api_client = Client("http://211.233.58.202:7960/")
15
 
16
- # Webhook URL
17
- WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY0MDYzMTA0MzE1MjZlNTUzYzUxM2Ei_pc"
18
-
19
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- def generate_image(prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
22
- logger.info(f"Received message: {prompt}, seed: {seed}, randomize_seed: {randomize_seed}, "
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=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
- logger.info("API response received: %s", result)
39
 
40
  # ๊ฒฐ๊ณผ ํ™•์ธ ๋ฐ ์ฒ˜๋ฆฌ
41
- if isinstance(result, tuple) and len(result) == 3:
42
- image_path, used_seed, translated_prompt = result
43
- logger.info(f"Image generated at: {image_path}")
44
- logger.info(f"Used seed: {used_seed}")
45
- logger.info(f"Translated prompt: {translated_prompt}")
46
 
47
- # ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์—ด์–ด์„œ PIL Image ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
48
- with Image.open(image_path) as img:
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 img_byte_arr, used_seed, translated_prompt
55
  else:
56
  raise ValueError("Unexpected API response format")
57
  except Exception as e:
58
- logger.error("Error during API request: %s", str(e), exc_info=True)
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
- gr.Markdown("# Image Generation")
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=[output_image, output_text]
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__":