alvdansen commited on
Commit
b94a3eb
β€’
1 Parent(s): 9b35819

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -29
app.py CHANGED
@@ -1,12 +1,3 @@
1
- import json
2
- import random
3
-
4
- import gradio as gr
5
- import numpy as np
6
- import spaces
7
- import torch
8
- from diffusers import DiffusionPipeline, LCMScheduler
9
-
10
  with open("sdxl_lora.json", "r") as file:
11
  data = json.load(file)
12
  sdxl_loras_raw = [
@@ -38,17 +29,50 @@ pipe.to(device=DEVICE, dtype=torch.float16)
38
  MAX_SEED = np.iinfo(np.int32).max
39
  MAX_IMAGE_SIZE = 1024
40
 
41
-
42
- def update_selection(
43
- selected_state: gr.SelectData,
44
- gr_sdxl_loras,
45
- ):
46
-
47
  lora_id = gr_sdxl_loras[selected_state.index]["repo"]
48
  trigger_word = gr_sdxl_loras[selected_state.index]["trigger_word"]
49
-
50
  return lora_id, trigger_word
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  @spaces.GPU
54
  def infer(
@@ -63,19 +87,7 @@ def infer(
63
  user_lora_weight,
64
  progress=gr.Progress(track_tqdm=True),
65
  ):
66
- flash_sdxl_id = "jasperai/flash-sdxl"
67
-
68
- new_adapter_id = user_lora_selector.replace("/", "_")
69
- loaded_adapters = pipe.get_list_adapters()
70
-
71
- if new_adapter_id not in loaded_adapters["unet"]:
72
- gr.Info("Swapping LoRA")
73
- pipe.unload_lora_weights()
74
- pipe.load_lora_weights(flash_sdxl_id, adapter_name="lora")
75
- pipe.load_lora_weights(user_lora_selector, adapter_name=new_adapter_id)
76
-
77
- pipe.set_adapters(["lora", new_adapter_id], adapter_weights=[1.0, user_lora_weight])
78
- gr.Info("LoRA setup done")
79
 
80
  if randomize_seed:
81
  seed = random.randint(0, MAX_SEED)
 
 
 
 
 
 
 
 
 
 
1
  with open("sdxl_lora.json", "r") as file:
2
  data = json.load(file)
3
  sdxl_loras_raw = [
 
29
  MAX_SEED = np.iinfo(np.int32).max
30
  MAX_IMAGE_SIZE = 1024
31
 
32
+ def update_selection(selected_state: gr.SelectData, gr_sdxl_loras):
 
 
 
 
 
33
  lora_id = gr_sdxl_loras[selected_state.index]["repo"]
34
  trigger_word = gr_sdxl_loras[selected_state.index]["trigger_word"]
 
35
  return lora_id, trigger_word
36
 
37
+ def load_lora_for_style(style_repo):
38
+ pipe.unload_lora_weights()
39
+ pipe.load_lora_weights(style_repo, adapter_name="lora")
40
+
41
+ def get_image(image_data):
42
+ if isinstance(image_data, str):
43
+ return image_data
44
+
45
+ if isinstance(image_data, dict):
46
+ local_path = image_data.get('local_path')
47
+ hf_url = image_data.get('hf_url')
48
+ else:
49
+ print(f"Unexpected image_data format: {type(image_data)}")
50
+ return None
51
+
52
+ # Try loading from local path first
53
+ if local_path and os.path.exists(local_path):
54
+ try:
55
+ Image.open(local_path).verify() # Verify that it's a valid image
56
+ return local_path
57
+ except Exception as e:
58
+ print(f"Error loading local image {local_path}: {e}")
59
+
60
+ # If local path fails or doesn't exist, try URL
61
+ if hf_url:
62
+ try:
63
+ response = requests.get(hf_url)
64
+ if response.status_code == 200:
65
+ img = Image.open(requests.get(hf_url, stream=True).raw)
66
+ img.verify() # Verify that it's a valid image
67
+ img.save(local_path) # Save for future use
68
+ return local_path
69
+ else:
70
+ print(f"Failed to fetch image from URL {hf_url}. Status code: {response.status_code}")
71
+ except Exception as e:
72
+ print(f"Error loading image from URL {hf_url}: {e}")
73
+
74
+ print(f"Failed to load image for {image_data}")
75
+ return None
76
 
77
  @spaces.GPU
78
  def infer(
 
87
  user_lora_weight,
88
  progress=gr.Progress(track_tqdm=True),
89
  ):
90
+ load_lora_for_style(user_lora_selector)
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  if randomize_seed:
93
  seed = random.randint(0, MAX_SEED)