Sqxww commited on
Commit
696e002
·
1 Parent(s): 4466d3a

add prompt count

Browse files
Files changed (1) hide show
  1. app.py +50 -13
app.py CHANGED
@@ -49,13 +49,16 @@ def change_image_style(
49
  edit_prompt = data.get("edit_prompt")
50
  # replace \n to <br>
51
  edit_prompt = edit_prompt.replace("\n", "<br>")
52
- return resultImageUrl, edit_prompt
53
  else:
54
- raise Exception(f"Error: {response.status_code} - {response.text}")
 
55
 
56
  def generate_image(
57
  input_image,
58
  style_images,
 
 
59
  extract_model,
60
  extract_prompt,
61
  prompt_prefix
@@ -66,20 +69,47 @@ def generate_image(
66
 
67
  if style_images is None:
68
  raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
 
 
 
 
 
 
 
 
 
69
 
70
  inputImageUrl = prefix + input_image
71
 
72
- result_images = []
 
73
  markdownStr = ""
74
- for style_image in style_images:
75
- if not style_image:
76
- raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
77
- styleImageUrl = prefix + style_image[0]
78
- result_image, edit_prompt = change_image_style(inputImageUrl, styleImageUrl, extract_model, extract_prompt, prompt_prefix)
79
- result_images.append(result_image)
80
- markdownStr += f"{edit_prompt} <img src='{result_image}' style='zoom: 33%;' />\n"
81
 
82
- return result_images, markdownStr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
 
85
  def swap_to_gallery(images):
@@ -101,6 +131,11 @@ with gr.Blocks() as demo:
101
  with gr.Column(visible=False) as clear_button:
102
  remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
103
  input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
 
 
 
 
 
104
  extract_model = gr.Dropdown(choices=extract_model_tag_list, label="Extract Model", value="o3")
105
  extract_prompt = gr.Textbox(lines=2, label="Extracted Prompt", value="Accurately extract everything from this photo into a prompt especially the hairstyle excluding the backgrounds. I want to replicate it perfectly. need to be a complete paragraph")
106
  prompt_prefix = gr.Textbox(lines=1, label="Prompt Prefix", value="change the backgrounds to match this:")
@@ -108,6 +143,8 @@ with gr.Blocks() as demo:
108
  submit = gr.Button("Submit")
109
 
110
  with gr.Column():
 
 
111
  gallery = gr.Gallery(label="Generated Images")
112
  markdown = gr.Markdown(label="Generated Images")
113
 
@@ -116,8 +153,8 @@ with gr.Blocks() as demo:
116
 
117
  submit.click(
118
  fn=generate_image,
119
- inputs=[input_image, uploaded_files, extract_model, extract_prompt, prompt_prefix],
120
- outputs=[gallery, markdown]
121
  )
122
 
123
 
 
49
  edit_prompt = data.get("edit_prompt")
50
  # replace \n to <br>
51
  edit_prompt = edit_prompt.replace("\n", "<br>")
52
+ return resultImageUrl, edit_prompt, ""
53
  else:
54
+ error_msg = f"Error: {response.status_code} - {response.text}"
55
+ return "", "", error_msg
56
 
57
  def generate_image(
58
  input_image,
59
  style_images,
60
+ prompt_count: int,
61
+ image_count: int,
62
  extract_model,
63
  extract_prompt,
64
  prompt_prefix
 
69
 
70
  if style_images is None:
71
  raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
72
+
73
+ style_size = len(style_images)
74
+ if style_size < 1:
75
+ raise gr.Error(f"Please upload at least one style image! Refer to step 1️⃣")
76
+
77
+ total_count = style_size * prompt_count * image_count
78
+ completed_count = 0
79
+ success_count = 0
80
+ error_count = 0
81
 
82
  inputImageUrl = prefix + input_image
83
 
84
+ result_image_list = []
85
+ error_list = []
86
  markdownStr = ""
 
 
 
 
 
 
 
87
 
88
+ for style_index in range(style_size):
89
+ style_image = style_images[style_index]
90
+ if not style_image:
91
+ error_list.append(f"Style image {style_index + 1} is empty.")
92
+ continue
93
+
94
+ for prompt_index in range(prompt_count):
95
+ for image_index in range(image_count):
96
+ completed_count += 1
97
+ styleImageUrl = prefix + style_image[0]
98
+ result_image, edit_prompt, error_msg = change_image_style(
99
+ inputImageUrl, styleImageUrl, extract_model, extract_prompt, prompt_prefix
100
+ )
101
+
102
+ if result_image:
103
+ success_count += 1
104
+ result_image_list.append(result_image)
105
+ markdownStr += f"{edit_prompt} <img src='{result_image}' style='zoom: 33%;' />\n"
106
+ else:
107
+ error_count += 1
108
+ error_list.append(f"Error processing style image {style_index + 1}, prompt {prompt_index + 1}, image {image_index + 1}: {error_msg}")
109
+
110
+ status = "processing" if completed_count < total_count else "completed"
111
+ status_message = f"Status: {status} ({completed_count}/{total_count}) success: {success_count}, error: {error_count}"
112
+ yield status_message, error_list, result_image_list, markdownStr
113
 
114
 
115
  def swap_to_gallery(images):
 
131
  with gr.Column(visible=False) as clear_button:
132
  remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
133
  input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
134
+ with gr.Row():
135
+ with gr.Column():
136
+ prompt_count = gr.Number(label="Prompt Count", value=4, precision=0)
137
+ with gr.Column():
138
+ image_count = gr.Number(label="Image Count", value=3, precision=0)
139
  extract_model = gr.Dropdown(choices=extract_model_tag_list, label="Extract Model", value="o3")
140
  extract_prompt = gr.Textbox(lines=2, label="Extracted Prompt", value="Accurately extract everything from this photo into a prompt especially the hairstyle excluding the backgrounds. I want to replicate it perfectly. need to be a complete paragraph")
141
  prompt_prefix = gr.Textbox(lines=1, label="Prompt Prefix", value="change the backgrounds to match this:")
 
143
  submit = gr.Button("Submit")
144
 
145
  with gr.Column():
146
+ status_text = gr.Textbox(label="Status", show_label=False, interactive=False)
147
+ error_text = gr.Textbox(label="Error List", show_label=False, interactive=False)
148
  gallery = gr.Gallery(label="Generated Images")
149
  markdown = gr.Markdown(label="Generated Images")
150
 
 
153
 
154
  submit.click(
155
  fn=generate_image,
156
+ inputs=[input_image, uploaded_files, prompt_count, image_count, extract_model, extract_prompt, prompt_prefix],
157
+ outputs=[status_text, error_text, gallery, markdown]
158
  )
159
 
160