Spaces:
Running
Running
File size: 15,859 Bytes
753e5f6 195ffb7 753e5f6 195ffb7 c438cdb 753e5f6 195ffb7 c6f24f8 753e5f6 f66af7a 195ffb7 753e5f6 f66af7a 1ae4512 753e5f6 195ffb7 3f5b005 753e5f6 195ffb7 753e5f6 1ae4512 195ffb7 1ae4512 195ffb7 753e5f6 1ae4512 753e5f6 42e93b3 643adaf 42e93b3 643adaf 42e93b3 f66af7a 3630a7f f66af7a 643adaf 1ae4512 195ffb7 f66af7a 103355c f66af7a 195ffb7 c55f324 daf848f c55f324 daf848f c55f324 46e3f65 c438cdb 4196d7f ec93fba 4196d7f cc56263 c438cdb cc56263 c438cdb cc56263 09d8509 cc56263 c438cdb cc56263 c438cdb 195ffb7 1aa1480 c438cdb 26efb3b c438cdb c55f324 dc56d68 c438cdb 643adaf c438cdb 195ffb7 c438cdb 63eb876 c438cdb 643adaf c438cdb 1bfe075 195ffb7 a967c6e 195ffb7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
import gradio as gr
import requests, base64, io
from dotenv import load_dotenv
import os, datetime, mimetypes
from gradio import Request
load_dotenv()
API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021")
APP_ENV = os.environ.get("APP_ENV", None)
# Assuming the API returns an image URL in the response
def generate_image(campaign_details):
# Define your payload/data to send to the image generation API
data = {
"request_string": campaign_details,
"is_single_image": False
}
# Make the API call
response = requests.post(
API_URL + "/generate_graphic",
json=data,
timeout=900 # Timeout set to 15 minutes (900 seconds)
)
# Ensure the API call was successful
if response.status_code != 200:
print(f"Error {response.status_code}: {response.text}")
return "Error: Unable to fetch image from the external API."
image_data = response.json()
data_url = image_data['image_url']
image_desc = image_data['image_description']
return data_url
content_html = """
<div style="text-align: center; margin-top: 30px;">
<h2 style="font-size: 24px; margin-bottom: 20px;">Slidegen Examples</h2>
<div style="display: flex; justify-content: space-between; align-items: flex-start; margin: 20px auto; max-width: 90%;">
<!-- Portrait video on the left -->
<div style="width: 30%; padding-right: 20px;">
<video controls style="width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">
<source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<p style="margin-top: 10px; font-size: 14px; color: #666;">Mobile Portrait Layout</p>
</div>
<!-- Grid of images on the right -->
<div style="width: 65%; display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px;">
<!-- Actual images -->
<div style="width: 100%;">
<img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" alt="Square Layout" style="width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">
<p style="margin-top: 5px; font-size: 14px; color: #666;">Square Layout</p>
</div>
<div style="width: 100%;">
<img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" alt="Mobile Landscape" style="width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">
<p style="margin-top: 5px; font-size: 14px; color: #666;">Mobile Landscape</p>
</div>
<!-- Placeholder for Screenshot layout -->
<div style="width: 100%;">
<div style="width: 100%; height: 0; padding-bottom: 62.5%; background-color: #f0f0f0; border-radius: 8px; position: relative; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<div style="width: 50px; height: 50px; margin: 0 auto 10px; border-radius: 50%; background-color: #ddd; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 24px; color: #888;">+</span>
</div>
<p style="margin: 0; font-size: 14px; color: #888;">Screenshot Layout</p>
</div>
</div>
<p style="margin-top: 5px; font-size: 14px; color: #666;">Screenshots (1280x800)</p>
</div>
<!-- Placeholder for Custom layout -->
<div style="width: 100%;">
<div style="width: 100%; height: 0; padding-bottom: 62.5%; background-color: #f0f0f0; border-radius: 8px; position: relative; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<div style="width: 50px; height: 50px; margin: 0 auto 10px; border-radius: 50%; background-color: #ddd; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 24px; color: #888;">+</span>
</div>
<p style="margin: 0; font-size: 14px; color: #888;">Custom Layout</p>
</div>
</div>
<p style="margin-top: 5px; font-size: 14px; color: #666;">Custom Size</p>
</div>
<!-- Placeholder for Instagram layout -->
<div style="width: 100%;">
<div style="width: 100%; height: 0; padding-bottom: 62.5%; background-color: #f0f0f0; border-radius: 8px; position: relative; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<div style="width: 50px; height: 50px; margin: 0 auto 10px; border-radius: 50%; background-color: #ddd; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 24px; color: #888;">+</span>
</div>
<p style="margin: 0; font-size: 14px; color: #888;">Instagram</p>
</div>
</div>
<p style="margin-top: 5px; font-size: 14px; color: #666;">Instagram Story (1080x1920)</p>
</div>
<!-- Placeholder for Twitter layout -->
<div style="width: 100%;">
<div style="width: 100%; height: 0; padding-bottom: 62.5%; background-color: #f0f0f0; border-radius: 8px; position: relative; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<div style="width: 50px; height: 50px; margin: 0 auto 10px; border-radius: 50%; background-color: #ddd; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 24px; color: #888;">+</span>
</div>
<p style="margin: 0; font-size: 14px; color: #888;">Twitter</p>
</div>
</div>
<p style="margin-top: 5px; font-size: 14px; color: #666;">Twitter Post (1200x675)</p>
</div>
<!-- Placeholder for Tablet layout -->
<div style="width: 100%;">
<div style="width: 100%; height: 0; padding-bottom: 70%; background-color: #f0f0f0; border-radius: 8px; position: relative; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<div style="width: 50px; height: 50px; margin: 0 auto 10px; border-radius: 50%; background-color: #ddd; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 24px; color: #888;">+</span>
</div>
<p style="margin: 0; font-size: 14px; color: #888;">Tablet</p>
</div>
</div>
<p style="margin-top: 5px; font-size: 14px; color: #666;">Tablet (2280x1600)</p>
</div>
</div>
</div>
<p style="margin-top: 20px; font-size: 16px; color: #666;">Choose a layout that suits your needs from the examples above</p>
</div>
"""
default_value = """
# Brand Name
Enter your brand name here
## Description
Enter your description here.
---
## Branding
### Colors
Primary Color: i.e #000000
Secondary Color: i.e #FFFFFF
### Font
Enter Google font here i.e Arial
### Logo
Enter your logo url here.
---
## Audience
Enter your target audience here.
---
## Headings
### Main Heading
Your main heading text here.
### Sub-heading
Your sub-heading text here.
---
## Image Layout
Selected Layout: Mobile Portrait (750x1334)
**Examples of layouts: Mobile Portrait (750x1334), Mobile Landscape (940x470), Square (2048x2048), Screenshots (1280x800), Screenshots Xlarge (2280x1600)
---
## Custom Graphics
Enter your custom graphic prompt here.
## Custom Graphics Image Layout
Feature Image Background Color (Optional): i.e #008000
"""
# Escape default_value for JavaScript
escaped_default_value = default_value.replace('\n', '\\n').replace('\'', '\\\'')
js = f'''
function js() {{
// let default_value = '{escaped_default_value}';
window.set_cookie = function(key, value) {{
// if (!key || !value || value.length < 20) {{
// console.log("Cannot set cookie, key or value is undefined or null", key, value);
// console.log("default value set to: ", default_value);
// return [default_value];
// }}
// document.cookie = key + '=' + value + '; Path=/; SameSite=Strict';
// return [value];
}}
}}
'''
def get_config(request: Request):
if not request or not hasattr(request, 'cookies'):
return default_value
# Initialize with default value
config = {"campaign_details": default_value}
# Check if 'campaign_details' is in cookies
if 'campaign_details' in request.cookies:
# Assign cookie value to config if exists
config_value = request.cookies.get('campaign_details', None)
if config_value:
if len(config_value) > 20:
config['campaign_details'] = config_value
else:
print("Cookie 'campaign_details' is found but empty, using default_value")
else:
print("Cookie 'campaign_details' not found, using default_value")
print(f"Config returned: {config['campaign_details']}") # Debugging log
return config['campaign_details']
with gr.Blocks(analytics_enabled=False) as iface:
campaign_details_input = gr.Textbox(
lines=10,
placeholder="Enter your Brand and campaign details here",
label="Campaign Details",
value=default_value
)
campaign_details_input.change(
fn=None,
inputs=campaign_details_input,
outputs=[],
js="(value) => { console.log('Setting cookie:', value); set_cookie('campaign_details', value); }"
)
# Create the main interface
interface = gr.Interface(
fn=generate_image,
inputs=campaign_details_input,
outputs=[
gr.components.Image(label="Generated Image")
],
title="Slidegen AI - Image generator",
description="Generate social media creatives from a few prompts",
live=False,
js=js
)
# Add examples section separately
gr.HTML("""
<div style="margin-top: 30px; text-align: center;">
<h3>Slidegen Examples</h3>
<div style="display: flex; margin: 20px auto; max-width: 90%;">
<div style="width: 30%; padding-right: 20px;">
<video width="100%" controls>
<source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4">
</video>
<div>Mobile Portrait</div>
</div>
<div style="width: 70%;">
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px;">
<div>
<img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" width="100%">
<div style="margin-top: 5px;">Square Layout</div>
</div>
<div>
<img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" width="100%">
<div style="margin-top: 5px;">Mobile Landscape</div>
</div>
<div>
<div style="background: #f0f0f0; height: 150px; display: flex; justify-content: center; align-items: center; border-radius: 8px;">
<div style="text-align: center;">
<div style="width: 40px; height: 40px; margin: 0 auto 10px; border-radius: 50%; background: #ddd; display: flex; justify-content: center; align-items: center;">+</div>
<div>Screenshot Layout</div>
</div>
</div>
<div style="margin-top: 5px;">Screenshots (1280x800)</div>
</div>
<div>
<div style="background: #f0f0f0; height: 150px; display: flex; justify-content: center; align-items: center; border-radius: 8px;">
<div style="text-align: center;">
<div style="width: 40px; height: 40px; margin: 0 auto 10px; border-radius: 50%; background: #ddd; display: flex; justify-content: center; align-items: center;">+</div>
<div>Custom Layout</div>
</div>
</div>
<div style="margin-top: 5px;">Custom Size</div>
</div>
<div>
<div style="background: #f0f0f0; height: 150px; display: flex; justify-content: center; align-items: center; border-radius: 8px;">
<div style="text-align: center;">
<div style="width: 40px; height: 40px; margin: 0 auto 10px; border-radius: 50%; background: #ddd; display: flex; justify-content: center; align-items: center;">+</div>
<div>Instagram</div>
</div>
</div>
<div style="margin-top: 5px;">Instagram Story (1080x1920)</div>
</div>
<div>
<div style="background: #f0f0f0; height: 150px; display: flex; justify-content: center; align-items: center; border-radius: 8px;">
<div style="text-align: center;">
<div style="width: 40px; height: 40px; margin: 0 auto 10px; border-radius: 50%; background: #ddd; display: flex; justify-content: center; align-items: center;">+</div>
<div>Twitter</div>
</div>
</div>
<div style="margin-top: 5px;">Twitter Post (1200x675)</div>
</div>
<div>
<div style="background: #f0f0f0; height: 150px; display: flex; justify-content: center; align-items: center; border-radius: 8px;">
<div style="text-align: center;">
<div style="width: 40px; height: 40px; margin: 0 auto 10px; border-radius: 50%; background: #ddd; display: flex; justify-content: center; align-items: center;">+</div>
<div>Tablet</div>
</div>
</div>
<div style="margin-top: 5px;">Tablet (2280x1600)</div>
</div>
</div>
</div>
</div>
</div>
""")
#iface.load(fn=get_config, outputs=campaign_details_input, js=js)
iface.launch(show_error=True)
|