salomonsky commited on
Commit
0cfb4a5
1 Parent(s): ac36fcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -112
app.py CHANGED
@@ -1,16 +1,18 @@
1
- import os
2
  import gradio as gr
 
 
 
 
3
  import numpy as np
4
  import random
5
- from huggingface_hub import AsyncInferenceClient
6
  from translatepy import Translator
7
  import requests
8
  import re
9
  import asyncio
10
- from PIL import Image
 
11
 
12
  translator = Translator()
13
- HF_TOKEN = os.environ.get("HF_TOKEN", None)
14
  basemodel = "black-forest-labs/FLUX.1-dev"
15
  MAX_SEED = np.iinfo(np.int32).max
16
 
@@ -33,40 +35,12 @@ def enable_lora(lora_add):
33
  else:
34
  return lora_add
35
 
36
- client = AsyncInferenceClient()
37
-
38
- async def generate_image(
39
- prompt:str,
40
- model:str,
41
- lora_word:str,
42
- width:int=768,
43
- height:int=1024,
44
- scales:float=3.5,
45
- steps:int=24,
46
- seed:int=-1):
47
-
48
- if seed == -1:
49
- seed = random.randint(0, MAX_SEED)
50
- seed = int(seed)
51
- print(f'prompt:{prompt}')
52
-
53
- text = str(translator.translate(prompt, 'English')) + "," + lora_word
54
-
55
- try:
56
- image = await client.text_to_image(
57
- prompt=text,
58
- height=height,
59
- width=width,
60
- guidance_scale=scales,
61
- num_inference_steps=steps,
62
- model=model,
63
- )
64
- except Exception as e:
65
- raise gr.Error(f"Error in {e}")
66
-
67
- return image, seed
68
 
69
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
 
 
70
  client = Client("finegrain/finegrain-image-enhancer")
71
  result = client.predict(
72
  input_image=handle_file(img_path),
@@ -99,6 +73,30 @@ async def upscale_image(image, upscale_factor):
99
 
100
  return result
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  async def gen(
103
  prompt:str,
104
  lora_add:str="XLabs-AI/flux-RealismLora",
@@ -108,7 +106,7 @@ async def gen(
108
  scales:float=3.5,
109
  steps:int=24,
110
  seed:int=-1,
111
- upscale_factor:int=2
112
  ):
113
  model = enable_lora(lora_add)
114
  image, seed = await generate_image(prompt,model,lora_word,width,height,scales,steps,seed)
@@ -127,78 +125,11 @@ with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
127
  sendBtn = gr.Button(scale=1, variant='primary')
128
  with gr.Accordion("Opciones avanzadas", open=True):
129
  with gr.Column(scale=1):
130
- width = gr.Slider(
131
- label="Ancho",
132
- minimum=512,
133
- maximum=1280,
134
- step=8,
135
- value=768,
136
- )
137
- height = gr.Slider(
138
- label="Alto",
139
- minimum=512,
140
- maximum=1280,
141
- step=8,
142
- value=1024,
143
- )
144
- scales = gr.Slider(
145
- label="Guía",
146
- minimum=3.5,
147
- maximum=7,
148
- step=0.1,
149
- value=3.5,
150
- )
151
- steps = gr.Slider(
152
- label="Pasos",
153
- minimum=1,
154
- maximum=100,
155
- step=1,
156
- value=24,
157
- )
158
- seed = gr.Slider(
159
- label="Semillas",
160
- minimum=-1,
161
- maximum=MAX_SEED,
162
- step=1,
163
- value=-1,
164
- )
165
- lora_add = gr.Textbox(
166
- label="Agregar Flux LoRA",
167
- info="Modelo de LoRA a agregar",
168
- lines=1,
169
- value="XLabs-AI/flux-RealismLora",
170
- )
171
- lora_word = gr.Textbox(
172
- label="Palabra clave de LoRA",
173
- info="Palabra clave para activar el modelo de LoRA",
174
- lines=1,
175
- value="",
176
- )
177
- upscale_factor = gr.Radio(
178
- label="Factor de escalado",
179
- choices=[2, 3, 4],
180
- value=2,
181
- )
182
-
183
- gr.on(
184
- triggers=[
185
- prompt.submit,
186
- sendBtn.click,
187
- ],
188
- fn=gen,
189
- inputs=[
190
- prompt,
191
- lora_add,
192
- lora_word,
193
- width,
194
- height,
195
- scales,
196
- steps,
197
- seed,
198
- upscale_factor
199
- ],
200
- outputs=[img, seed]
201
- )
202
-
203
- if __name__ == "__main__":
204
- demo.queue(api_open=False).launch(show_api=False, share=False)
 
 
1
  import gradio as gr
2
+ import os
3
+ from gradio_client import Client, handle_file
4
+ from huggingface_hub import login
5
+ from PIL import Image
6
  import numpy as np
7
  import random
 
8
  from translatepy import Translator
9
  import requests
10
  import re
11
  import asyncio
12
+
13
+ login(token=os.environ.get("HF_TOKEN", None), username=os.environ.get("HF_USERNAME", None))
14
 
15
  translator = Translator()
 
16
  basemodel = "black-forest-labs/FLUX.1-dev"
17
  MAX_SEED = np.iinfo(np.int32).max
18
 
 
35
  else:
36
  return lora_add
37
 
38
+ def handle_file(img_path):
39
+ return Image.open(img_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
42
+ if upscale_factor == 0:
43
+ return handle_file(img_path)
44
  client = Client("finegrain/finegrain-image-enhancer")
45
  result = client.predict(
46
  input_image=handle_file(img_path),
 
73
 
74
  return result
75
 
76
+ async def generate_image(
77
+ prompt:str,
78
+ model:str,
79
+ lora_word:str,
80
+ width:int=768,
81
+ height:int=1024,
82
+ scales:float=3.5,
83
+ steps:int=24,
84
+ seed:int=-1
85
+ ):
86
+ if seed == -1:
87
+ seed = random.randint(0, MAX_SEED)
88
+ seed = int(seed)
89
+ print(f'prompt:{prompt}')
90
+
91
+ text = str(translator.translate(prompt, 'English')) + "," + lora_word
92
+
93
+ try:
94
+ image = gr.Image(type="pil", image=gr.processing_utils.encode_pil_image(text_to_image(text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)))
95
+ except Exception as e:
96
+ raise gr.Error(f"Error in {e}")
97
+
98
+ return image, seed
99
+
100
  async def gen(
101
  prompt:str,
102
  lora_add:str="XLabs-AI/flux-RealismLora",
 
106
  scales:float=3.5,
107
  steps:int=24,
108
  seed:int=-1,
109
+ upscale_factor:int=0
110
  ):
111
  model = enable_lora(lora_add)
112
  image, seed = await generate_image(prompt,model,lora_word,width,height,scales,steps,seed)
 
125
  sendBtn = gr.Button(scale=1, variant='primary')
126
  with gr.Accordion("Opciones avanzadas", open=True):
127
  with gr.Column(scale=1):
128
+ width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=768)
129
+ height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=1024)
130
+ scales = gr.Slider(label="Guía", minimum=3.5, maximum=7, step=0.1, value=3.5)
131
+ steps = gr.Slider(label="Pasos", minimum=1, maximum=50, step=1)
132
+ upscale_factor = gr.Slider(label="Factor de escala", minimum=0, maximum=4, step=1, value=0)
133
+ seed = gr.Number(label="Semilla", value=-1)
134
+ sendBtn.click(gen, inputs=[prompt, lora_add, lora_word, width, height, scales, steps, seed, upscale_factor], outputs=[img])
135
+ demo.launch()