Zhofang commited on
Commit
1cf85de
·
verified ·
1 Parent(s): 06c8ef6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -129
app.py CHANGED
@@ -6,142 +6,30 @@ import os
6
  from PIL import Image
7
  from deep_translator import GoogleTranslator
8
 
9
-
10
- import gradio as gr
11
-
12
- def welcome(name):
13
- return f"Welcome to Gradio, {name}!"
14
-
15
- js = """
16
- function createGradioAnimation() {
17
- var container = document.createElement('div');
18
- container.id = 'gradio-animation';
19
- container.style.fontSize = '2em';
20
- container.style.fontWeight = 'bold';
21
- container.style.textAlign = 'center';
22
- container.style.marginBottom = '20px';
23
-
24
- var text = 'Welcome to Gradio!';
25
- for (var i = 0; i < text.length; i++) {
26
- (function(i){
27
- setTimeout(function(){
28
- var letter = document.createElement('span');
29
- letter.style.opacity = '0';
30
- letter.style.transition = 'opacity 0.5s';
31
- letter.innerText = text[i];
32
-
33
- container.appendChild(letter);
34
-
35
- setTimeout(function() {
36
- letter.style.opacity = '1';
37
- }, 50);
38
- }, i * 250);
39
- })(i);
40
- }
41
-
42
- var gradioContainer = document.querySelector('.gradio-container');
43
- gradioContainer.insertBefore(container, gradioContainer.firstChild);
44
-
45
- return 'Animation created';
46
- }
47
- """
48
- with gr.Blocks(js=js) as demo:
49
- inp = gr.Textbox(placeholder="What is your name?")
50
- out = gr.Textbox()
51
- inp.change(welcome, inp, out)
52
-
53
- demo.launch()
54
-
55
- html='''<style>
56
- body, html, #app {
57
- margin: 0;
58
- width: 100%;
59
- height: 100%;
60
- }
61
-
62
- #app {
63
- overflow: hidden;
64
- touch-action: pan-up;
65
- color: #ffffff;
66
- font-family: 'Montserrat', sans-serif;
67
- text-align: center;
68
- text-shadow: 0 0 5px #000000, 0 0 20px #000;
69
- user-select: none;
70
- }
71
-
72
- #app h1 {
73
- --fontSize: 50px;
74
- --lineHeight: 70px;
75
- width: auto;
76
- height: calc(2 * var(--lineHeight));
77
- line-height: var(--lineHeight);
78
- margin: calc(50vh - var(--lineHeight)) auto 0;
79
- font-size: var(--fontSize);
80
- }
81
-
82
- #app a {
83
- margin-top: 10px;
84
- display: inline-block;
85
- text-decoration: none;
86
- color: #fff;
87
- }
88
-
89
- #app canvas {
90
- display: block;
91
- position: fixed;
92
- z-index: -1;
93
- top: 0;
94
- }
95
- </style>
96
-
97
- <script>
98
- import { swarmBackground } from 'https://unpkg.com/[email protected]/build/threejs-toys.module.cdn.min.js'
99
-
100
- const bg = swarmBackground({
101
- el: document.getElementById('app'),
102
- eventsEl: document.body,
103
- gpgpuSize: 256,
104
- color: [Math.random() * 0xffffff, Math.random() * 0xffffff],
105
- geometry: 'default'
106
- })
107
-
108
- bg.three.camera.position.set(0, 0, 200)
109
-
110
- document.body.addEventListener('click', () => {
111
- bg.setColors([Math.random() * 0xffffff, Math.random() * 0xffffff])
112
- })
113
- </script>
114
- <div id="app">
115
- <div id="hero">
116
- <h1>SWARM<br/>BACKGROUND</h1>
117
- <a target="_blank" href="https://github.com/klevron/threejs-toys">github/threejs-toys</a>
118
- </div>
119
- </div>
120
- '''
121
- # os.makedirs('assets', exist_ok=True)
122
  if not os.path.exists('icon.jpg'):
123
  os.system("wget -O icon.jpg https://i.pinimg.com/564x/64/49/88/644988c59447eb00286834c2e70fdd6b.jpg")
124
- API_URL_DEV = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
125
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
126
  timeout = 100
127
 
128
- def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, huggingface_api_key=None, use_dev=False):
129
- # Determine which API URL to use
130
- api_url = API_URL_DEV if use_dev else API_URL
131
-
132
  # Check if the request is an API call by checking for the presence of the huggingface_api_key
133
  is_api_call = huggingface_api_key is not None
134
 
135
  if is_api_call:
 
136
  # Use the environment variable for the API key in GUI mode
137
  API_TOKEN = os.getenv("HF_READ_TOKEN")
138
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
 
139
  else:
140
  # Validate the API key if it's an API call
141
  if huggingface_api_key == "":
142
  raise gr.Error("API key is required for API calls.")
 
143
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
144
-
145
  if prompt == "" or prompt is None:
146
  return None
147
 
@@ -166,7 +54,7 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
166
  "strength": strength
167
  }
168
 
169
- response = requests.post(api_url, headers=headers, json=payload, timeout=timeout)
170
  if response.status_code != 200:
171
  print(f"Error: Failed to get image. Response status: {response.status_code}")
172
  print(f"Response content: {response.text}")
@@ -199,21 +87,19 @@ css = """
199
  align-items: center;
200
  justify-content: center;
201
  }
 
202
  #title-icon {
203
  width: 32px; /* Adjust the width of the icon as needed */
204
  height: auto;
205
  margin-right: 10px; /* Space between icon and title */
206
  }
 
207
  #title-text {
208
  font-size: 24px; /* Adjust font size as needed */
209
  font-weight: bold;
210
  }
211
  """
212
- with gr.Blocks(js=js) as demo:
213
- inp = gr.Textbox(placeholder="What is your name?")
214
- out = gr.Textbox()
215
- inp.change(welcome, inp, out)
216
-
217
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
218
  gr.HTML("""
219
  <center>
@@ -222,7 +108,7 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
222
  <h1 id="title-text">FLUX Capacitor</h1>
223
  </div>
224
  </center>
225
- """+html)
226
 
227
  with gr.Column(elem_id="app-container"):
228
  with gr.Row():
@@ -238,15 +124,16 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
238
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
239
  seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
240
  huggingface_api_key = gr.Textbox(label="Hugging Face API Key (required for API calls)", placeholder="Enter your Hugging Face API Key here", type="password", elem_id="api-key")
241
- use_dev = gr.Checkbox(label="Use Dev API", value=False, elem_id="use-dev-checkbox")
242
 
243
  with gr.Row():
244
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
245
  with gr.Row():
 
 
246
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
247
  seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
248
 
249
- # Adjust the click function to include the API key and use_dev as inputs
250
- text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key, use_dev], outputs=[image_output, seed_output])
251
 
252
  app.launch(show_api=True, share=False)
 
6
  from PIL import Image
7
  from deep_translator import GoogleTranslator
8
 
9
+ #os.makedirs('assets', exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  if not os.path.exists('icon.jpg'):
11
  os.system("wget -O icon.jpg https://i.pinimg.com/564x/64/49/88/644988c59447eb00286834c2e70fdd6b.jpg")
12
+ API_URL_DEV ="https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
13
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
14
  timeout = 100
15
 
16
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, huggingface_api_key=None):
 
 
 
17
  # Check if the request is an API call by checking for the presence of the huggingface_api_key
18
  is_api_call = huggingface_api_key is not None
19
 
20
  if is_api_call:
21
+
22
  # Use the environment variable for the API key in GUI mode
23
  API_TOKEN = os.getenv("HF_READ_TOKEN")
24
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
25
+
26
  else:
27
  # Validate the API key if it's an API call
28
  if huggingface_api_key == "":
29
  raise gr.Error("API key is required for API calls.")
30
+
31
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
32
+
33
  if prompt == "" or prompt is None:
34
  return None
35
 
 
54
  "strength": strength
55
  }
56
 
57
+ response = requests.post(API_URL_DEV, headers=headers, json=payload, timeout=timeout)
58
  if response.status_code != 200:
59
  print(f"Error: Failed to get image. Response status: {response.status_code}")
60
  print(f"Response content: {response.text}")
 
87
  align-items: center;
88
  justify-content: center;
89
  }
90
+
91
  #title-icon {
92
  width: 32px; /* Adjust the width of the icon as needed */
93
  height: auto;
94
  margin-right: 10px; /* Space between icon and title */
95
  }
96
+
97
  #title-text {
98
  font-size: 24px; /* Adjust font size as needed */
99
  font-weight: bold;
100
  }
101
  """
102
+
 
 
 
 
103
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
104
  gr.HTML("""
105
  <center>
 
108
  <h1 id="title-text">FLUX Capacitor</h1>
109
  </div>
110
  </center>
111
+ """)
112
 
113
  with gr.Column(elem_id="app-container"):
114
  with gr.Row():
 
124
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
125
  seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
126
  huggingface_api_key = gr.Textbox(label="Hugging Face API Key (required for API calls)", placeholder="Enter your Hugging Face API Key here", type="password", elem_id="api-key")
 
127
 
128
  with gr.Row():
129
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
130
  with gr.Row():
131
+ # Define two outputs: one for the image file path and one for the seed
132
+ #image_path_output = gr.Textbox(label="Image File Path", elem_id="gallery")
133
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
134
  seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
135
 
136
+ # Adjust the click function to include the API key as an input
137
+ text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key], outputs=[image_output, seed_output])
138
 
139
  app.launch(show_api=True, share=False)