Nifemi Alpine Durin commited on
Commit
195ffb7
·
1 Parent(s): c438cdb
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -1,28 +1,37 @@
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,14 +40,17 @@ content_html = """
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,13 +95,14 @@ Selected Layout: Mobile Portrait (750x1334)
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';
@@ -105,6 +118,7 @@ def get_config(request: Request):
105
  return config['campaign_details']
106
 
107
 
 
108
  with gr.Blocks() as iface:
109
 
110
  campaign_details_input = gr.Textbox(
@@ -115,23 +129,28 @@ with gr.Blocks() as iface:
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()
 
 
1
  import gradio as gr
2
+ import requests, base64, io
 
3
  from dotenv import load_dotenv
4
+ import os, datetime, mimetypes
5
  from gradio import Request
6
 
7
  load_dotenv()
8
  API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021")
9
+ APP_ENV = os.environ.get("APP_ENV", None)
10
+
11
 
12
 
13
  # Assuming the API returns an image URL in the response
14
  def generate_image(campaign_details):
15
+
16
+
17
+ # Define your payload/data to send to the image generation API
18
  data = {
19
  "request_string": campaign_details,
20
  "is_single_image": False
21
  }
22
 
23
+ # Make the API call
24
  response = requests.post(API_URL + "/generate_graphic", json=data)
25
 
26
+ # Ensure the API call was successful
27
  if response.status_code != 200:
28
  print(f"Error {response.status_code}: {response.text}")
29
  return "Error: Unable to fetch image from the external API."
30
 
31
  image_data = response.json()
32
+
33
  data_url = image_data['image_url']
34
+ image_desc = image_data['image_description']
35
 
36
  return data_url
37
 
 
40
  <div style="text-align: center; margin-top: 30px;">
41
  <h2>Slidegen Examples</h2>
42
  <div style="display: flex; justify-content: space-around; align-items: center;">
43
+ <!-- Displaying one video -->
44
  <video controls width="30%">
45
  <source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4">
46
  Your browser does not support the video tag.
47
  </video>
48
+ <!-- Displaying two images -->
49
  <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" alt="Image Description 1" style="width: 30%; margin: 0 10px;">
50
  <img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" alt="Image Description 2" style="width: 30%; margin: 0 10px;">
51
  </div>
52
  </div>
53
+
54
  """
55
 
56
  default_value = """
 
95
 
96
  **Examples of layouts: Mobile Portrait (750x1334), Mobile Landscape (940x470), Square (2048x2048)
97
 
98
+
99
  ---
100
 
101
  ## Custom Graphics
102
  Enter your custom graphic prompt here.
103
  """
104
 
105
+
106
  js = '''function js(){
107
  window.set_cookie = function(key, value){
108
  document.cookie = key+'='+value+'; Path=/; SameSite=Strict';
 
118
  return config['campaign_details']
119
 
120
 
121
+
122
  with gr.Blocks() as iface:
123
 
124
  campaign_details_input = gr.Textbox(
 
129
  elem_id="campaign_details_textarea"
130
  )
131
 
 
 
132
  campaign_details_input.change(
133
+ fn=lambda x: x,
134
+ inputs=campaign_details_input,
135
+ outputs=[],
136
+ js="(value) => set_cookie('campaign_details', value)"
137
+ )
138
 
139
  gr.Interface(
140
  fn=generate_image,
141
  inputs=campaign_details_input,
142
+ outputs=[
143
+ gr.components.Image(label="Generated Image")
144
+ ],
145
  title="Slidegen AI - Image generator",
146
  article=content_html,
147
  description="Generate social media creatives from a few prompts",
148
  live=False
149
  )
150
 
151
+ iface.load(fn=get_config, inputs=None, outputs=campaign_details_input, js=js)
152
+
153
+
154
+
155
  iface.launch()
156
+