Spaces:
Sleeping
Sleeping
File size: 13,200 Bytes
766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc b283998 766c4fc ac2193d 766c4fc b283998 ac2193d 766c4fc ac2193d 766c4fc ac2193d 766c4fc ac2193d 766c4fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
import replicate
import os
from src.utils import image_to_base64 ,open_image_from_url, update_model_dicts, BB_uploadfile,numpy_to_base64
from src.deepl import detect_and_translate
import json
import time
style_json="model_dict.json"
model_dict=json.load(open(style_json,"r"))
def generate_image_control_net(prompt,lora_model,api_path,aspect_ratio,lora_scale,
use_control_net,control_net_type,control_net_img,control_net_strength,
num_outputs=1,guidance_scale=3.5,seed=None,
):
#print(prompt,lora_model,api_path,aspect_ratio,use_control_net)
#print(prompt,len(prompt),type(prompt),prompt is None)
if len(prompt)==0:
prompt=os.environ["default_promt"]
#print(prompt,lora_model,api_path,aspect_ratio)
inputs = {
"prompt": detect_and_translate(prompt),
"output_format": "png",
"num_outputs":num_outputs,
"guidance_scale": guidance_scale,
"output_quality": 100,
}
if seed is not None:
inputs["seed"]=seed
if use_control_net:
api_path= "xlabs-ai/flux-dev-controlnet:f2c31c31d81278a91b2447a304dae654c64a5d5a70340fba811bb1cbd41019a2" #X labs control net replicate repo
lora_url=model_dict[lora_model][1]
assert control_net_img is not None, "Please add control net image"
control_net_img=image_to_base64(control_net_img)
inputs["lora_url"]=lora_url
inputs["prompt"]+=", "+model_dict[lora_model][2]
inputs["control_image"]=control_net_img
inputs["control_type"]=control_net_type
inputs["control_strengh"]=control_net_strength
#other settings
inputs["steps"]=30
inputs["lora_strength"]=lora_scale
inputs["negative_prompt"]= "low quality, ugly, distorted, artefacts"
else:
api_path=model_dict[lora_model][0]
inputs["aspect_ratio"]=aspect_ratio
inputs["prompt"]+=", "+model_dict[lora_model][2]
inputs["num_inference_steps"]=28
inputs["model"]="dev"
inputs["lora_scale"]=lora_scale
#run model
output = replicate.run(
api_path,
input=inputs
)
#print(output)
return output[0]
def generate_image_replicate(prompt,lora_model,api_path,aspect_ratio,model,lora_scale,
num_outputs=1,guidance_scale=3.5,seed=None,
):
#print(prompt,len(prompt),type(prompt),prompt is None)
if len(prompt)==0:
prompt=os.environ["default_promt"]
#print(prompt,lora_model,api_path,aspect_ratio)
#if model=="dev":
num_inference_steps=30
if model=="schnell":
num_inference_steps=5
if lora_model is not None:
api_path=model_dict[lora_model][0]
inputs={
"model": model,
"prompt": detect_and_translate(prompt),
"lora_scale":lora_scale,
"aspect_ratio": aspect_ratio,
"num_outputs":num_outputs,
"num_inference_steps":num_inference_steps,
"guidance_scale":guidance_scale,
"output_format":"png",
}
if seed is not None:
inputs["seed"]=seed
output = replicate.run(
api_path,
input=inputs
)
#print(output)
return output[0]
def replicate_bgcontrolnet(img,prompt,background_prompt, sampler_name= "DPM++ SDE Karras",
negative_prompt="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, mutated hands and fingers:1.4), (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, disconnected limbs, mutation, mutated, ugly, disgusting, amputation"
):
img=image_to_base64(img)
prompt=prompt+" ," +background_prompt
output=replicate.run(
"wolverinn/realistic-background:9f020c55e037529bf20ed1cb799d7aa290404cfbd45157686717ffc7ee511eab",
input={
"seed": -1,
"image":img,
"prompt":prompt,
"sampler_name":sampler_name,
"negative_prompt":negative_prompt
}
)
return output["image"]
def replicate_caption_api(image,model,context_text):
#print(model,context_text)
base64_image = image_to_base64(image)
if model=="blip":
output = replicate.run(
"andreasjansson/blip-2:f677695e5e89f8b236e52ecd1d3f01beb44c34606419bcc19345e046d8f786f9",
input={
"image": base64_image,
"caption": True,
"question": context_text,
"temperature": 1,
"use_nucleus_sampling": False
}
)
#print(output)
elif model=="llava-16":
output = replicate.run(
# "yorickvp/llava-13b:80537f9eead1a5bfa72d5ac6ea6414379be41d4d4f6679fd776e9535d1eb58bb",
"yorickvp/llava-v1.6-34b:41ecfbfb261e6c1adf3ad896c9066ca98346996d7c4045c5bc944a79d430f174",
input={
"image": base64_image,
"top_p": 1,
"prompt": context_text,
"max_tokens": 1024,
"temperature": 0.2
}
)
#print(output)
output = "".join(output)
elif model=="img2prompt":
output = replicate.run(
"methexis-inc/img2prompt:50adaf2d3ad20a6f911a8a9e3ccf777b263b8596fbd2c8fc26e8888f8a0edbb5",
input={
"image":base64_image
}
)
#print(output)
return output
def update_replicate_api_key(api_key):
os.environ["REPLICATE_API_TOKEN"] = api_key
return f"Replicate API key updated: {api_key[:5]}..." if api_key else "Replicate API key cleared"
def virtual_try_on(crop, seed, steps, category, garm_img, human_img, garment_des):
output = replicate.run(
"cuuupid/idm-vton:906425dbca90663ff5427624839572cc56ea7d380343d13e2a4c4b09d3f0c30f",
input={
"crop": crop,
"seed": seed,
"steps": steps,
"category": category,
# "force_dc": force_dc,
"garm_img": numpy_to_base64( garm_img),
"human_img": numpy_to_base64(human_img),
#"mask_only": mask_only,
"garment_des": garment_des
}
)
#print(output)
return output
from src.utils import create_zip
from PIL import Image
def process_images(files,model,context_text,token_string):
images = []
textbox =""
for file in files:
#print(file)
image = Image.open(file)
if model=="None":
caption="[Insert cap here]"
else:
caption = replicate_caption_api(image,model,context_text)
textbox += f"Tags: {caption}, file: " + os.path.basename(file) + "\n"
images.append(image)
#texts.append(textbox)
zip_path=create_zip(files,textbox,token_string)
return images, textbox,zip_path
def replicate_create_model(owner,name,visibility="private",hardware="gpu-a40-large"):
try:
model = replicate.models.create(
owner=owner,
name=name,
visibility=visibility,
hardware=hardware,
)
print(model)
return True
except Exception as e:
print(e)
if "A model with that name and owner already exists" in str(e):
return True
return False
def traning_function(zip_path,training_model,training_destination,seed,token_string,max_train_steps,hf_repo_id=None,hf_token=None):
##Place holder for now
BB_bucket_name="jarvisdataset"
BB_defult="https://f005.backblazeb2.com/file/"
if BB_defult not in zip_path:
zip_path=BB_uploadfile(zip_path,os.path.basename(zip_path),BB_bucket_name)
#print(zip_path)
training_logs = f"Using zip traning file at: {zip_path}\n"
yield training_logs, None
input={
"steps": max_train_steps,
"lora_rank": 16,
"batch_size": 1,
"autocaption": True,
"trigger_word": token_string,
"learning_rate": 0.0004,
"seed": seed,
"input_images": zip_path
}
#print(training_destination)
username,model_name=training_destination.split("/")
assert replicate_create_model(username,model_name,visibility="private",hardware="gpu-a40-large"),"Error in creating model on replicate, check API key and username is correct "
#print(input)
try:
training = replicate.trainings.create(
destination=training_destination,
version="ostris/flux-dev-lora-trainer:1296f0ab2d695af5a1b5eeee6e8ec043145bef33f1675ce1a2cdb0f81ec43f02",
input=input,
)
training_logs = f"Training started with model: {training_model}\n"
training_logs += f"Destination: {training_destination}\n"
training_logs += f"Seed: {seed}\n"
training_logs += f"Token string: {token_string}\n"
training_logs += f"Max train steps: {max_train_steps}\n"
# Poll the training status
while training.status != "succeeded":
training.reload()
training_logs += f"Training status: {training.status}\n"
training_logs += f"{training.logs}\n"
if training.status == "failed":
training_logs += "Training failed!\n"
return training_logs, training
yield training_logs, None
time.sleep(10) # Wait for 10 seconds before checking again
training_logs += "Training completed!\n"
if hf_repo_id and hf_token:
training_logs += f"Uploading to Hugging Face repo: {hf_repo_id}\n"
# Here you would implement the logic to upload to Hugging Face
traning_finnal=training.output
# In a real scenario, you might want to download and display some result images
# For now, we'll just return the original images
#images = [Image.open(file) for file in files]
_= update_model_dicts(traning_finnal["version"],token_string,style_json="model_dict.json")
traning_finnal["replicate_link"]="https://replicate.com/"+traning_finnal["version"].replace(":","/")
yield training_logs, traning_finnal
except Exception as e:
yield f"An error occurred: {str(e)}", None
def sam_segment(image,prompt,negative_prompt,adjustment_factor=-15):
#img2 base64
image = image_to_base64(image)
output = replicate.run(
"schananas/grounded_sam:ee871c19efb1941f55f66a3d7d960428c8a5afcb77449547fe8e5a3ab9ebc21c",
input={
"image": image,
"mask_prompt": prompt,
"adjustment_factor": adjustment_factor,
"negative_mask_prompt":negative_prompt
}
)
out_items={}
for item in output:
# https://replicate.com/schananas/grounded_sam/api#output-schema
#print(item)
out_items[os.path.basename(item).split(".")[0]]=item
return out_items
def replicate_zest(img,material_img="https://replicate.delivery/pbxt/Kl23gJODaW7EuxrDzBG9dcgqRdMaYSWmBQ9UexnwPiL7AnIr/3.jpg"):
if type(img)!=str:
img=image_to_base64(img)
if type(material_img)!=str:
material_img=image_to_base64(material_img)
output = replicate.run(
"camenduru/zest:11abc0a411459327938957581151c642dd1bee4cefe443a9a63b230c4fbc0952",
input={
"input_image": img,
"material_image":material_img
}
)
#print(output)
return output
from src.utils import resize_image,find_closest_valid_dimension
light_source_options=[
"Use Background Image",
"Left Light",
"Right Light",
"Top Light",
"Bottom Light",
"Ambient"
]
defult_image="https://replicate.delivery/pbxt/KxPIbJUjSmVBlvn0M3C8PAz6brN5Z0eyZSGcKIVw3XfJ6vNV/7.webp"
def replicate_iclight_BG(img,prompt,bg_img,light_source="Use Background Image",
negative_prompt="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, mutated hands and fingers:1.4), (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, disconnected limbs, mutation, mutated, ugly, disgusting, amputation"
):
assert light_source in light_source_options, "Please select a correct ligh source option"
width, height = img.size
#print(width,height)
img=image_to_base64(img)
#if light_source=="Use Background Image":
if bg_img is None:
bg_img=open_image_from_url(defult_image)
bg_img=image_to_base64(bg_img)
#else:
#bg_img=None
prompt=prompt
output=replicate.run(
"zsxkib/ic-light-background:60015df78a8a795470da6494822982140d57b150b9ef14354e79302ff89f69e3",
input={
"cfg": 2,
"steps": 25,
"width": width,
"height": height,
"prompt": prompt,
"light_source": light_source,
"highres_scale": 1.5,
"output_format": "png",
"subject_image": img,
"compute_normal": False,
"output_quality": 100,
"appended_prompt": "best quality",
"highres_denoise": 0.5,
"negative_prompt": negative_prompt,
"background_image": bg_img,
"number_of_images": 1
}
)
return output[0]
|