Spaces:
Running
Running
Commit ·
dd80798
1
Parent(s): ef735cd
fixing the url issue
Browse files
generator_function/image_function.py
CHANGED
|
@@ -32,6 +32,7 @@ def _encode_image_to_base64(image_path):
|
|
| 32 |
|
| 33 |
logger = logging.getLogger("imagegen_service")
|
| 34 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 35 |
|
| 36 |
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
| 37 |
MAX_WORKERS = min(32, (os.cpu_count() or 1) + 4)
|
|
@@ -39,6 +40,11 @@ REQUEST_TIMEOUT = 30
|
|
| 39 |
RETRY_ATTEMPTS = 3
|
| 40 |
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def get_cost_per_image(
|
| 43 |
model_key: str,
|
| 44 |
width: int | None = None,
|
|
@@ -183,7 +189,7 @@ def _get_model_config_cached(model_key: str) -> Optional[Dict[str, Any]]:
|
|
| 183 |
|
| 184 |
def _s3():
|
| 185 |
if not hasattr(_thread_local, "s3"):
|
| 186 |
-
needed = ["R2_ENDPOINT","R2_ACCESS_KEY","R2_SECRET_KEY","R2_BUCKET_NAME"
|
| 187 |
if any(not os.getenv(k) for k in needed):
|
| 188 |
_thread_local.s3 = None
|
| 189 |
return None
|
|
@@ -214,7 +220,7 @@ def _upload_to_r2(image_bytes: bytes) -> Optional[str]:
|
|
| 214 |
Body=image_bytes,
|
| 215 |
ContentType="image/png",
|
| 216 |
)
|
| 217 |
-
return f"{
|
| 218 |
except Exception as e:
|
| 219 |
if attempt == RETRY_ATTEMPTS - 1:
|
| 220 |
logger.error(f"R2 upload failed: {e}")
|
|
|
|
| 32 |
|
| 33 |
logger = logging.getLogger("imagegen_service")
|
| 34 |
logging.basicConfig(level=logging.INFO)
|
| 35 |
+
CANONICAL_STATIC_BASE = "https://adgen.static.lookfinity.com"
|
| 36 |
|
| 37 |
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
| 38 |
MAX_WORKERS = min(32, (os.cpu_count() or 1) + 4)
|
|
|
|
| 40 |
RETRY_ATTEMPTS = 3
|
| 41 |
|
| 42 |
|
| 43 |
+
def _public_base_url() -> str:
|
| 44 |
+
base = (os.getenv("NEW_BASE") or "").strip().rstrip("/")
|
| 45 |
+
return base or CANONICAL_STATIC_BASE
|
| 46 |
+
|
| 47 |
+
|
| 48 |
def get_cost_per_image(
|
| 49 |
model_key: str,
|
| 50 |
width: int | None = None,
|
|
|
|
| 189 |
|
| 190 |
def _s3():
|
| 191 |
if not hasattr(_thread_local, "s3"):
|
| 192 |
+
needed = ["R2_ENDPOINT", "R2_ACCESS_KEY", "R2_SECRET_KEY", "R2_BUCKET_NAME"]
|
| 193 |
if any(not os.getenv(k) for k in needed):
|
| 194 |
_thread_local.s3 = None
|
| 195 |
return None
|
|
|
|
| 220 |
Body=image_bytes,
|
| 221 |
ContentType="image/png",
|
| 222 |
)
|
| 223 |
+
return f"{_public_base_url()}/{key}"
|
| 224 |
except Exception as e:
|
| 225 |
if attempt == RETRY_ATTEMPTS - 1:
|
| 226 |
logger.error(f"R2 upload failed: {e}")
|
generator_function/video_hashid_function.py
CHANGED
|
@@ -7,6 +7,12 @@ import boto3
|
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# ---------- HASH UTILS ----------
|
| 12 |
def _sha256(path: Path, chunk: int = 1 << 20) -> str:
|
|
@@ -112,7 +118,7 @@ def upload_video_to_r2(video_bytes: bytes, file_name: str) -> str:
|
|
| 112 |
|
| 113 |
s3.put_object(Bucket=bucket, Key=key, Body=video_bytes, ContentType="video/mp4")
|
| 114 |
|
| 115 |
-
return f"{
|
| 116 |
|
| 117 |
# ---------- PROCESS + UPLOAD ----------
|
| 118 |
def process_video_with_hash_info(input_path: str, output_path: Optional[str] = None, variation_idx: int = 0) -> dict:
|
|
|
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
+
CANONICAL_STATIC_BASE = "https://adgen.static.lookfinity.com"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _public_base_url() -> str:
|
| 14 |
+
base = (os.getenv("NEW_BASE") or "").strip().rstrip("/")
|
| 15 |
+
return base or CANONICAL_STATIC_BASE
|
| 16 |
|
| 17 |
# ---------- HASH UTILS ----------
|
| 18 |
def _sha256(path: Path, chunk: int = 1 << 20) -> str:
|
|
|
|
| 118 |
|
| 119 |
s3.put_object(Bucket=bucket, Key=key, Body=video_bytes, ContentType="video/mp4")
|
| 120 |
|
| 121 |
+
return f"{_public_base_url()}/{key}"
|
| 122 |
|
| 123 |
# ---------- PROCESS + UPLOAD ----------
|
| 124 |
def process_video_with_hash_info(input_path: str, output_path: Optional[str] = None, variation_idx: int = 0) -> dict:
|
helpers_function/helpers.py
CHANGED
|
@@ -7,6 +7,7 @@ import base64
|
|
| 7 |
import logging
|
| 8 |
|
| 9 |
logger = logging.getLogger(__name__)
|
|
|
|
| 10 |
|
| 11 |
def get_video_thumbnail_base64(video_path: str, time_sec: int = 1) -> str:
|
| 12 |
try:
|
|
@@ -25,6 +26,11 @@ def get_video_thumbnail_base64(video_path: str, time_sec: int = 1) -> str:
|
|
| 25 |
|
| 26 |
log = logging.getLogger(__name__)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def upload_image_to_r2(image_bytes, folder_name="search_arb", app_type="bulk_generation"):
|
| 29 |
try:
|
| 30 |
s3 = boto3.client(
|
|
@@ -37,7 +43,7 @@ def upload_image_to_r2(image_bytes, folder_name="search_arb", app_type="bulk_gen
|
|
| 37 |
filename = f"{uuid4().hex}.png"
|
| 38 |
file_key = f"hug_face/{app_type.strip('/')}/{folder_name.strip('/')}/{filename}" if folder_name else f"hug_face/{app_type.strip('/')}/{filename}"
|
| 39 |
s3.put_object(Bucket=os.getenv('R2_BUCKET_NAME'), Key=file_key, Body=image_bytes, ContentType="image/png")
|
| 40 |
-
return f"{
|
| 41 |
except Exception as e:
|
| 42 |
log.error(e)
|
| 43 |
return None
|
|
|
|
| 7 |
import logging
|
| 8 |
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
+
CANONICAL_STATIC_BASE = "https://adgen.static.lookfinity.com"
|
| 11 |
|
| 12 |
def get_video_thumbnail_base64(video_path: str, time_sec: int = 1) -> str:
|
| 13 |
try:
|
|
|
|
| 26 |
|
| 27 |
log = logging.getLogger(__name__)
|
| 28 |
|
| 29 |
+
|
| 30 |
+
def _public_base_url() -> str:
|
| 31 |
+
base = (os.getenv("NEW_BASE") or "").strip().rstrip("/")
|
| 32 |
+
return base or CANONICAL_STATIC_BASE
|
| 33 |
+
|
| 34 |
def upload_image_to_r2(image_bytes, folder_name="search_arb", app_type="bulk_generation"):
|
| 35 |
try:
|
| 36 |
s3 = boto3.client(
|
|
|
|
| 43 |
filename = f"{uuid4().hex}.png"
|
| 44 |
file_key = f"hug_face/{app_type.strip('/')}/{folder_name.strip('/')}/{filename}" if folder_name else f"hug_face/{app_type.strip('/')}/{filename}"
|
| 45 |
s3.put_object(Bucket=os.getenv('R2_BUCKET_NAME'), Key=file_key, Body=image_bytes, ContentType="image/png")
|
| 46 |
+
return f"{_public_base_url()}/{file_key}"
|
| 47 |
except Exception as e:
|
| 48 |
log.error(e)
|
| 49 |
return None
|