KingNish commited on
Commit
dcaf916
·
1 Parent(s): a438309

Changes to be committed:

Browse files
Files changed (1) hide show
  1. app.py +66 -24
app.py CHANGED
@@ -1,10 +1,57 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
 
 
 
3
 
4
- def image_to_image(prompt, image, flux_client):
5
- prompt = "You are an AI image-to-image editor that transforms a user-provided input image based on their prompt while maintaining consistency and fidelity to the original image. Generate an output image that accurately reflects the user's requested modifications, preserving key elements like style, composition, and context from the input image unless explicitly instructed otherwise. " + prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  image = flux_client.predict(
7
- input_image=handle_file(image),
8
  prompt=prompt,
9
  seed=0,
10
  randomize_seed=True,
@@ -13,25 +60,30 @@ def image_to_image(prompt, image, flux_client):
13
  api_name="/infer"
14
  )
15
  print(image)
 
 
 
 
 
16
  return image[0]
17
 
18
  def image_to_video(prompt, image, ltx_client):
19
  result = ltx_client.predict(
20
- prompt=prompt,
21
- input_image_url=None,
22
- middle_image_url=handle_file(image),
23
- final_image_url=None,
24
- duration_ui=4,
25
- api_name="/generate_video"
26
  )
27
  print(result)
28
  return result
29
 
30
- def personalized_video(prompt, image, request: gr.Request):
31
  x_ip_token = request.headers['x-ip-token']
32
  flux_client = Client("black-forest-labs/FLUX.1-Kontext-Dev", headers={"x-ip-token": x_ip_token})
33
  ltx_client = Client("KingNish/ltx-video-distilled", headers={"x-ip-token": x_ip_token})
34
- image = image_to_image(prompt, image, flux_client)
35
  yield image, None
36
  video = image_to_video(prompt, image, ltx_client)
37
  yield image, video
@@ -42,22 +94,12 @@ with gr.Blocks() as demo:
42
  with gr.Column():
43
  input_image = gr.Image(label="Input Image", type="filepath")
44
  prompt = gr.Textbox(label="Prompt")
 
45
  submit_button = gr.Button("Submit")
46
  with gr.Column():
47
  edited_image = gr.Image(label="Edited Image")
48
  output_video = gr.Video(label="Output Video")
49
- submit_button.click(personalized_video, [prompt, input_image], [edited_image, output_video])
50
- prompt.submit(personalized_video, [prompt, input_image], [edited_image, output_video])
51
-
52
- # gr.Examples(
53
- # examples=[
54
- # ["A sunny day", "https://black-forest-labs-flux-1-kontext-dev.hf.space/gradio_api/file=/tmp/gradio/927b05b08b8ec2201f7d95722471d2089d4197f152c9c07d0bc9f9672962d03b/flowers.png", None, None],
55
- # ],
56
- # inputs=[prompt, input_image, flux_client, ltx_client],
57
- # outputs=output_video,
58
- # fn=personalized_video,
59
- # cache_examples="lazy"
60
- # )
61
 
62
  demo.launch()
63
-
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
+ from PIL import Image, ImageOps
4
+ import os
5
+ import tempfile
6
 
7
+ def resize_and_pad(image_path, dimension):
8
+ # Open the image
9
+ img = Image.open(image_path)
10
+ original_width, original_height = img.size
11
+
12
+ if dimension == "Square (1024x1024)":
13
+ target_width, target_height = 1024, 1024
14
+ elif dimension == "Portrait (768x1360)":
15
+ target_width, target_height = 768, 1360
16
+ elif dimension == "Landscape (1360x768)":
17
+ target_width, target_height = 1360, 768
18
+ else:
19
+ raise ValueError("Invalid dimension selected")
20
+
21
+ # Calculate the scaling factor
22
+ width_ratio = target_width / original_width
23
+ height_ratio = target_height / original_height
24
+ scaling_factor = min(width_ratio, height_ratio)
25
+
26
+ # Calculate new dimensions
27
+ new_width = int(original_width * scaling_factor)
28
+ new_height = int(original_height * scaling_factor)
29
+
30
+ # Resize the image
31
+ img = img.resize((new_width, new_height), Image.LANCZOS)
32
+
33
+ # Create a new white image with target dimensions
34
+ new_img = Image.new("RGB", (target_width, target_height), (255, 255, 255))
35
+
36
+ # Calculate padding
37
+ offset = ((target_width - new_width) // 2, (target_height - new_height) // 2)
38
+
39
+ # Paste the resized image into the new image
40
+ new_img.paste(img, offset)
41
+
42
+ # Save the new image temporarily
43
+ padded_image_path = os.path.join(tempfile.gettempdir(), "padded_image.png")
44
+ new_img.save(padded_image_path)
45
+
46
+ return padded_image_path
47
+
48
+ def image_to_image(prompt, image, dimension, flux_client):
49
+ # First, resize and pad the image
50
+ padded_image_path = resize_and_pad(image, dimension)
51
+
52
+ prompt = "Transforms a user-provided input image based on their prompt while maintaining consistency and fidelity to the original image. Generate an output image that accurately reflects the user's requested modifications. " + prompt
53
  image = flux_client.predict(
54
+ input_image=handle_file(padded_image_path),
55
  prompt=prompt,
56
  seed=0,
57
  randomize_seed=True,
 
60
  api_name="/infer"
61
  )
62
  print(image)
63
+ # Clean up the temporary file (optional, but good practice)
64
+ try:
65
+ os.remove(padded_image_path)
66
+ except:
67
+ pass
68
  return image[0]
69
 
70
  def image_to_video(prompt, image, ltx_client):
71
  result = ltx_client.predict(
72
+ prompt=prompt,
73
+ input_image_url=None,
74
+ middle_image_url=handle_file(image),
75
+ final_image_url=None,
76
+ duration_ui=4,
77
+ api_name="/generate_video"
78
  )
79
  print(result)
80
  return result
81
 
82
+ def personalized_video(prompt, image, dimension, request: gr.Request):
83
  x_ip_token = request.headers['x-ip-token']
84
  flux_client = Client("black-forest-labs/FLUX.1-Kontext-Dev", headers={"x-ip-token": x_ip_token})
85
  ltx_client = Client("KingNish/ltx-video-distilled", headers={"x-ip-token": x_ip_token})
86
+ image = image_to_image(prompt, image, dimension, flux_client)
87
  yield image, None
88
  video = image_to_video(prompt, image, ltx_client)
89
  yield image, video
 
94
  with gr.Column():
95
  input_image = gr.Image(label="Input Image", type="filepath")
96
  prompt = gr.Textbox(label="Prompt")
97
+ dimension = gr.Radio(["Square (1024x1024)", "Portrait (768x1360)", "Landscape (1360x768)"], label="Dimension", value="Square (1024x1024)")
98
  submit_button = gr.Button("Submit")
99
  with gr.Column():
100
  edited_image = gr.Image(label="Edited Image")
101
  output_video = gr.Video(label="Output Video")
102
+ submit_button.click(personalized_video, [prompt, input_image, dimension], [edited_image, output_video])
103
+ prompt.submit(personalized_video, [prompt, input_image, dimension], [edited_image, output_video])
 
 
 
 
 
 
 
 
 
 
104
 
105
  demo.launch()