Kalyankonga commited on
Commit
4f5bbed
·
verified ·
1 Parent(s): 5ef9711

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -38
app.py CHANGED
@@ -1,30 +1,15 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from PIL import Image
4
- import os
5
 
6
  # Connect to the verified IC-Light engine
7
  client = Client("lllyasviel/IC-Light")
8
 
9
- # DIAGNOSTIC: This will print the EXACT API structure to your logs
10
- print("--- FETCHING API STRUCTURE ---")
11
- try:
12
- print(client.view_api(return_format="str"))
13
- except:
14
- pass
15
- print("------------------------------")
16
-
17
  def dynamic_relight(image_path, prompt, lighting_choice, s1, s2, s3, s4):
18
  if image_path is None:
19
  return None
20
-
21
- # 1. Resize to verified safe dimensions (512x768 is the model's native ratio)
22
- img = Image.open(image_path)
23
- img.thumbnail((512, 768))
24
- temp_path = "safe_input.png"
25
- img.save(temp_path)
26
 
27
- # 2. Your Unique Project Feature: Slider-Weighted Prompt
28
  keywords = ["cinematic", "detailed", "texture", "focus"]
29
  slider_vals = [s1, s2, s3, s4]
30
  weighted_prompt = prompt
@@ -32,34 +17,44 @@ def dynamic_relight(image_path, prompt, lighting_choice, s1, s2, s3, s4):
32
  weight = slider_vals[i] / 50.0
33
  weighted_prompt += f", ({word}:{weight:.1f})"
34
 
35
- # 3. THE FIX: Sending ALL 13 Required Arguments
36
- # Based on the official repo's function signature
 
 
 
 
 
 
 
37
  try:
38
  result = client.predict(
39
- handle_file(temp_path), # 1. input_fg
40
- weighted_prompt, # 2. prompt
41
- "Appearance Variation", # 3. image_relation (Relight)
42
- lighting_choice, # 4. lighting_preference (Left/Right/etc)
43
- 12345, # 5. seed (Required int)
44
- 512, # 6. image_width
45
- 768, # 7. image_height
46
- 25, # 8. steps
47
- 2.0, # 9. cfg_scale
48
- 0.9, # 10. lowres_denoise
49
- 1.5, # 11. highres_scale
50
- 0.5, # 12. highres_denoise
51
- "best quality", # 13. a_prompt (Added prompt)
52
- "lowres, bad anatomy", # 14. n_prompt (Negative prompt)
53
- fn_index=1 # Target the primary generation tab
54
  )
55
- return result[0]
 
 
 
 
56
  except Exception as e:
57
- # Fallback: Prints the actual error if this precise payload fails
58
- raise gr.Error(f"API Mismatch: {str(e)}")
59
 
60
  # UI Setup
61
  with gr.Blocks() as demo:
62
- gr.Markdown("# 💡 Full-Parameter Feature Relighter")
63
  with gr.Row():
64
  with gr.Column():
65
  img = gr.Image(type="filepath", label="Input Image")
@@ -76,4 +71,4 @@ with gr.Blocks() as demo:
76
 
77
  btn.click(dynamic_relight, [img, txt, dirs, s1, s2, s3, s4], out)
78
 
79
- demo.launch()
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from PIL import Image
 
4
 
5
  # Connect to the verified IC-Light engine
6
  client = Client("lllyasviel/IC-Light")
7
 
 
 
 
 
 
 
 
 
8
  def dynamic_relight(image_path, prompt, lighting_choice, s1, s2, s3, s4):
9
  if image_path is None:
10
  return None
 
 
 
 
 
 
11
 
12
+ # 1. Slider Logic (Your Unique Feature)
13
  keywords = ["cinematic", "detailed", "texture", "focus"]
14
  slider_vals = [s1, s2, s3, s4]
15
  weighted_prompt = prompt
 
17
  weight = slider_vals[i] / 50.0
18
  weighted_prompt += f", ({word}:{weight:.1f})"
19
 
20
+ # 2. Safety Resize
21
+ # The log shows the default height is 640. We match this to avoid 'tensor size' errors.
22
+ img = Image.open(image_path)
23
+ img.thumbnail((512, 640))
24
+ safe_path = "safe_input.png"
25
+ img.save(safe_path)
26
+
27
+ # 3. THE VERIFIED API CALL
28
+ # Based strictly on the "Client.predict() Usage Info" you provided.
29
  try:
30
  result = client.predict(
31
+ input_fg=handle_file(safe_path), # 1. input_fg (Required)
32
+ prompt=weighted_prompt, # 2. prompt (Required)
33
+ image_width=512, # 3. image_width
34
+ image_height=640, # 4. image_height (Matches log default)
35
+ num_samples=1, # 5. num_samples
36
+ seed=12345, # 6. seed
37
+ steps=25, # 7. steps
38
+ a_prompt="best quality", # 8. a_prompt
39
+ n_prompt="lowres, bad anatomy", # 9. n_prompt
40
+ cfg=2.0, # 10. cfg
41
+ highres_scale=1.5, # 11. highres_scale
42
+ highres_denoise=0.5, # 12. highres_denoise
43
+ lowres_denoise=0.9, # 13. lowres_denoise
44
+ bg_source=lighting_choice, # 14. bg_source (Left Light, etc.)
45
+ api_name="/process_relight" # The CRITICAL name found in your logs
46
  )
47
+
48
+ # The API returns a tuple: (preprocessed_bg, gallery_list)
49
+ # We want the final image from the gallery list
50
+ return result[1][0]['image']
51
+
52
  except Exception as e:
53
+ raise gr.Error(f"Execution Failed: {str(e)}")
 
54
 
55
  # UI Setup
56
  with gr.Blocks() as demo:
57
+ gr.Markdown("# 💡 Final Accurate Feature Relighter")
58
  with gr.Row():
59
  with gr.Column():
60
  img = gr.Image(type="filepath", label="Input Image")
 
71
 
72
  btn.click(dynamic_relight, [img, txt, dirs, s1, s2, s3, s4], out)
73
 
74
+ demo.launch(show_error=True)