martintomov commited on
Commit
a280fbf
1 Parent(s): 9552c68
Files changed (1) hide show
  1. app.py +48 -42
app.py CHANGED
@@ -9,11 +9,18 @@ import time
9
  import base64
10
  import json
11
 
 
 
 
 
 
 
 
12
  with open("examples/examples.json") as f:
13
  examples = json.load(f)
14
 
15
  # IC Light, Replace Background
16
- async def submit_ic_light_bria(api_key, image_data, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color):
17
  if not lightsource_start_color.startswith("#"):
18
  lightsource_start_color = f"#{lightsource_start_color}"
19
  if not lightsource_end_color.startswith("#"):
@@ -31,7 +38,6 @@ async def submit_ic_light_bria(api_key, image_data, positive_prompt, negative_pr
31
  "lightsource_start_color": lightsource_start_color,
32
  "lightsource_end_color": lightsource_end_color
33
  },
34
- credentials={"api_key": api_key} # Pass the user's API key dynamically
35
  )
36
 
37
  log_index = 0
@@ -63,7 +69,7 @@ async def submit_ic_light_bria(api_key, image_data, positive_prompt, negative_pr
63
  return [f"Error: {str(e)}"], None
64
 
65
  # SDXL, Depth Anything, Replace Background
66
- async def submit_sdxl_rembg(api_key, image_data, positive_prompt, negative_prompt):
67
  retries = 3
68
  for attempt in range(retries):
69
  try:
@@ -74,7 +80,6 @@ async def submit_sdxl_rembg(api_key, image_data, positive_prompt, negative_promp
74
  "Positive prompt": positive_prompt,
75
  "Negative prompt": negative_prompt
76
  },
77
- credentials={"api_key": api_key} # Pass the user's API key dynamically
78
  )
79
 
80
  log_index = 0
@@ -101,12 +106,12 @@ async def submit_sdxl_rembg(api_key, image_data, positive_prompt, negative_promp
101
  except Exception as e:
102
  print(f"Attempt {attempt + 1} failed: {e}")
103
  if attempt < retries - 1:
104
- time.sleep(2) # HTTP req retry mechanism
105
  else:
106
  return [f"Error: {str(e)}"], None
107
 
108
  # SV3D, AnimateDiff
109
- async def submit_sv3d(api_key, image_data, fps, loop_frames_count, gif_loop):
110
  retries = 3
111
  for attempt in range(retries):
112
  try:
@@ -118,7 +123,6 @@ async def submit_sv3d(api_key, image_data, fps, loop_frames_count, gif_loop):
118
  "Loop Frames Count": loop_frames_count,
119
  "GIF Loop": gif_loop
120
  },
121
- credentials={"api_key": api_key} # Pass the user's API key dynamically
122
  )
123
 
124
  log_index = 0
@@ -150,17 +154,17 @@ def convert_image_to_base64(image):
150
  image.save(buffered, format="PNG")
151
  return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
152
 
153
- def submit_sync_ic_light_bria(api_key, image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color):
154
  image_data = convert_image_to_base64(Image.open(image_upload))
155
- return asyncio.run(submit_ic_light_bria(api_key, image_data, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color))
156
 
157
- def submit_sync_sdxl_rembg(api_key, image_upload, positive_prompt, negative_prompt):
158
  image_data = convert_image_to_base64(Image.open(image_upload))
159
- return asyncio.run(submit_sdxl_rembg(api_key, image_data, positive_prompt, negative_prompt))
160
 
161
- def submit_sync_sv3d(api_key, image_upload, fps, loop_frames_count, gif_loop):
162
  image_data = convert_image_to_base64(Image.open(image_upload))
163
- return asyncio.run(submit_sv3d(api_key, image_data, fps, loop_frames_count, gif_loop))
164
 
165
  def run_gradio_app():
166
  with gr.Blocks() as demo:
@@ -171,13 +175,7 @@ def run_gradio_app():
171
  gr.Markdown("🧡 Bitcoin address - bc1qs3q0rjpr9fvn9knjy5aktfr8w5duvvjpezkgt9")
172
  gr.Markdown("🚀 Want to run your own workflow? Import it into [fal.ai](https://fal.ai)'s ComfyUI and get a Python API endpoint.")
173
 
174
- # API Key input section
175
- with gr.Row():
176
- api_key_input = gr.Textbox(label="Enter your fal.ai API Key", type="password")
177
- api_key_submit = gr.Button("Submit API Key")
178
-
179
- # Main app content (initially hidden)
180
- with gr.Row(visible=False) as main_content:
181
  with gr.Column(scale=1):
182
  workflow = gr.Dropdown(label="Select Workflow", choices=["IC Light, Replace Background", "SDXL, Depth Anything, Replace Background", "SV3D"], value="IC Light, Replace Background")
183
  image_upload = gr.Image(label="Upload Image", type="filepath")
@@ -194,37 +192,45 @@ def run_gradio_app():
194
  output_logs = gr.Textbox(label="Logs")
195
  output_result = gr.Image(label="Result")
196
 
197
- def validate_api_key(api_key):
198
- return gr.Row(visible=True), api_key
199
-
200
- api_key_submit.click(validate_api_key, [api_key_input], [main_content, api_key_input])
201
-
202
- def submit_handler(api_key, workflow, image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop):
203
  if workflow == "IC Light, Replace Background":
204
- logs, result_image = submit_sync_ic_light_bria(api_key, image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color)
205
- return logs, result_image
206
  elif workflow == "SDXL, Depth Anything, Replace Background":
207
- logs, result_image = submit_sync_sdxl_rembg(api_key, image_upload, positive_prompt, negative_prompt)
208
- return logs, result_image
209
  elif workflow == "SV3D":
210
- logs, gif_url = submit_sync_sv3d(api_key, image_upload, fps, loop_frames_count, gif_loop)
211
- response = requests.get(gif_url)
212
- gif_bytes = BytesIO(response.content)
213
- return logs, Image.open(gif_bytes)
214
 
215
- submit_btn.click(submit_handler, [api_key_input, workflow, image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop], [output_logs, output_result])
216
 
217
- def update_fields(workflow):
218
  if workflow == "IC Light, Replace Background":
219
- return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
220
  elif workflow == "SDXL, Depth Anything, Replace Background":
221
- return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
222
  elif workflow == "SV3D":
223
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
224
-
225
- workflow.change(update_fields, workflow, [positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  demo.launch()
228
 
229
  if __name__ == "__main__":
230
- run_gradio_app()
 
9
  import base64
10
  import json
11
 
12
+ # Local Dev
13
+ import os
14
+ from dotenv import load_dotenv
15
+
16
+ load_dotenv()
17
+ FAL_KEY = os.getenv("FAL_KEY")
18
+
19
  with open("examples/examples.json") as f:
20
  examples = json.load(f)
21
 
22
  # IC Light, Replace Background
23
+ async def submit_ic_light_bria(image_data, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color):
24
  if not lightsource_start_color.startswith("#"):
25
  lightsource_start_color = f"#{lightsource_start_color}"
26
  if not lightsource_end_color.startswith("#"):
 
38
  "lightsource_start_color": lightsource_start_color,
39
  "lightsource_end_color": lightsource_end_color
40
  },
 
41
  )
42
 
43
  log_index = 0
 
69
  return [f"Error: {str(e)}"], None
70
 
71
  # SDXL, Depth Anything, Replace Background
72
+ async def submit_sdxl_rembg(image_data, positive_prompt, negative_prompt):
73
  retries = 3
74
  for attempt in range(retries):
75
  try:
 
80
  "Positive prompt": positive_prompt,
81
  "Negative prompt": negative_prompt
82
  },
 
83
  )
84
 
85
  log_index = 0
 
106
  except Exception as e:
107
  print(f"Attempt {attempt + 1} failed: {e}")
108
  if attempt < retries - 1:
109
+ time.sleep(2) # HTTP req retry mechanism
110
  else:
111
  return [f"Error: {str(e)}"], None
112
 
113
  # SV3D, AnimateDiff
114
+ async def submit_sv3d(image_data, fps, loop_frames_count, gif_loop):
115
  retries = 3
116
  for attempt in range(retries):
117
  try:
 
123
  "Loop Frames Count": loop_frames_count,
124
  "GIF Loop": gif_loop
125
  },
 
126
  )
127
 
128
  log_index = 0
 
154
  image.save(buffered, format="PNG")
155
  return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
156
 
157
+ def submit_sync_ic_light_bria(image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color):
158
  image_data = convert_image_to_base64(Image.open(image_upload))
159
+ return asyncio.run(submit_ic_light_bria(image_data, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color))
160
 
161
+ def submit_sync_sdxl_rembg(image_upload, positive_prompt, negative_prompt):
162
  image_data = convert_image_to_base64(Image.open(image_upload))
163
+ return asyncio.run(submit_sdxl_rembg(image_data, positive_prompt, negative_prompt))
164
 
165
+ def submit_sync_sv3d(image_upload, fps, loop_frames_count, gif_loop):
166
  image_data = convert_image_to_base64(Image.open(image_upload))
167
+ return asyncio.run(submit_sv3d(image_data, fps, loop_frames_count, gif_loop))
168
 
169
  def run_gradio_app():
170
  with gr.Blocks() as demo:
 
175
  gr.Markdown("🧡 Bitcoin address - bc1qs3q0rjpr9fvn9knjy5aktfr8w5duvvjpezkgt9")
176
  gr.Markdown("🚀 Want to run your own workflow? Import it into [fal.ai](https://fal.ai)'s ComfyUI and get a Python API endpoint.")
177
 
178
+ with gr.Row():
 
 
 
 
 
 
179
  with gr.Column(scale=1):
180
  workflow = gr.Dropdown(label="Select Workflow", choices=["IC Light, Replace Background", "SDXL, Depth Anything, Replace Background", "SV3D"], value="IC Light, Replace Background")
181
  image_upload = gr.Image(label="Upload Image", type="filepath")
 
192
  output_logs = gr.Textbox(label="Logs")
193
  output_result = gr.Image(label="Result")
194
 
195
+ def update_ui(workflow):
 
 
 
 
 
196
  if workflow == "IC Light, Replace Background":
197
+ return [gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
 
198
  elif workflow == "SDXL, Depth Anything, Replace Background":
199
+ return [gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
 
200
  elif workflow == "SV3D":
201
+ return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)]
 
 
 
202
 
203
+ workflow.change(fn=update_ui, inputs=workflow, outputs=[positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop])
204
 
205
+ def on_submit(image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop, workflow):
206
  if workflow == "IC Light, Replace Background":
207
+ logs, image = submit_sync_ic_light_bria(image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color)
208
+ return logs, image
209
  elif workflow == "SDXL, Depth Anything, Replace Background":
210
+ logs, image = submit_sync_sdxl_rembg(image_upload, positive_prompt, negative_prompt)
211
+ return logs, image
212
  elif workflow == "SV3D":
213
+ logs, gif_url = submit_sync_sv3d(image_upload, fps, loop_frames_count, gif_loop)
214
+ return logs, gif_url
215
+
216
+ submit_btn.click(
217
+ fn=on_submit,
218
+ inputs=[image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop, workflow],
219
+ outputs=[output_logs, output_result]
220
+ )
221
+
222
+ gr.Examples(
223
+ examples=[
224
+ [example['input_image'], example['positive_prompt'], example['negative_prompt'], example.get('lightsource_start_color', "#FFFFFF"), example.get('lightsource_end_color', "#000000"), example.get('fps', 8), example.get('loop_frames_count', 30), example.get('gif_loop', True), example['workflow']]
225
+ for example in examples
226
+ ],
227
+ inputs=[image_upload, positive_prompt, negative_prompt, lightsource_start_color, lightsource_end_color, fps, loop_frames_count, gif_loop, workflow],
228
+ outputs=[output_logs, output_result],
229
+ fn=on_submit,
230
+ cache_examples=True
231
+ )
232
 
233
  demo.launch()
234
 
235
  if __name__ == "__main__":
236
+ run_gradio_app()