Spaces:
Running
Running
Nifemi Alpine Durin
commited on
Commit
·
f66af7a
1
Parent(s):
bb83891
switch to md
Browse files
app.py
CHANGED
|
@@ -8,60 +8,19 @@ API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021")
|
|
| 8 |
APP_ENV = os.environ.get("APP_ENV", None)
|
| 9 |
|
| 10 |
|
| 11 |
-
def image_to_base64(input_file):
|
| 12 |
-
file_bytes = None
|
| 13 |
-
file_path = None
|
| 14 |
-
|
| 15 |
-
if input_file is None:
|
| 16 |
-
return None
|
| 17 |
-
|
| 18 |
-
# Check if the input is a string (file path)
|
| 19 |
-
if isinstance(input_file, str):
|
| 20 |
-
|
| 21 |
-
file_path = input_file
|
| 22 |
-
with open(input_file, "rb") as file:
|
| 23 |
-
file_bytes = file.read()
|
| 24 |
-
else:
|
| 25 |
-
# Handle file-like object (_TemporaryFileWrapper)
|
| 26 |
-
file_path = input_file.name
|
| 27 |
-
file_bytes = input_file.read()
|
| 28 |
-
|
| 29 |
-
# Guess the MIME type of the file based on the file extension
|
| 30 |
-
mime_type, _ = mimetypes.guess_type(file_path)
|
| 31 |
-
if mime_type is None:
|
| 32 |
-
mime_type = 'application/octet-stream' # Use a binary data MIME type as a fallback
|
| 33 |
-
|
| 34 |
-
data_url = f"data:{mime_type};base64,{base64.b64encode(file_bytes).decode()}"
|
| 35 |
-
|
| 36 |
-
return data_url
|
| 37 |
-
|
| 38 |
|
| 39 |
# Assuming the API returns an image URL in the response
|
| 40 |
-
def generate_image(
|
| 41 |
-
|
| 42 |
-
if logo_image:
|
| 43 |
-
logo_base64 = image_to_base64(logo_image)
|
| 44 |
-
else:
|
| 45 |
-
logo_base64 = ""
|
| 46 |
|
| 47 |
|
| 48 |
# Define your payload/data to send to the image generation API
|
| 49 |
data = {
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"secondary_color": secondary_color,
|
| 53 |
-
"description": description,
|
| 54 |
-
"target_audience": target_audience,
|
| 55 |
-
"font": font,
|
| 56 |
-
"heading_text": heading_text or "",
|
| 57 |
-
"sub_heading_text": sub_heading_text or "",
|
| 58 |
-
"logo_url": logo_base64,
|
| 59 |
-
"user_prompt": custom_graphic_prompt,
|
| 60 |
-
"image_dimension": image_layout
|
| 61 |
}
|
| 62 |
|
| 63 |
# Make the API call
|
| 64 |
-
response = requests.post(API_URL, json=data)
|
| 65 |
|
| 66 |
# Ensure the API call was successful
|
| 67 |
if response.status_code != 200:
|
|
@@ -98,29 +57,59 @@ content_html = """
|
|
| 98 |
|
| 99 |
"""
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
iface = gr.Interface(
|
| 102 |
fn=generate_image,
|
| 103 |
inputs=[
|
| 104 |
-
gr.components.Textbox(placeholder="Brand
|
| 105 |
-
gr.components.ColorPicker(value="#FFFFFF", label="Primary Color"),
|
| 106 |
-
gr.components.ColorPicker(value="#05A592", label="Secondary Color"),
|
| 107 |
-
gr.components.Textbox(placeholder="Brief description of your brand and design guidelines", label="Description", lines=2),
|
| 108 |
-
gr.components.Textbox(placeholder="Your Brand's target audience", label="Target Audience"),
|
| 109 |
-
gr.components.Textbox(placeholder="Google Font Label i.e Rubik", label="Font"),
|
| 110 |
-
gr.components.File(label="Logo Image", scale=2, file_types=["jpg", "jpeg", "png", "gif", "bmp"]),
|
| 111 |
-
gr.components.Textbox(placeholder="Heading text (not required)", label="Heading Text"),
|
| 112 |
-
gr.components.Textbox(placeholder="Paragraph text (not required)", label="Sub Heading Text"),
|
| 113 |
-
gr.components.Dropdown(
|
| 114 |
-
choices=[
|
| 115 |
-
("Select Layout", "750x1334"),
|
| 116 |
-
("Mobile Portrait (750x1334)", "750x1334"),
|
| 117 |
-
("Mobile Landscape (940x470)", "940x470"),
|
| 118 |
-
("Square (2048x2048)", "2048x2048")
|
| 119 |
-
],
|
| 120 |
-
value="750x1334",
|
| 121 |
-
label="Image Layout"
|
| 122 |
-
),
|
| 123 |
-
gr.components.Textbox(lines=4, placeholder="Enter your custom dall-e 2-3 image prompt here (not required)", value="", label="Custom Graphic Prompt")
|
| 124 |
],
|
| 125 |
outputs=[
|
| 126 |
gr.components.Image(label="Generated Image")
|
|
|
|
| 8 |
APP_ENV = os.environ.get("APP_ENV", None)
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Assuming the API returns an image URL in the response
|
| 13 |
+
def generate_image(campaign_details):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
# Define your payload/data to send to the image generation API
|
| 17 |
data = {
|
| 18 |
+
"request_string": campaign_details,
|
| 19 |
+
"is_single_image": True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
|
| 22 |
# Make the API call
|
| 23 |
+
response = requests.post(API_URL + "/generate_graphic", json=data)
|
| 24 |
|
| 25 |
# Ensure the API call was successful
|
| 26 |
if response.status_code != 200:
|
|
|
|
| 57 |
|
| 58 |
"""
|
| 59 |
|
| 60 |
+
default_value = """
|
| 61 |
+
# Brand Name
|
| 62 |
+
Enter your brand name here
|
| 63 |
+
|
| 64 |
+
## Description
|
| 65 |
+
Enter your description here.
|
| 66 |
+
|
| 67 |
+
---
|
| 68 |
+
|
| 69 |
+
## Branding
|
| 70 |
+
|
| 71 |
+
### Colors
|
| 72 |
+
Primary Color: i.e #000000
|
| 73 |
+
Secondary Color: i.e #FFFFFF
|
| 74 |
+
|
| 75 |
+
### Font
|
| 76 |
+
Enter font here i.e Arial
|
| 77 |
+
|
| 78 |
+
### Logo
|
| 79 |
+
Enter your logo url here.
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## Audience
|
| 84 |
+
Enter your target audience here.
|
| 85 |
+
|
| 86 |
+
---
|
| 87 |
+
|
| 88 |
+
## Headings
|
| 89 |
+
### Main Heading
|
| 90 |
+
Your main heading text here.
|
| 91 |
+
|
| 92 |
+
### Sub-heading
|
| 93 |
+
Your sub-heading text here.
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
|
| 97 |
+
## Image Layout
|
| 98 |
+
### Select Layout
|
| 99 |
+
Layout can be any of: Mobile Portrait (750x1334), Mobile Landscape (940x470), Square (2048x2048)
|
| 100 |
+
|
| 101 |
+
Selected Layout: Mobile Portrait (750x1334)
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## Custom Graphics
|
| 106 |
+
Enter your custom graphic prompt here.
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
iface = gr.Interface(
|
| 110 |
fn=generate_image,
|
| 111 |
inputs=[
|
| 112 |
+
gr.components.Textbox(lines=10, placeholder="Enter your Brand and campaign details here", value=default_value, label="Campaign Details")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
],
|
| 114 |
outputs=[
|
| 115 |
gr.components.Image(label="Generated Image")
|