HelloAI1 commited on
Commit
2eaf1c2
1 Parent(s): a98a7d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -58
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import numpy as np
2
  import gradio as gr
3
  import requests
4
  import time
@@ -48,7 +47,7 @@ class Prodia:
48
  response = requests.post(url, headers=headers, data=json.dumps(params))
49
 
50
  if response.status_code != 200:
51
- raise Exception(f"Bad Prodia Response: {response.status_code} - {response.text}")
52
 
53
  return response
54
 
@@ -56,46 +55,42 @@ class Prodia:
56
  response = requests.get(url, headers=self.headers)
57
 
58
  if response.status_code != 200:
59
- raise Exception(f"Bad Prodia Response: {response.status_code} - {response.text}")
60
 
61
  return response
62
 
 
63
  def image_to_base64(image_path):
 
64
  with Image.open(image_path) as image:
 
65
  buffered = BytesIO()
66
- image.save(buffered, format="PNG")
 
 
67
  img_str = base64.b64encode(buffered.getvalue())
68
- return img_str.decode('utf-8')
69
 
70
- api_key = os.getenv("PRODIA_API_KEY")
71
- if not api_key:
72
- raise ValueError("Prodia API key not found in environment variables")
73
 
74
- prodia_client = Prodia(api_key=api_key)
75
 
76
  def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
77
- try:
78
- result = prodia_client.generate({
79
- "prompt": prompt,
80
- "negative_prompt": negative_prompt,
81
- "model": model,
82
- "steps": steps,
83
- "sampler": sampler,
84
- "cfg_scale": cfg_scale,
85
- "width": width,
86
- "height": height,
87
- "seed": seed
88
- })
89
-
90
- job = prodia_client.wait(result)
91
-
92
- if job['status'] == 'succeeded':
93
- return job["imageUrl"]
94
- else:
95
- return "Generation failed, please try again."
96
-
97
- except Exception as e:
98
- return f"An error occurred: {e}"
99
 
100
  css = """
101
  /* Overall Styling */
@@ -136,7 +131,7 @@ body {
136
 
137
  /* Button Styling */
138
  #generate {
139
- background-color: #007bff;
140
  color: white;
141
  padding: 15px 25px;
142
  border: none;
@@ -145,10 +140,10 @@ body {
145
  }
146
 
147
  #generate:hover {
148
- background-color: #0056b3;
149
  }
150
 
151
- /* Responsive Design */
152
  @media screen and (max-width: 768px) {
153
  #settings {
154
  grid-template-columns: 1fr;
@@ -156,14 +151,13 @@ body {
156
  }
157
  """
158
 
159
- with gr.Blocks(css=css) as demo:
160
- state = gr.InterfaceState(value="Welcome Screen")
161
 
162
- def update_visibility(tab_state):
163
- return tab_state == "Main Generation Screen"
164
 
165
  with gr.Tabs() as tabs:
166
- with gr.Tab("Welcome Screen"):
167
  with gr.Row():
168
  logo = gr.Image(
169
  value="http://disneypixaraigenerator.com/wp-content/uploads/2023/12/cropped-android-chrome-512x512-1.png",
@@ -173,34 +167,65 @@ with gr.Blocks(css=css) as demo:
173
  )
174
 
175
  with gr.Row():
176
- title = gr.Textbox("<h1 style='text-align: center;'>Disney Pixar AI Generator</h1>", elem_id="title")
177
 
178
  with gr.Row():
179
  start_button = gr.Button("Get Started", variant='primary', elem_id="start-button")
180
 
181
- with gr.Tab("Main Generation Screen"):
182
  with gr.Row():
183
- gr.Textbox("<h1 style='text-align: center;'>Create Your Disney Pixar AI Poster</h1>", elem_id="title")
184
 
185
- with gr.Row():
186
  image_output = gr.Image(
187
  value="https://cdn-uploads.huggingface.co/production/uploads/noauth/XWJyh9DhMGXrzyRJk7SfP.png",
188
  label="Generated Image",
189
  elem_id="image-output"
190
  )
191
 
192
- with gr.Row():
193
- prompt = gr.Textbox(
194
- "space warrior, beautiful, female, ultrarealistic, soft lighting, 8k",
195
- placeholder="Enter your prompt here...",
196
- show_label=False,
197
- lines=3,
198
- elem_id="prompt-input"
199
- )
200
- negative_prompt = gr.Textbox(
201
- placeholder="Enter negative prompts (optional)...",
202
- show_label=False,
203
- lines=3,
204
- value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly"
205
- )
206
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
  import time
 
47
  response = requests.post(url, headers=headers, data=json.dumps(params))
48
 
49
  if response.status_code != 200:
50
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
51
 
52
  return response
53
 
 
55
  response = requests.get(url, headers=self.headers)
56
 
57
  if response.status_code != 200:
58
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
59
 
60
  return response
61
 
62
+
63
  def image_to_base64(image_path):
64
+ # Open the image with PIL
65
  with Image.open(image_path) as image:
66
+ # Convert the image to bytes
67
  buffered = BytesIO()
68
+ image.save(buffered, format="PNG") # You can change format to PNG if needed
69
+
70
+ # Encode the bytes to base64
71
  img_str = base64.b64encode(buffered.getvalue())
 
72
 
73
+ return img_str.decode('utf-8') # Convert bytes to string
74
+
 
75
 
76
+ prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
77
 
78
  def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
79
+ result = prodia_client.generate({
80
+ "prompt": prompt,
81
+ "negative_prompt": negative_prompt,
82
+ "model": model,
83
+ "steps": steps,
84
+ "sampler": sampler,
85
+ "cfg_scale": cfg_scale,
86
+ "width": width,
87
+ "height": height,
88
+ "seed": seed
89
+ })
90
+
91
+ job = prodia_client.wait(result)
92
+
93
+ return job["imageUrl"]
 
 
 
 
 
 
 
94
 
95
  css = """
96
  /* Overall Styling */
 
131
 
132
  /* Button Styling */
133
  #generate {
134
+ background-color: #007bff; /* Example - use your preferred color */
135
  color: white;
136
  padding: 15px 25px;
137
  border: none;
 
140
  }
141
 
142
  #generate:hover {
143
+ background-color: #0056b3; /* Darker shade on hover */
144
  }
145
 
146
+ /* Responsive Design - Adjust breakpoints as needed */
147
  @media screen and (max-width: 768px) {
148
  #settings {
149
  grid-template-columns: 1fr;
 
151
  }
152
  """
153
 
154
+ # --- Gradio Interface ---
 
155
 
156
+ with gr.Blocks(css=css) as demo:
157
+ state = gr.State(value="Welcome Screen") # To control the visibility of tabs
158
 
159
  with gr.Tabs() as tabs:
160
+ with gr.TabItem("Welcome Screen"):
161
  with gr.Row():
162
  logo = gr.Image(
163
  value="http://disneypixaraigenerator.com/wp-content/uploads/2023/12/cropped-android-chrome-512x512-1.png",
 
167
  )
168
 
169
  with gr.Row():
170
+ title = gr.Markdown("<h1 style='text-align: center;'>Disney Pixar AI Generator</h1>", elem_id="title")
171
 
172
  with gr.Row():
173
  start_button = gr.Button("Get Started", variant='primary', elem_id="start-button")
174
 
175
+ with gr.TabItem("Main Generation Screen"):
176
  with gr.Row():
177
+ gr.Markdown("<h1 style='text-align: center;'>Create Your Disney Pixar AI Poster</h1>", elem_id="title")
178
 
179
+ with gr.Row(elem_id="image-output-container"):
180
  image_output = gr.Image(
181
  value="https://cdn-uploads.huggingface.co/production/uploads/noauth/XWJyh9DhMGXrzyRJk7SfP.png",
182
  label="Generated Image",
183
  elem_id="image-output"
184
  )
185
 
186
+ with gr.Row(elem_id="settings"):
187
+ with gr.Column(scale=1, min_width=300, elem_classes="setting-group"):
188
+ prompt = gr.Textbox(
189
+ "space warrior, beautiful, female, ultrarealistic, soft lighting, 8k",
190
+ placeholder="Enter your prompt here...",
191
+ show_label=False,
192
+ lines=3,
193
+ elem_id="prompt-input"
194
+ )
195
+ negative_prompt = gr.Textbox(
196
+ placeholder="Enter negative prompts (optional)...",
197
+ show_label=False,
198
+ lines=3,
199
+ value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly"
200
+ )
201
+ text_button = gr.Button("Generate", variant='primary', elem_id="generate")
202
+
203
+ with gr.Column(scale=1, min_width=300, elem_classes="setting-group"):
204
+ model = gr.Dropdown(
205
+ interactive=True,
206
+ value="sd_xl_base_1.0.safetensors [be9edd61]",
207
+ show_label=True,
208
+ label="Model",
209
+ choices=prodia_client.list_models()
210
+ )
211
+ sampler = gr.Dropdown(
212
+ value="DPM++ 2M Karras",
213
+ show_label=True,
214
+ label="Sampling Method",
215
+ choices=prodia_client.list_samplers()
216
+ )
217
+ steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=25, value=20, step=1)
218
+
219
+ with gr.Column(scale=1, min_width=300, elem_classes="setting-group"):
220
+ width = gr.Slider(label="Width", minimum=512, maximum=1536, value=1024, step=8)
221
+ height = gr.Slider(label="Height", minimum=512, maximum=1536, value=1024, step=8)
222
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
223
+ seed = gr.Number(label="Seed", value=-1)
224
+
225
+ text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
226
+ start_button.click(fn=lambda: "Main Generation Screen", inputs=None, outputs=state)
227
+ state.change(fn=lambda x: gr.update(visible=(x == "Main Generation Screen")), inputs=state, outputs=tabs)
228
+
229
+ # Launch the Gradio app
230
+ demo.launch(max_threads=128)
231
+