Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -41,19 +41,89 @@ model.to(device)
|
|
41 |
rembg_session = rembg.new_session()
|
42 |
my_aws_access_key_id = os.getenv("ACCESS")
|
43 |
my_aws_secret_access_key = os.getenv("SECRET")
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def generate_image_from_text(pos_prompt):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
def check_input_image(input_image):
|
59 |
if input_image is None:
|
|
|
41 |
rembg_session = rembg.new_session()
|
42 |
my_aws_access_key_id = os.getenv("ACCESS")
|
43 |
my_aws_secret_access_key = os.getenv("SECRET")
|
44 |
+
bedrock = boto3.client(service_name='bedrock', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
|
45 |
+
bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
|
46 |
+
# def generate_image_from_text(pos_prompt):
|
47 |
+
# # bedrock_runtime = boto3.client(region_name = 'us-east-1', service_name='bedrock-runtime')
|
48 |
+
# parameters = {'text_prompts': [{'text': pos_prompt , 'weight':1},
|
49 |
+
# {'text': """Blurry, out of frame, out of focus, Detailed, dull, duplicate, bad quality, low resolution, cropped""", 'weight': -1}],
|
50 |
+
# 'cfg_scale': 7, 'seed': 0, 'samples': 1}
|
51 |
+
# request_body = json.dumps(parameters)
|
52 |
+
# response = bedrock_runtime.invoke_model(body=request_body,modelId = 'stability.stable-diffusion-xl-v1')
|
53 |
+
# response_body = json.loads(response.get('body').read())
|
54 |
+
# base64_image_data = base64.b64decode(response_body['artifacts'][0]['base64'])
|
55 |
+
|
56 |
+
# return Image.open(io.BytesIO(base64_image_data))
|
57 |
+
|
58 |
+
|
59 |
+
def gen_pos_prompt(text):
|
60 |
+
instruction = f'''Your task is to create a positive prompt for image generation.
|
61 |
+
|
62 |
+
Objective: Generate images focusing on the correct shapes and basic contours of objects. The level of detail should vary depending on the complexity of the input.
|
63 |
+
|
64 |
+
Guidelines:
|
65 |
+
|
66 |
+
Complex Objects (e.g., animals, vehicles): For these, the image should resemble a toy object with simplified details and shapes.
|
67 |
+
|
68 |
+
Example Input: A sports bike
|
69 |
+
Example Positive Prompt: Simple red sports bike with streamlined shape, minimal details, digital painting, concept art style, flat colors, basic contours, soft lighting, no intricate designs, clean lines, neutral background, smooth surfaces, low contrast. (Toy-like appearance)
|
70 |
+
|
71 |
+
Example Input: A lion
|
72 |
+
Example Positive Prompt: Toy-like depiction of a lion with simplified features, minimal details, digital painting, concept art style, flat colors, basic contours, soft lighting, clean lines, neutral background, smooth surfaces, low contrast.
|
73 |
+
|
74 |
+
Simple Objects (e.g., a tennis ball): For these, the prompt should specify a realistic depiction and not a digital painting.
|
75 |
+
|
76 |
+
Example Input: A tennis ball
|
77 |
+
Example Positive Prompt: A realistic depiction of a tennis ball, showing its accurate shape and texture, clean lines, minimal additional details, soft lighting, neutral background.
|
78 |
+
|
79 |
+
Prompt Structure:
|
80 |
+
|
81 |
+
Subject: Clearly describe the object and its essential shape.
|
82 |
+
Medium: Specify the art style (e.g., digital painting, concept art).
|
83 |
+
Style: Include relevant style terms (e.g., simplified, toy-like for complex objects; realistic for simple objects).
|
84 |
+
Resolution: Mention resolution if necessary (e.g., basic resolution).
|
85 |
+
Lighting: Indicate the type of lighting (e.g., soft lighting).
|
86 |
+
Color: Describe the color scheme (e.g., flat colors, basic color).
|
87 |
+
Additional Details: Keep additional details minimal or specify if not desired.
|
88 |
+
Keywords for Reference:
|
89 |
+
|
90 |
+
Medium: Digital painting, concept art
|
91 |
+
Style: Simplified, toy-like (for complex objects), realistic (for simple objects)
|
92 |
+
Lighting: Soft lighting
|
93 |
+
Color: Flat colors, basic color
|
94 |
+
Resolution: Basic resolution
|
95 |
+
Additional Details: Minimal, clean lines
|
96 |
+
|
97 |
+
Input: {text}
|
98 |
+
Positive Prompt:
|
99 |
+
'''
|
100 |
+
|
101 |
+
body = json.dumps({'inputText': instruction,
|
102 |
+
'textGenerationConfig': {'temperature': 0.1, 'topP': 0.01, 'maxTokenCount':512}})
|
103 |
+
response = bedrock_runtime.invoke_model(body=body, modelId='amazon.titan-text-express-v1')
|
104 |
+
pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
|
105 |
+
return pos_prompt
|
106 |
|
107 |
def generate_image_from_text(pos_prompt):
|
108 |
+
new_prompt = gen_pos_prompt(pos_prompt)
|
109 |
+
neg_prompt = '''Highly detailed, complex textures, intricate patterns, realistic lighting, high contrast, reflections, fuzzy surface, realistic proportions, hyperrealistic, photographic quality, vibrant colors, detailed background, shadows, disfigured, deformed, ugly.'''
|
110 |
+
parameters = {
|
111 |
+
'taskType': 'TEXT_IMAGE',
|
112 |
+
'textToImageParams': {'text': new_prompt,
|
113 |
+
'negativeText': neg_prompt},
|
114 |
+
'imageGenerationConfig': {"cfgScale":8,
|
115 |
+
"seed":0,
|
116 |
+
"width":512,
|
117 |
+
"height":512,
|
118 |
+
"numberOfImages":1
|
119 |
+
}
|
120 |
+
}
|
121 |
+
request_body = json.dumps(parameters)
|
122 |
+
response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v1')
|
123 |
+
response_body = json.loads(response.get('body').read())
|
124 |
+
base64_image_data = base64.b64decode(response_body['images'][0])
|
125 |
+
|
126 |
+
return Image.open(io.BytesIO(base64_image_data))
|
127 |
|
128 |
def check_input_image(input_image):
|
129 |
if input_image is None:
|