Cesar Ortego Navacerrada
commited on
Commit
·
b98931c
1
Parent(s):
d532ff1
Add application file
Browse files- imagenesGradio.py +109 -0
imagenesGradio.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
######################
|
2 |
+
# Datos importados
|
3 |
+
######################
|
4 |
+
|
5 |
+
#Datos importados de: https://huggingface.co/runwayml/stable-diffusion-v1-5
|
6 |
+
|
7 |
+
######################
|
8 |
+
# Import libraries
|
9 |
+
######################
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
# ######################
|
14 |
+
# Variables globales
|
15 |
+
# ######################
|
16 |
+
|
17 |
+
model_id = r"..\7. Generador_imagenes\3. Stable_diffusion web\stable-diffusion-v1-5"
|
18 |
+
|
19 |
+
# ######################
|
20 |
+
# Funciones auxiliares
|
21 |
+
# ######################
|
22 |
+
|
23 |
+
if False:
|
24 |
+
def pipe_callback(step: int, timestep: int, latents: torch.FloatTensor):
|
25 |
+
|
26 |
+
with st.container():
|
27 |
+
|
28 |
+
st.write(f'Vamos por la iteración {step}')
|
29 |
+
st.progress(step*2) #bar_progress debe empezar con 0 y terminar en 100
|
30 |
+
st.write(f'Quedan {timestep/100:.0f} segundos')
|
31 |
+
|
32 |
+
# ######################
|
33 |
+
# Modelo
|
34 |
+
# ######################
|
35 |
+
|
36 |
+
def ia_imagenes(modelo, prompt, prompt_negativo, uploaded_file, my_strength, my_guidance_scale):
|
37 |
+
|
38 |
+
if modelo == "Texto":
|
39 |
+
|
40 |
+
from diffusers import StableDiffusionPipeline
|
41 |
+
import torch
|
42 |
+
|
43 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
44 |
+
model_id,
|
45 |
+
revision="fp16" if torch.cuda.is_available() else "fp32",
|
46 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
47 |
+
requires_safety_checker = False
|
48 |
+
).to("cuda")
|
49 |
+
|
50 |
+
|
51 |
+
image_pipe = pipe(prompt, negative_prompt=prompt_negativo, width=728, height=728) #otras variables: guidance_scale=guidance_scale, num_inference_steps=steps, callback = pipe_callback
|
52 |
+
|
53 |
+
imagen = image_pipe.images[0]
|
54 |
+
|
55 |
+
return imagen
|
56 |
+
|
57 |
+
|
58 |
+
elif modelo == "Imagen":
|
59 |
+
|
60 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
61 |
+
from PIL import Image
|
62 |
+
import torch
|
63 |
+
|
64 |
+
uploaded_file = Image.fromarray(uploaded_file)
|
65 |
+
|
66 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
67 |
+
model_id,
|
68 |
+
revision="fp16" if torch.cuda.is_available() else "fp32",
|
69 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
70 |
+
requires_safety_checker = False
|
71 |
+
).to("cuda")
|
72 |
+
|
73 |
+
my_strength = 0.8 if my_strength == 0 or my_strength == None else my_strength
|
74 |
+
my_guidance_scale = 7.5 if my_guidance_scale == 0 or my_guidance_scale == None else my_guidance_scale
|
75 |
+
|
76 |
+
imagen = pipe(prompt, image=uploaded_file, negative_prompt=prompt_negativo, strength = my_strength, guidance_scale = my_guidance_scale).images[0] #, strength=0.8, guidance_scale=7.5
|
77 |
+
|
78 |
+
return imagen
|
79 |
+
|
80 |
+
else:
|
81 |
+
raise gr.Error("Te has olvidado de marcar una opción")
|
82 |
+
|
83 |
+
|
84 |
+
demo = gr.Interface(
|
85 |
+
fn = ia_imagenes,
|
86 |
+
inputs = [
|
87 |
+
gr.Radio(["Texto", "Imagen"],value = "Texto"),
|
88 |
+
"text",
|
89 |
+
"text",
|
90 |
+
"image",
|
91 |
+
"number",
|
92 |
+
"number"
|
93 |
+
|
94 |
+
],
|
95 |
+
outputs = "image",
|
96 |
+
title="Creación de Imagenes 🍆",
|
97 |
+
)
|
98 |
+
|
99 |
+
|
100 |
+
demo.launch(show_error = True, auth=("cesar", "mascara"), share=True)
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
'''
|
105 |
+
FALTA:
|
106 |
+
-BOTON PARA DESCARGAR IMAGEN
|
107 |
+
-TIEMPO QUE TARDA
|
108 |
+
'''
|
109 |
+
|