Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,7 +44,7 @@ model = TSR.from_pretrained(
|
|
| 44 |
config_name="config.yaml",
|
| 45 |
weight_name="model.ckpt",
|
| 46 |
)
|
| 47 |
-
model.renderer.set_chunk_size(
|
| 48 |
model.to(device)
|
| 49 |
|
| 50 |
rembg_session = rembg.new_session()
|
|
@@ -55,91 +55,6 @@ bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id
|
|
| 55 |
s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
|
| 56 |
|
| 57 |
|
| 58 |
-
def gen_pos_prompt(text):
|
| 59 |
-
instruction = f'''Your task is to create a positive prompt for image generation.
|
| 60 |
-
Objective: Generate images that prioritize structural integrity and accurate shapes. The focus should be on the correct form and basic contours of objects, with minimal concern for colors.
|
| 61 |
-
|
| 62 |
-
Guidelines:
|
| 63 |
-
|
| 64 |
-
Complex Objects (e.g., animals, vehicles): For these, the image should resemble a toy object, emphasizing the correct shape and structure while minimizing details and color complexity.
|
| 65 |
-
|
| 66 |
-
Example Input: A sports bike
|
| 67 |
-
Example Positive Prompt: Simple sports bike with accurate shape and structure, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, toy-like appearance, low contrast.
|
| 68 |
-
|
| 69 |
-
Example Input: A lion
|
| 70 |
-
Example Positive Prompt: Toy-like depiction of a lion with a focus on structural accuracy, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, simplified features, low contrast.
|
| 71 |
-
|
| 72 |
-
Simple Objects (e.g., a tennis ball): For these, the prompt should specify a realistic depiction, focusing on the accurate shape and structure.
|
| 73 |
-
|
| 74 |
-
Example Input: A tennis ball
|
| 75 |
-
Example Positive Prompt: Realistic depiction of a tennis ball with accurate shape and texture, digital painting, clean lines, minimal additional details, soft lighting, neutral or muted colors, focus on structural integrity.
|
| 76 |
-
|
| 77 |
-
Prompt Structure:
|
| 78 |
-
|
| 79 |
-
Subject: Clearly describe the object and its essential shape and structure.
|
| 80 |
-
Medium: Specify the art style (e.g., digital painting, concept art).
|
| 81 |
-
Style: Include relevant style terms (e.g., simplified, toy-like for complex objects; realistic for simple objects).
|
| 82 |
-
Resolution: Mention resolution if necessary (e.g., basic resolution).
|
| 83 |
-
Lighting: Indicate the type of lighting (e.g., soft lighting).
|
| 84 |
-
Color: Use neutral or muted colors with minimal emphasis on color details.
|
| 85 |
-
Additional Details: Keep additional details minimal or specify if not desired.
|
| 86 |
-
Input: {text}
|
| 87 |
-
Positive Prompt:
|
| 88 |
-
'''
|
| 89 |
-
|
| 90 |
-
body = json.dumps({'inputText': instruction,
|
| 91 |
-
'textGenerationConfig': {'temperature': 0.1, 'topP': 0.01, 'maxTokenCount':512}})
|
| 92 |
-
response = bedrock_runtime.invoke_model(body=body, modelId='amazon.titan-text-express-v1')
|
| 93 |
-
pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
|
| 94 |
-
return pos_prompt
|
| 95 |
-
|
| 96 |
-
def encode_image_to_base64(image):
|
| 97 |
-
with io.BytesIO() as buffered:
|
| 98 |
-
image.save(buffered, format="PNG")
|
| 99 |
-
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def generate_image_from_text(encoded_image, seed, pos_prompt=None):
|
| 103 |
-
neg_prompt = '''Detailed, complex textures, intricate patterns, realistic lighting, high contrast, reflections, fuzzy surface, realistic proportions, photographic quality, vibrant colors, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
|
| 104 |
-
encoded_str = encode_image_to_base64(encoded_image)
|
| 105 |
-
if pos_prompt:
|
| 106 |
-
parameters = {
|
| 107 |
-
'taskType': 'IMAGE_VARIATION',
|
| 108 |
-
'imageVariationParams': {
|
| 109 |
-
'images': [encoded_str],
|
| 110 |
-
'text': gen_pos_prompt(pos_prompt),
|
| 111 |
-
'negativeText': neg_prompt,
|
| 112 |
-
'similarityStrength': 0.7
|
| 113 |
-
},
|
| 114 |
-
'imageGenerationConfig': {
|
| 115 |
-
"cfgScale": 8,
|
| 116 |
-
"seed": seed,
|
| 117 |
-
"width": 512,
|
| 118 |
-
"height": 512,
|
| 119 |
-
"numberOfImages": 1
|
| 120 |
-
}
|
| 121 |
-
}
|
| 122 |
-
else:
|
| 123 |
-
parameters = {
|
| 124 |
-
'taskType': 'IMAGE_VARIATION',
|
| 125 |
-
'imageVariationParams': {
|
| 126 |
-
'images': [encoded_str],
|
| 127 |
-
'negativeText': neg_prompt,
|
| 128 |
-
'similarityStrength': 0.7
|
| 129 |
-
},
|
| 130 |
-
'imageGenerationConfig': {
|
| 131 |
-
"cfgScale": 8,
|
| 132 |
-
"seed": seed,
|
| 133 |
-
"width": 512,
|
| 134 |
-
"height": 512,
|
| 135 |
-
"numberOfImages": 1
|
| 136 |
-
}
|
| 137 |
-
}
|
| 138 |
-
request_body = json.dumps(parameters)
|
| 139 |
-
response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v1')
|
| 140 |
-
response_body = json.loads(response.get('body').read())
|
| 141 |
-
base64_image_data = base64.b64decode(response_body['images'][0])
|
| 142 |
-
return Image.open(io.BytesIO(base64_image_data))
|
| 143 |
|
| 144 |
|
| 145 |
def upload_file_to_s3(file_path, bucket_name, object_name=None):
|
|
@@ -217,12 +132,8 @@ async def process_image(
|
|
| 217 |
|
| 218 |
if auth == os.getenv("AUTHORIZE"):
|
| 219 |
image_bytes = await file.read()
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
if enhance_image:
|
| 223 |
-
image_pil = generate_image_from_text(encoded_image=input_image, seed=seed, pos_prompt=text_prompt)
|
| 224 |
-
else:
|
| 225 |
-
image_pil = input_image
|
| 226 |
|
| 227 |
preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
|
| 228 |
mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
|
|
|
|
| 44 |
config_name="config.yaml",
|
| 45 |
weight_name="model.ckpt",
|
| 46 |
)
|
| 47 |
+
model.renderer.set_chunk_size(131072)
|
| 48 |
model.to(device)
|
| 49 |
|
| 50 |
rembg_session = rembg.new_session()
|
|
|
|
| 55 |
s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
def upload_file_to_s3(file_path, bucket_name, object_name=None):
|
|
|
|
| 132 |
|
| 133 |
if auth == os.getenv("AUTHORIZE"):
|
| 134 |
image_bytes = await file.read()
|
| 135 |
+
image_pil = Image.open(BytesIO(image_bytes))
|
| 136 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
|
| 139 |
mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
|