Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- src/rep_api.py +40 -12
- src/utils.py +9 -10
src/rep_api.py
CHANGED
|
@@ -1,25 +1,32 @@
|
|
| 1 |
import replicate
|
| 2 |
import os
|
| 3 |
from src.utils import image_to_base64
|
| 4 |
-
from src.utils import BB_uploadfile
|
| 5 |
-
|
| 6 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def generate_image_replicate(prompt,api_path,aspect_ratio="1:1",model="dev",lora_scale=1,num_outputs=1,guidance_scale=3.5,seed=None):
|
| 11 |
-
print(prompt,api_path)
|
| 12 |
#if model=="dev":
|
| 13 |
num_inference_steps=30
|
| 14 |
if model=="schnell":
|
| 15 |
num_inference_steps=5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
inputs={
|
| 17 |
"model": model,
|
| 18 |
"prompt": prompt,
|
| 19 |
"lora_scale":lora_scale,
|
| 20 |
"aspect_ratio": aspect_ratio,
|
| 21 |
"num_outputs":num_outputs,
|
| 22 |
-
|
| 23 |
"num_inference_steps":num_inference_steps,
|
| 24 |
"guidance_scale":guidance_scale,
|
| 25 |
}
|
|
@@ -30,7 +37,10 @@ def generate_image_replicate(prompt,api_path,aspect_ratio="1:1",model="dev",lora
|
|
| 30 |
input=inputs
|
| 31 |
)
|
| 32 |
print(output)
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def replicate_caption_api(image,model,context_text):
|
|
@@ -77,13 +87,31 @@ def update_replicate_api_key(api_key):
|
|
| 77 |
os.environ["REPLICATE_API_TOKEN"] = api_key
|
| 78 |
return f"Replicate API key updated: {api_key[:5]}..." if api_key else "Replicate API key cleared"
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
def traning_function(zip_path,training_model,training_destination,seed,token_string,max_train_steps,hf_repo_id=None,hf_token=None):
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
print(zip_path)
|
| 88 |
training_logs = f"Using zip traning file at: {zip_path}\n"
|
| 89 |
yield training_logs, None
|
|
|
|
| 1 |
import replicate
|
| 2 |
import os
|
| 3 |
from src.utils import image_to_base64
|
| 4 |
+
from src.utils import BB_uploadfile
|
| 5 |
+
import json
|
| 6 |
import time
|
| 7 |
+
style_json="model_dict.json"
|
| 8 |
+
model_dict=json.load(open(style_json,"r"))
|
| 9 |
+
from dotenv import load_dotenv, find_dotenv
|
| 10 |
+
_ = load_dotenv(find_dotenv())
|
| 11 |
+
|
| 12 |
|
| 13 |
+
def generate_image_replicate(prompt,lora_model,api_path,aspect_ratio,gallery,model="dev",lora_scale=1,num_outputs=1,guidance_scale=3.5,seed=None):
|
| 14 |
+
print(prompt,lora_model,api_path,aspect_ratio)
|
|
|
|
|
|
|
| 15 |
#if model=="dev":
|
| 16 |
num_inference_steps=30
|
| 17 |
if model=="schnell":
|
| 18 |
num_inference_steps=5
|
| 19 |
+
|
| 20 |
+
if lora_model is not None:
|
| 21 |
+
api_path=model_dict[lora_model]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
inputs={
|
| 25 |
"model": model,
|
| 26 |
"prompt": prompt,
|
| 27 |
"lora_scale":lora_scale,
|
| 28 |
"aspect_ratio": aspect_ratio,
|
| 29 |
"num_outputs":num_outputs,
|
|
|
|
| 30 |
"num_inference_steps":num_inference_steps,
|
| 31 |
"guidance_scale":guidance_scale,
|
| 32 |
}
|
|
|
|
| 37 |
input=inputs
|
| 38 |
)
|
| 39 |
print(output)
|
| 40 |
+
if gallery is None:
|
| 41 |
+
gallery=[]
|
| 42 |
+
gallery.append(output[0])
|
| 43 |
+
return output[0],gallery
|
| 44 |
|
| 45 |
|
| 46 |
def replicate_caption_api(image,model,context_text):
|
|
|
|
| 87 |
os.environ["REPLICATE_API_TOKEN"] = api_key
|
| 88 |
return f"Replicate API key updated: {api_key[:5]}..." if api_key else "Replicate API key cleared"
|
| 89 |
|
| 90 |
+
from src.utils import create_zip
|
| 91 |
+
from PIL import Image
|
| 92 |
|
| 93 |
|
| 94 |
+
def process_images(files,model,context_text):
|
| 95 |
+
images = []
|
| 96 |
+
textbox =""
|
| 97 |
+
for file in files:
|
| 98 |
+
print(file)
|
| 99 |
+
image = Image.open(file)
|
| 100 |
+
caption = replicate_caption_api(image,model,context_text)
|
| 101 |
+
textbox += f"Tags: {caption}, file: " + os.path.basename(file) + "\n"
|
| 102 |
+
images.append(image)
|
| 103 |
+
#texts.append(textbox)
|
| 104 |
+
zip_path=create_zip(files,textbox,"TOK")
|
| 105 |
+
|
| 106 |
+
return images, textbox,zip_path
|
| 107 |
+
|
| 108 |
def traning_function(zip_path,training_model,training_destination,seed,token_string,max_train_steps,hf_repo_id=None,hf_token=None):
|
| 109 |
+
##Place holder for now
|
| 110 |
+
BB_bucket_name="jarvisdataset"
|
| 111 |
+
BB_defult="https://f005.backblazeb2.com/file/"
|
| 112 |
+
if BB_defult not in zip_path:
|
| 113 |
+
|
| 114 |
+
zip_path=BB_uploadfile(zip_path,os.path.basename(zip_path),BB_bucket_name)
|
| 115 |
print(zip_path)
|
| 116 |
training_logs = f"Using zip traning file at: {zip_path}\n"
|
| 117 |
yield training_logs, None
|
src/utils.py
CHANGED
|
@@ -57,18 +57,17 @@ def create_zip(files,captions,trigger):
|
|
| 57 |
caption_content = captions[i] +f", {trigger}"
|
| 58 |
zip_file.writestr(caption_name, caption_content)
|
| 59 |
return zip_path
|
| 60 |
-
info = b2.InMemoryAccountInfo()
|
| 61 |
-
b2_api = b2.B2Api(info)
|
| 62 |
-
application_key_id = os.getenv("BB_KeyID")
|
| 63 |
-
application_key = os.getenv("BB_AppKey")
|
| 64 |
-
#print(application_key_id,application_key)
|
| 65 |
-
b2_api.authorize_account("production", application_key_id, application_key)
|
| 66 |
-
BB_bucket_name=os.getenv("BB_bucket")
|
| 67 |
-
BB_bucket=b2_api.get_bucket_by_name(os.getenv("BB_bucket"))
|
| 68 |
-
BB_defurl="https://f005.backblazeb2.com/file/"
|
| 69 |
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
metadata = {"key": "value"}
|
| 73 |
uploaded_file = BB_bucket.upload_local_file(
|
| 74 |
local_file=local_file,
|
|
|
|
| 57 |
caption_content = captions[i] +f", {trigger}"
|
| 58 |
zip_file.writestr(caption_name, caption_content)
|
| 59 |
return zip_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
+
|
| 63 |
+
def BB_uploadfile(local_file,file_name,BB_bucket_name,FRIENDLY_URL=True,application_key_id = os.getenv("BB_KeyID"),application_key = os.getenv("BB_AppKey"),):
|
| 64 |
+
info = b2.InMemoryAccountInfo()
|
| 65 |
+
b2_api = b2.B2Api(info)
|
| 66 |
+
#print(application_key_id,application_key)
|
| 67 |
+
b2_api.authorize_account("production", application_key_id, application_key)
|
| 68 |
+
BB_bucket=b2_api.get_bucket_by_name(BB_bucket_name)
|
| 69 |
+
BB_defurl="https://f005.backblazeb2.com/file/"
|
| 70 |
+
|
| 71 |
metadata = {"key": "value"}
|
| 72 |
uploaded_file = BB_bucket.upload_local_file(
|
| 73 |
local_file=local_file,
|