File size: 15,215 Bytes
753e5f6
195ffb7
753e5f6
195ffb7
c438cdb
 
753e5f6
0c4acd2
195ffb7
0c4acd2
195ffb7
c6f24f8
 
753e5f6
0c4acd2
195ffb7
753e5f6
f66af7a
0c4acd2
 
753e5f6
 
195ffb7
3f5b005
 
 
 
 
753e5f6
195ffb7
0c4acd2
 
 
 
753e5f6
0c4acd2
753e5f6
1ae4512
195ffb7
1ae4512
195ffb7
0c4acd2
 
 
 
 
 
 
 
 
 
753e5f6
0c4acd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753e5f6
 
42e93b3
 
643adaf
ff30aae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
643adaf
 
ff30aae
 
 
 
 
 
 
 
643adaf
 
ff30aae
 
 
 
 
 
643adaf
 
ff30aae
 
 
 
643adaf
ff30aae
643adaf
 
ff30aae
 
 
 
643adaf
ff30aae
643adaf
 
ff30aae
 
 
 
643adaf
ff30aae
643adaf
 
ff30aae
 
 
 
643adaf
ff30aae
643adaf
 
ff30aae
 
 
 
643adaf
ff30aae
643adaf
 
42e93b3
ff30aae
643adaf
42e93b3
 
 
f66af7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3630a7f
f66af7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2c1565
 
 
 
1ae4512
195ffb7
f66af7a
 
 
 
103355c
 
 
 
f66af7a
 
195ffb7
3a649e5
 
 
e46071a
3a649e5
 
e46071a
3a649e5
 
e46071a
3a649e5
0c4acd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff30aae
 
 
 
 
 
3a649e5
 
c438cdb
 
4196d7f
ec93fba
 
4196d7f
cc56263
c438cdb
cc56263
c438cdb
cc56263
 
 
 
 
09d8509
cc56263
 
 
c438cdb
cc56263
 
c438cdb
195ffb7
ff30aae
f91e4c1
 
 
 
 
 
 
 
 
 
 
ff30aae
 
f91e4c1
ff30aae
f91e4c1
ff30aae
f91e4c1
 
 
 
 
 
0c4acd2
 
 
 
 
ff30aae
0c4acd2
 
 
 
 
ff30aae
0c4acd2
 
ff30aae
0c4acd2
ff30aae
0c4acd2
 
 
 
 
ff30aae
0c4acd2
 
 
 
 
 
 
f91e4c1
0c4acd2
f91e4c1
 
0c4acd2
 
c438cdb
643adaf
5192904
 
c438cdb
ff30aae
195ffb7
 
c6378ba
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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://127.0.0.1:8021")
APP_ENV = os.environ.get("APP_ENV", None)
STRIPE_PAYMENT_LINK = os.environ.get("STRIPE_PAYMENT_LINK", "https://buy.stripe.com/00gbJE8St1EL5OM6oA")



# Assuming the API returns an image URL in the response
def generate_image(campaign_details, license_key=None):
    # Define your payload/data to send to the image generation API
    data = {
        "request_string": campaign_details,
        "is_single_image": False,
        "license_key": license_key
    }

    # 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 == 403:
        error_msg = response.json().get("detail", "License key error")
        return None, error_msg
    elif response.status_code != 200:
        print(f"Error {response.status_code}: {response.text}")
        return None, "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']
    
    # Get usage information if available
    usage_info = ""
    if "remaining_uses" in image_data:
        usage_info = f"Remaining uses with this license key: {image_data['remaining_uses']}"
    elif "free_uses_left" in image_data:
        usage_info = f"Free uses remaining: {image_data['free_uses_left']}/5. Get a license key for more!"
        
    return data_url, usage_info


# Validate license key against the API
def validate_license_key(license_key):
    if not license_key or license_key.strip() == "":
        return "Please enter a license key (your email) or purchase one."
        
    response = requests.post(
        API_URL + "/validate-license-key",
        params={"license_key": license_key},
        timeout=10
    )
    
    if response.status_code != 200:
        return "Error validating license key. Please try again."
        
    data = response.json()
    if data["is_valid"]:
        return f"Valid license key. Remaining uses: {data['remaining_uses']}"
    else:
        return "Invalid license key. Please purchase one or try again."


content_html = """
<div style="text-align: center; margin-top: 30px;">
    <h2 style="font-size: 24px; margin-bottom: 20px;">Slidegen Examples</h2>
    
    <!-- Wrapper with responsive styles -->
    <style>
        @media (max-width: 768px) {
            .slidegen-container {
                flex-direction: column-reverse !important;
            }
            .video-container {
                margin-right: 0 !important;
                margin-top: 25px !important;
                width: 100% !important;
                max-width: 300px !important;
                margin-left: auto !important;
                margin-right: auto !important;
            }
            .carousel-container {
                width: 100% !important;
            }
        }
    </style>
    
    <div style="margin: 20px auto; max-width: 95%; position: relative; display: flex; align-items: flex-start;" class="slidegen-container">
        <!-- Stationary Tutorial Video on the left (on desktop) or bottom (on mobile) -->
        <div style="flex: 0 0 180px; margin-right: 25px;" class="video-container">
            <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                <video controls controlsList="nodownload" playsinline style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                    <source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4">
                    Your browser does not support the video tag.
                </video>
            </div>
            <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Tutorial Video</p>
        </div>
        
        <!-- Scrollable carousel container for other items -->
        <div style="flex: 1; overflow-x: auto; white-space: nowrap; padding: 0; scrollbar-width: thin; scrollbar-color: #4a5568 #1a2438;" class="carousel-container">
            <!-- Screenshots (1280x800) -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 280px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://res.cloudinary.com/dol2onsxl/image/upload/v1745556423/Slidegen/bgpt-faveimage_20241230061609_nbrjb5.png" alt="Screenshot Layout" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Screenshots (1280x800)</p>
            </div>
            
            <!-- Square Layout -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 220px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" alt="Square Layout" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Square Layout</p>
            </div>
            
            <!-- Custom Size -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 280px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://res.cloudinary.com/dol2onsxl/image/upload/v1745556411/Slidegen/exploring-host-cities_lp6dy8.png" alt="Custom Layout" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Custom Size</p>
            </div>
            
            <!-- Mobile Landscape -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 280px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" alt="Mobile Landscape" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Mobile Landscape</p>
            </div>
            
            <!-- Twitter Post -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 220px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://res.cloudinary.com/dol2onsxl/image/upload/v1745556418/Slidegen/browsergpt-cartoon-ad_xodysb.png" alt="Twitter Layout" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Twitter Post (1200x675)</p>
            </div>
            
            <!-- Landscape Article Image -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 280px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://res.cloudinary.com/dol2onsxl/image/upload/v1745558123/Slidegen/article_image_1745215220129_32yqnq_ldeyft.png" alt="Landscape Layout" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Landscape Article Image</p>
            </div>
            
            <!-- Landscape Layout -->
            <div style="display: inline-block; vertical-align: top; margin-right: 25px; width: 280px;">
                <div style="height: 220px; display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
                    <img src="https://res.cloudinary.com/dol2onsxl/image/upload/v1745558119/Slidegen/1692111223459-153ucd_hibeys.png" alt="Landscape Layout" style="max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                </div>
                <p style="margin-top: 5px; font-size: 14px; color: #666; text-align: center;">Landscape Layout</p>
            </div>
        </div>
    </div>
    <p style="margin-top: 15px; font-size: 14px; color: #888; text-align: center;">← Scroll horizontally to see more layouts β†’</p>
    <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), 
iPhone 6.9" (1290x2796), iPhone 6.5" (1284x2778)


---

## Custom Graphics
Enter your custom graphic prompt here.

## Custom Graphics Image Layout
Feature Image Background Color (Optional): i.e #008000

"""


# Custom CSS for dark background
css = """
body {
    background-color: #080e1a !important;
}
.dark {
    background-color: #080e1a !important;
}
.gradio-container {
    background-color: #080e1a !important;
}

/* Simple license key section styling */
.license-section {
    margin-top: 24px;
    margin-bottom: 40px;
    text-align: center;
}

.center-text {
    text-align: center;
    margin-bottom: 0px;
}

.license-bottom {
    margin-bottom: 5px;
}

.license-button-row {
    margin-top: 16px;
    margin-bottom: 16px;
}

.license-status {
    margin-top: 16px;
    margin-bottom: 3px;
}

/* Custom button width */
.license-button-row button {
    width: 40% !important;
    margin: 0 auto !important;
}
"""


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(css=css, title="Slidegen AI - Image generator") as iface:
    gr.Markdown("# Slidegen AI - Image generator")
    gr.Markdown("Generate social media creatives from a few prompts")

    with gr.Row():
        with gr.Column(scale=1):
            campaign_details_input = gr.Textbox(
                lines=10,
                placeholder="Enter your Brand and campaign details here",
                label="Campaign Details",
                value=default_value
            )
            
            # This JS approach doesn't work in Gradio 3.x, so we're using a simpler approach
            campaign_details_input.change(
                fn=lambda x: x,  # Just a simple identity function
                inputs=campaign_details_input,
                outputs=campaign_details_input
            )
            
            submit_btn = gr.Button("Generate Image")
            
        with gr.Column(scale=1):
            output_image = gr.Image(label="Generated Image")
            usage_info = gr.Markdown("Usage info will appear here after generation.")
            
            # Center-aligned license key section with bottom margin
            with gr.Group(elem_classes=["license-section"]):
                license_key_input = gr.Textbox(
                    placeholder="Enter your license key",
                    label="License Key (optional for first 5 images)"
                )
                
                # Center the button using Row and Columns
                with gr.Row(elem_classes=["license-button-row"]):
                    with gr.Column(scale=3):
                        # Empty column for spacing
                        pass
                    with gr.Column(scale=4):
                        validate_btn = gr.Button("Validate")
                    with gr.Column(scale=3):
                        # Empty column for spacing
                        pass
                
                license_key_status = gr.Markdown("You can generate 5 free images without a license key.", elem_classes=["license-status", "center-text"])
                gr.Markdown(f"Need more images? [Purchase a license key]({STRIPE_PAYMENT_LINK}) for 50 images.", elem_classes=["center-text", "license-bottom"])
                gr.Markdown("Each image generation uses 1 credit. Free tier gives 5 credits, license key provides 100 credits.", elem_classes=["center-text", "license-bottom"])
    
    # Set up the validation button click event
    validate_btn.click(
        fn=validate_license_key,
        inputs=license_key_input,
        outputs=license_key_status
    )
    
    # Update the submit button click event to include the license key
    submit_btn.click(
        fn=generate_image, 
        inputs=[campaign_details_input, license_key_input], 
        outputs=[output_image, usage_info]
    )
    
    # Add examples section
    gr.HTML(content_html)

    #iface.load(fn=get_config, outputs=campaign_details_input)


iface.launch(show_error=True)