Nifemi Alpine Durin commited on
Commit
c438cdb
·
1 Parent(s): 1ae4512

add persistence

Browse files
Files changed (1) hide show
  1. app.py +49 -34
app.py CHANGED
@@ -1,36 +1,28 @@
1
  import gradio as gr
2
- import requests, base64, io
 
3
  from dotenv import load_dotenv
4
- import os, datetime, mimetypes
5
- from PIL import Image
6
  load_dotenv()
7
  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
 
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": False
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:
27
  print(f"Error {response.status_code}: {response.text}")
28
  return "Error: Unable to fetch image from the external API."
29
 
30
  image_data = response.json()
31
-
32
  data_url = image_data['image_url']
33
- image_desc = image_data['image_description']
34
 
35
  return data_url
36
 
@@ -39,17 +31,14 @@ content_html = """
39
  <div style="text-align: center; margin-top: 30px;">
40
  <h2>Slidegen Examples</h2>
41
  <div style="display: flex; justify-content: space-around; align-items: center;">
42
- <!-- Displaying one video -->
43
  <video controls width="30%">
44
  <source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4">
45
  Your browser does not support the video tag.
46
  </video>
47
- <!-- Displaying two images -->
48
  <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" alt="Image Description 1" style="width: 30%; margin: 0 10px;">
49
  <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" alt="Image Description 2" style="width: 30%; margin: 0 10px;">
50
  </div>
51
  </div>
52
-
53
  """
54
 
55
  default_value = """
@@ -94,29 +83,55 @@ Selected Layout: Mobile Portrait (750x1334)
94
 
95
  **Examples of layouts: Mobile Portrait (750x1334), Mobile Landscape (940x470), Square (2048x2048)
96
 
97
-
98
  ---
99
 
100
  ## Custom Graphics
101
  Enter your custom graphic prompt here.
102
  """
103
 
104
- iface = gr.Interface(
105
- fn=generate_image,
106
- inputs=[
107
- gr.components.Textbox(lines=10, placeholder="Enter your Brand and campaign details here", value=default_value, label="Campaign Details")
108
- ],
109
- outputs=[
110
- gr.components.Image(label="Generated Image")
111
- ],
112
- title="Slidegen AI - Image generator",
113
- article=content_html,
114
- description="Generate social media creatives from a few prompts",
115
- live=False
116
- )
117
-
118
- # iface.queue()
119
- # Run the interface
120
- #trigger
121
- iface.launch()
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
  from dotenv import load_dotenv
5
+ from gradio import Request
6
+
7
  load_dotenv()
8
  API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021")
 
 
9
 
10
 
11
  # Assuming the API returns an image URL in the response
12
  def generate_image(campaign_details):
 
 
 
13
  data = {
14
  "request_string": campaign_details,
15
  "is_single_image": False
16
  }
17
 
 
18
  response = requests.post(API_URL + "/generate_graphic", json=data)
19
 
 
20
  if response.status_code != 200:
21
  print(f"Error {response.status_code}: {response.text}")
22
  return "Error: Unable to fetch image from the external API."
23
 
24
  image_data = response.json()
 
25
  data_url = image_data['image_url']
 
26
 
27
  return data_url
28
 
 
31
  <div style="text-align: center; margin-top: 30px;">
32
  <h2>Slidegen Examples</h2>
33
  <div style="display: flex; justify-content: space-around; align-items: center;">
 
34
  <video controls width="30%">
35
  <source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4">
36
  Your browser does not support the video tag.
37
  </video>
 
38
  <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" alt="Image Description 1" style="width: 30%; margin: 0 10px;">
39
  <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" alt="Image Description 2" style="width: 30%; margin: 0 10px;">
40
  </div>
41
  </div>
 
42
  """
43
 
44
  default_value = """
 
83
 
84
  **Examples of layouts: Mobile Portrait (750x1334), Mobile Landscape (940x470), Square (2048x2048)
85
 
 
86
  ---
87
 
88
  ## Custom Graphics
89
  Enter your custom graphic prompt here.
90
  """
91
 
92
+ # JavaScript to save and load textarea content using cookies
93
+ js = '''function js(){
94
+ window.set_cookie = function(key, value){
95
+ document.cookie = key+'='+value+'; Path=/; SameSite=Strict';
96
+ return [value]
97
+ }
98
+ }'''
 
 
 
 
 
 
 
 
 
 
 
99
 
100
+
101
+ def get_config(request: Request):
102
+ config = {"campaign_details": default_value}
103
+ if 'campaign_details' in request.cookies:
104
+ config['campaign_details'] = request.cookies['campaign_details']
105
+ return config['campaign_details']
106
+
107
+
108
+ with gr.Blocks() as iface:
109
+
110
+ campaign_details_input = gr.Textbox(
111
+ lines=10,
112
+ placeholder="Enter your Brand and campaign details here",
113
+ value=default_value,
114
+ label="Campaign Details",
115
+ elem_id="campaign_details_textarea"
116
+ )
117
+
118
+ iface.load(fn=get_config, inputs=None, outputs=campaign_details_input, js=js)
119
+
120
+ campaign_details_input.change(
121
+ fn=lambda x: x,
122
+ inputs=campaign_details_input,
123
+ outputs=[],
124
+ js="(value) => set_cookie('campaign_details', value)"
125
+ )
126
+
127
+ gr.Interface(
128
+ fn=generate_image,
129
+ inputs=campaign_details_input,
130
+ outputs=gr.Image(label="Generated Image"),
131
+ title="Slidegen AI - Image generator",
132
+ article=content_html,
133
+ description="Generate social media creatives from a few prompts",
134
+ live=False
135
+ )
136
+
137
+ iface.launch()