Spaces:
Sleeping
Sleeping
Upload helpers.py
Browse files- helpers.py +46 -0
helpers.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import replicate
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
def encode_image(file_path):
|
| 10 |
+
with open(file_path, "rb") as f:
|
| 11 |
+
base64_image = base64.b64encode(f.read()).decode("utf-8")
|
| 12 |
+
return base64_image
|
| 13 |
+
|
| 14 |
+
def get_as_base64(url):
|
| 15 |
+
|
| 16 |
+
return base64.b64encode(requests.get(url).content)
|
| 17 |
+
|
| 18 |
+
def generate_rep_image(wear, prompt):
|
| 19 |
+
|
| 20 |
+
repl_client = replicate.Client(api_token=os.getenv("REPLICATE_KEY"))
|
| 21 |
+
# print(encode_image(wear))
|
| 22 |
+
output = repl_client.run(
|
| 23 |
+
"black-forest-labs/flux-kontext-pro",
|
| 24 |
+
input={
|
| 25 |
+
"prompt": prompt,
|
| 26 |
+
"input_image": open(wear, "rb"),
|
| 27 |
+
"aspect_ratio": "1:1",
|
| 28 |
+
"output_format": "png",
|
| 29 |
+
"safety_tolerance": 2
|
| 30 |
+
}
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
print(output)
|
| 34 |
+
|
| 35 |
+
final_output = repl_client.run(
|
| 36 |
+
"daanelson/real-esrgan-a100:f94d7ed4a1f7e1ffed0d51e4089e4911609d5eeee5e874ef323d2c7562624bed",
|
| 37 |
+
input={
|
| 38 |
+
"image": str(output),
|
| 39 |
+
"scale": 4,
|
| 40 |
+
"face_enhance": False
|
| 41 |
+
}
|
| 42 |
+
)
|
| 43 |
+
rep_image = get_as_base64(final_output)
|
| 44 |
+
print(final_output)
|
| 45 |
+
return rep_image
|
| 46 |
+
|