Sqxww commited on
Commit
4d5a47b
·
1 Parent(s): 4c2c03f

add custom params

Browse files
Files changed (1) hide show
  1. app.py +48 -11
app.py CHANGED
@@ -3,11 +3,30 @@ import requests
3
 
4
  prefix = "https://smartfeed-custom-tools.hf.space/gradio_api/file="
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def change_image_style(image_url, style_image_url):
8
  data = {
9
  "image_url": image_url,
10
- "style_image_url": style_image_url
 
 
 
11
  }
12
 
13
  response = requests.post(
@@ -17,12 +36,22 @@ def change_image_style(image_url, style_image_url):
17
  )
18
 
19
  if response.status_code == 200:
20
- resultImageUrl = response.json().get("data").get("image_url")
21
- return resultImageUrl
 
 
 
 
22
  else:
23
  raise Exception(f"Error: {response.status_code} - {response.text}")
24
 
25
- def generate_image(input_image, style_images):
 
 
 
 
 
 
26
 
27
  if not input_image:
28
  raise gr.Error(f"Please upload an input image! Refer to step 1️⃣")
@@ -32,15 +61,18 @@ def generate_image(input_image, style_images):
32
 
33
  inputImageUrl = prefix + input_image
34
 
35
- result_images = list[str]()
 
36
  for style_image in style_images:
37
  if not style_image:
38
  raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
39
  styleImageUrl = prefix + style_image[0]
40
- result_images.append(change_image_style(inputImageUrl, styleImageUrl))
 
 
 
 
41
 
42
- return result_images
43
-
44
 
45
  def swap_to_gallery(images):
46
  return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
@@ -61,18 +93,23 @@ with gr.Blocks() as demo:
61
  with gr.Column(visible=False) as clear_button:
62
  remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
63
  input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
 
 
 
 
64
  submit = gr.Button("Submit")
65
 
66
  with gr.Column():
67
  gallery = gr.Gallery(label="Generated Images")
 
68
 
69
  files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
70
  remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
71
 
72
  submit.click(
73
  fn=generate_image,
74
- inputs=[input_image, uploaded_files],
75
- outputs=[gallery]
76
  )
77
 
78
 
 
3
 
4
  prefix = "https://smartfeed-custom-tools.hf.space/gradio_api/file="
5
 
6
+ extract_models = [
7
+ "o3",
8
+ "gpt-5",
9
+ "o4-mini",
10
+ "o4-mini-high",
11
+ ]
12
+
13
+
14
+ def change_image_style(
15
+ image_url,
16
+ style_image_url,
17
+ extract_model,
18
+ extract_prompt,
19
+ prompt_prefix
20
+ ):
21
+ image_url = "https://replicate.delivery/pbxt/Mjo5VWBA0fm8oq3BefpbdtXeJitBJc6AobPnnMdFxxCKzT3P/linkedin.jpeg"
22
+ style_image_url = "https://replicate.delivery/pbxt/N55l5TWGh8mSlNzW8usReoaNhGbFwvLeZR3TX1NL4pd2Wtfv/replicate-prediction-f2d25rg6gnrma0cq257vdw2n4c.png"
23
 
 
24
  data = {
25
  "image_url": image_url,
26
+ "style_image_url": style_image_url,
27
+ "extract_model": extract_model,
28
+ "extract_prompt": extract_prompt,
29
+ "prompt_prefix": prompt_prefix
30
  }
31
 
32
  response = requests.post(
 
36
  )
37
 
38
  if response.status_code == 200:
39
+ data = response.json().get("data")
40
+ resultImageUrl = data.get("image_url")
41
+ edit_prompt = data.get("edit_prompt")
42
+ # replace \n to <br>
43
+ edit_prompt = edit_prompt.replace("\n", "<br>")
44
+ return resultImageUrl, edit_prompt
45
  else:
46
  raise Exception(f"Error: {response.status_code} - {response.text}")
47
 
48
+ def generate_image(
49
+ input_image,
50
+ style_images,
51
+ extract_model,
52
+ extract_prompt,
53
+ prompt_prefix
54
+ ):
55
 
56
  if not input_image:
57
  raise gr.Error(f"Please upload an input image! Refer to step 1️⃣")
 
61
 
62
  inputImageUrl = prefix + input_image
63
 
64
+ result_images = []
65
+ markdownStr = ""
66
  for style_image in style_images:
67
  if not style_image:
68
  raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
69
  styleImageUrl = prefix + style_image[0]
70
+ result_image, edit_prompt = change_image_style(inputImageUrl, styleImageUrl, extract_model, extract_prompt, prompt_prefix)
71
+ result_images.append(result_image)
72
+ markdownStr += f"{edit_prompt}: <img src='{result_image}' style='zoom: 33%;' />\n"
73
+
74
+ return result_images, markdownStr
75
 
 
 
76
 
77
  def swap_to_gallery(images):
78
  return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
 
93
  with gr.Column(visible=False) as clear_button:
94
  remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
95
  input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
96
+ extract_model = gr.Dropdown(choices=extract_models, label="Extract Model", value="o3")
97
+ 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")
98
+ prompt_prefix = gr.Textbox(lines=1, label="Prompt Prefix", value="change the backgrounds to match this:")
99
+
100
  submit = gr.Button("Submit")
101
 
102
  with gr.Column():
103
  gallery = gr.Gallery(label="Generated Images")
104
+ markdown = gr.Markdown(label="Generated Images")
105
 
106
  files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
107
  remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
108
 
109
  submit.click(
110
  fn=generate_image,
111
+ inputs=[input_image, uploaded_files, extract_model, extract_prompt, prompt_prefix],
112
+ outputs=[gallery, markdown]
113
  )
114
 
115