Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
01c2d0a
1
Parent(s): b828572
Deploy from GitHub Actions: ac48a1a5c78eb0252c4d6fb0baf19ca81d8b3be5
Browse files- .env.example +8 -2
- src/main.py +21 -23
.env.example
CHANGED
|
@@ -10,5 +10,11 @@ MODEL_NAME=
|
|
| 10 |
# Cache
|
| 11 |
REDIS_URL=
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Cache
|
| 11 |
REDIS_URL=
|
| 12 |
|
| 13 |
+
# Uploadthing
|
| 14 |
+
UPLOADTHING_SECRET=
|
| 15 |
+
|
| 16 |
+
# Web App
|
| 17 |
+
WEB_APP_URL=
|
| 18 |
+
|
| 19 |
+
# Worker Secret
|
| 20 |
+
WORKER_SECRET=
|
src/main.py
CHANGED
|
@@ -15,46 +15,44 @@ logging.basicConfig(level=logging.INFO)
|
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
REDIS_URL = os.getenv("REDIS_URL")
|
| 18 |
-
|
| 19 |
|
| 20 |
if not REDIS_URL:
|
| 21 |
-
logger.
|
|
|
|
| 22 |
|
| 23 |
-
if not
|
| 24 |
-
logger.
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
r = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
| 27 |
|
| 28 |
session = new_session(MODEL_NAME)
|
| 29 |
|
| 30 |
-
app = FastAPI(title="NoBG Worker
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def upload_to_vercel_blob(image_bytes: bytes, filename: str) -> str:
|
| 34 |
-
token = os.getenv("BLOB_READ_WRITE_TOKEN")
|
| 35 |
-
if not token:
|
| 36 |
-
raise ValueError("BLOB_READ_WRITE_TOKEN environment variable is missing.")
|
| 37 |
-
|
| 38 |
-
url = f"https://blob.vercel-storage.com/{filename}"
|
| 39 |
|
| 40 |
-
headers = {
|
| 41 |
-
"Authorization": f"Bearer {token}",
|
| 42 |
-
"X-Content-Type": "image/png",
|
| 43 |
-
}
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
if response.status_code
|
| 48 |
-
logger.error(f"
|
| 49 |
response.raise_for_status()
|
| 50 |
|
| 51 |
data = response.json()
|
| 52 |
blob_url = data.get("url")
|
| 53 |
|
| 54 |
if not blob_url:
|
| 55 |
-
raise ValueError("
|
| 56 |
|
| 57 |
-
logger.info(f"Successfully uploaded to
|
| 58 |
return blob_url
|
| 59 |
|
| 60 |
|
|
@@ -86,7 +84,7 @@ def process_job(job_data_str: str):
|
|
| 86 |
else:
|
| 87 |
filename = f"{original_filename}-nobg.png"
|
| 88 |
|
| 89 |
-
result_url =
|
| 90 |
|
| 91 |
r.hset(
|
| 92 |
f"{PREFIX}:job_status:{job_id}",
|
|
|
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
REDIS_URL = os.getenv("REDIS_URL")
|
| 18 |
+
WEB_APP_URL = os.getenv("WEB_APP_URL")
|
| 19 |
|
| 20 |
if not REDIS_URL:
|
| 21 |
+
logger.error("REDIS_URL is not set. Worker will fail to start.")
|
| 22 |
+
exit(1)
|
| 23 |
|
| 24 |
+
if not WEB_APP_URL:
|
| 25 |
+
logger.error("WEB_APP_URL is not set. Worker will fail to upload.")
|
| 26 |
+
exit(1)
|
| 27 |
+
|
| 28 |
+
WORKER_SECRET = os.getenv("WORKER_SECRET")
|
| 29 |
|
| 30 |
r = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
| 31 |
|
| 32 |
session = new_session(MODEL_NAME)
|
| 33 |
|
| 34 |
+
app = FastAPI(title="NoBG Worker")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
def upload_to_uploadthing(image_bytes: bytes, filename: str) -> str:
|
| 38 |
+
url = f"{WEB_APP_URL}/api/worker/upload"
|
| 39 |
+
|
| 40 |
+
files = {"file": (filename, image_bytes, "image/png")}
|
| 41 |
+
headers = {"Authorization": f"Bearer {WORKER_SECRET}"} if WORKER_SECRET else {}
|
| 42 |
+
|
| 43 |
+
response = requests.post(url, files=files, headers=headers)
|
| 44 |
|
| 45 |
+
if response.status_code != 200:
|
| 46 |
+
logger.error(f"Worker Upload API error: {response.status_code} - {response.text}")
|
| 47 |
response.raise_for_status()
|
| 48 |
|
| 49 |
data = response.json()
|
| 50 |
blob_url = data.get("url")
|
| 51 |
|
| 52 |
if not blob_url:
|
| 53 |
+
raise ValueError("Worker Upload API did not return a URL")
|
| 54 |
|
| 55 |
+
logger.info(f"Successfully uploaded to Uploadthing: {blob_url}")
|
| 56 |
return blob_url
|
| 57 |
|
| 58 |
|
|
|
|
| 84 |
else:
|
| 85 |
filename = f"{original_filename}-nobg.png"
|
| 86 |
|
| 87 |
+
result_url = upload_to_uploadthing(img_bytes, filename)
|
| 88 |
|
| 89 |
r.hset(
|
| 90 |
f"{PREFIX}:job_status:{job_id}",
|