Spaces:
Running
on
Zero
Running
on
Zero
serverless
Browse files- requirements.txt +2 -0
- serverless_handler.py +69 -0
requirements.txt
CHANGED
@@ -11,4 +11,6 @@ torchsde
|
|
11 |
compel
|
12 |
onnxruntime-gpu
|
13 |
hidiffusion
|
|
|
|
|
14 |
git+https://github.com/tencent-ailab/IP-Adapter.git
|
|
|
11 |
compel
|
12 |
onnxruntime-gpu
|
13 |
hidiffusion
|
14 |
+
runpod
|
15 |
+
boto3
|
16 |
git+https://github.com/tencent-ailab/IP-Adapter.git
|
serverless_handler.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import runpod
|
2 |
+
import auth
|
3 |
+
import app_imagegen
|
4 |
+
import json
|
5 |
+
import uuid
|
6 |
+
import boto3 # Ensure you have boto3 installed for Cloudflare R2 interaction
|
7 |
+
from botocore.exceptions import NoCredentialsError
|
8 |
+
from botocore.client import Config
|
9 |
+
import os # Import os to access environment variables
|
10 |
+
from datetime import datetime
|
11 |
+
import re
|
12 |
+
|
13 |
+
config = json.load(open("config.json"))
|
14 |
+
model_manager = app_imagegen.ModelManager(config, max_loaded_models=2, lazy_load=True)
|
15 |
+
|
16 |
+
def handler(job):
|
17 |
+
job_input = job["input"]
|
18 |
+
result = app_imagegen.image_gen_api_entry(
|
19 |
+
job_input["prompt"],
|
20 |
+
job_input.get("negative_prompt",""),
|
21 |
+
job_input.get("styles",{}),
|
22 |
+
job_input.get("options",{}),
|
23 |
+
job_input.get("token",""),
|
24 |
+
model_manager
|
25 |
+
)
|
26 |
+
user_id = job_input.get("user_id","")
|
27 |
+
domain = job_input.get("domain","")
|
28 |
+
# Save result as WebP using a GUID as filename
|
29 |
+
filename = f"{uuid.uuid4()}.webp"
|
30 |
+
with open(filename, "wb") as f:
|
31 |
+
f.write(result) # Assuming result is in bytes
|
32 |
+
|
33 |
+
# Upload to Cloudflare R2
|
34 |
+
r2_client = boto3.client('s3',
|
35 |
+
endpoint_url=f"https://{config['r2']['account_id']}.r2.cloudflarestorage.com",
|
36 |
+
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'), # Read from environment variable
|
37 |
+
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'), # Read from environment variable
|
38 |
+
config=Config(signature_version='s3v4'),
|
39 |
+
region_name='auto'
|
40 |
+
)
|
41 |
+
# Prepare metadata
|
42 |
+
publish_time = datetime.now().isoformat()
|
43 |
+
sanitized_prompt = re.sub(r'[^\x20-\x7E]', ' ', job_input["prompt"]) # Replace non-ASCII characters with space
|
44 |
+
sanitized_prompt = sanitized_prompt[:512] # Limit length to 256 characters
|
45 |
+
metadata = {
|
46 |
+
'domain': domain,
|
47 |
+
'model': job_input.get("styles",{}).get("vibe","") ,
|
48 |
+
'prompt': ''.join([i if ord(i) < 128 else ' ' for i in sanitized_prompt]),
|
49 |
+
'author': user_id,
|
50 |
+
'publish_time': str(publish_time)
|
51 |
+
}
|
52 |
+
new_url_path = ""
|
53 |
+
try:
|
54 |
+
r2_key = f"paid/{user_id}/{filename}"
|
55 |
+
r2_client.put_object(
|
56 |
+
Bucket=config['r2']['bucket_name'],
|
57 |
+
Key=r2_key,
|
58 |
+
Body=open(filename, 'rb'),
|
59 |
+
ContentType='image/webp', # Adjust this if the image type might vary
|
60 |
+
Metadata=metadata
|
61 |
+
)
|
62 |
+
new_url_path = f"/{r2_key}"
|
63 |
+
except NoCredentialsError:
|
64 |
+
print("Credentials not available")
|
65 |
+
|
66 |
+
return {"image_url":new_url_path}
|
67 |
+
|
68 |
+
|
69 |
+
runpod.serverless.start({"handler": handler})
|