Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,17 @@
|
|
1 |
-
|
2 |
-
from pydantic import BaseModel
|
3 |
-
from pyngrok import ngrok
|
4 |
-
import nest_asyncio
|
5 |
-
import uvicorn
|
6 |
-
from diffusers import StableDiffusionPipeline
|
7 |
import torch
|
|
|
|
|
8 |
from PIL import Image
|
9 |
-
import io
|
10 |
-
import base64
|
11 |
|
12 |
-
# Instanciar a aplicação FastAPI
|
13 |
-
app = FastAPI()
|
14 |
-
|
15 |
-
# Carregar o modelo da Hugging Face
|
16 |
-
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5" # Substitua pelo ID do seu modelo
|
17 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
18 |
-
pipe = pipe.to("cuda") # Use GPU
|
19 |
-
|
20 |
-
# Definir o modelo de dados de entrada
|
21 |
-
class InputData(BaseModel):
|
22 |
-
prompt: str
|
23 |
-
|
24 |
-
# Endpoint inicial para verificar o funcionamento
|
25 |
-
@app.get("/")
|
26 |
-
def read_root():
|
27 |
-
return {"message": "API is running. Use /generate to create an image."}
|
28 |
-
|
29 |
-
# Endpoint para gerar imagens
|
30 |
-
@app.post("/generate")
|
31 |
-
def generate_image(data: InputData):
|
32 |
-
# Gerar imagem com o modelo Hugging Face
|
33 |
-
image = pipe(data.prompt).images[0]
|
34 |
-
|
35 |
-
# Converter a imagem para Base64
|
36 |
-
buffered = io.BytesIO()
|
37 |
-
image.save(buffered, format="PNG")
|
38 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
39 |
-
|
40 |
-
# Retornar o resultado
|
41 |
-
return {"result": f"Generated image for prompt: {data.prompt}", "image": img_str}
|
42 |
-
|
43 |
-
# Expor o servidor publicamente com ngrok
|
44 |
-
public_url = ngrok.connect(8000)
|
45 |
-
print(f"Public URL: {public_url}")
|
46 |
-
|
47 |
-
# Corrigir o loop de execução no Colab
|
48 |
-
nest_asyncio.apply()
|
49 |
-
|
50 |
-
# Iniciar o servidor
|
51 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
52 |
|
|
|
|
|
|
|
53 |
|
|
|
|
|
|
|
54 |
|
55 |
# Carregando o dataset do Hugging Face
|
56 |
dataset = load_dataset("LEIDIA/Data_Womleimg")
|
@@ -58,50 +19,32 @@ dataset = load_dataset("LEIDIA/Data_Womleimg")
|
|
58 |
|
59 |
# Parâmetros para carregar o dataset personalizado
|
60 |
dataset_descriptions = [
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
"Images/Image19.jpg,a brunette girl wearing a black leather top and matching black leather pants with a decorative button on the waist. It presented in a stylish, modern pose against a gray background",
|
80 |
-
"Images/Image20.jpg,a young asian woman standing inside a subway car or train. She is leaning on one of the vertical poles of the car, with her arms crossed. The woman is dressed in a white short-sleeved t-shirt and a black leather shorts. Her hair is long and dark, and she appears to be looking to the side, with a serious or thoughtful expression.",
|
81 |
-
"Images/Image21.jpg,a young asian woman in a subway or train environment. She is standing near the carriage doors, with one hand resting on her hip and the other at her side. The woman is dressed in a tight white tank top and black leather pants. Her hair is long, straight and dark, and she is looking directly at the camera with a serious expression.",
|
82 |
-
"Images/Image22.jpg,a young brunette woman posing with her back to the camera, with her face turned to the side. She is dressed in a black leather top and tight black leather pants, as well as black leather gloves. His hair is long and wavy, with light highlights.The pose and outfit suggest a sensual and modern style, possibly for a fashion photo shoot or a themed shoot.",
|
83 |
-
"Images/Image23.jpg,The image shows a part of a woman's body, specifically the butt region and part of the thighs. The woman is wearing a white thigh leather pants. The outfit has a cut that highlights the curves of the body and includes a wide belt at the waist with metallic details.The background of the image is neutral and blurred, which helps to highlight the clothing and body shape, The lighting is soft and even, enhancing the texture and shine of the clothing material. The pose and choice of clothing suggest a modern and sensual style, possibly for a fashion photo shoot or a themed shoot.The image focuses on the silhouette and curves of the body, highlighting the model's clothes and physical shape.",
|
84 |
-
"Images/Image24.jpg,This image shows a part of a woman's body, specifically the butt region and part of the thighs, seen from the side.The woman is wearing a white tight leather pants,The clothing has visible seams that highlight the body's curves.The belt at the waist is wide and has metallic details, including a small clasp or button. The background of the image is neutral and blurred, which helps to highlight the clothing and body shape. The lighting is soft and even, enhancing the texture and shine of the clothing material.The image focuses on the silhouette and curves of the body, highlighting the model's clothes and physical shape. The choice of material and design of the clothing, combined with the model's pose, conveys a feeling of elegance and sophistication.",
|
85 |
-
"Images/Image25.jpg,shows a woman's lower body seen from behind, a beautiful butt in shape,She is wearing a tight white leather pants that highlights your curves,The outfit has visible seams and a wide belt at the waist. The background is neutral and blurred, highlighting the woman's silhouette.",
|
86 |
-
"Images/Image26.jpg,a woman from behind, dressed in a tight-fitting outfit made of gray leather clothes, The outfit includes details that resemble armor, such as shoulder pads and a wide belt with a buckle. The woman's hair is tied up in a high ponytail, and she is looking to the side. The background is neutral and blurred, highlighting the woman's figure.",
|
87 |
-
"Images/Image27.jpg,a woman sitting in a chair, dressed in a black blazer and purple leather pants, She is wearing black high-heeled leather boots and a black choker. Her hair is blonde and wavy, and she is looking directly into the camera. The background is neutral and blurred, highlighting the woman's figure.",
|
88 |
-
"Images/Image28.jpg,a woman posing on an urban street. She is dressed in a black leather jacket, black leather pants, and tall boots with buckle details. Her hair is blonde and wavy curly, and she wears a light-colored T-shirt under her jacket. The background includes a wooden door and a stone wall, suggesting an urban and historic environment.",
|
89 |
-
"Images/Image29.jpg,a blonde woman wearing a black leather zip-up jacket and tight black leather pants. She is posing with one hand on her hip and the other holding the zipper of her jacket. The background is a textured wall, and the lighting highlights the woman's outfit and confident pose.",
|
90 |
-
"Images/Image30.jpg,a blonde woman dressed in a black leather jacket, tight black leather pants, and thigh-high boots. She is standing with her legs apart and one hand on her waist, in a confident pose. The background appears to be a photography studio with concrete walls and visible equipment.",
|
91 |
-
"Images/Image31.jpg,a blonde woman standing, dressed in a black leather jacket and tight black leather pants. The jacket has zippers and metallic details, and the woman has one hand in her pocket and the other at her side. She has blonde, wavy hair, and is looking directly at the camera with a confident expression. The background is neutral and blurred, highlighting the woman's figure.",
|
92 |
-
"Images/Image32.jpg,a woman standing, dressed in a long red leather coat, a short black leather skirt, black high boots and a black turtleneck. She is holding a black bag and posing with one hand on her hip. The background is neutral with red curtains, highlighting the woman and her clothes.",
|
93 |
-
"Images/Image33.jpg,a woman on an urban street. She is wearing a red leather coat open, revealing a top that exposes her abdomen, and a short dark red leather shorts. Her hair is long and loose, and she is wearing red lipstick. The background shows a busy street with blurred shops and people."
|
94 |
-
|
95 |
]
|
96 |
|
97 |
-
|
98 |
-
|
99 |
# Definir parâmetros padrão para geração rápida
|
100 |
DEFAULT_PROMPT = "A beautiful brunette woman wearing a blue leather pants "
|
101 |
-
DEFAULT_INFERENCE_STEPS =
|
102 |
IMAGE_WIDTH = 512
|
103 |
IMAGE_HEIGHT = 812
|
104 |
-
GUIDANCE_SCALE =
|
105 |
|
106 |
def resize_to_divisible_by_8(image):
|
107 |
width, height = image.size
|
@@ -111,22 +54,18 @@ def resize_to_divisible_by_8(image):
|
|
111 |
|
112 |
# Função simples para gerar imagem
|
113 |
def infer_simple(prompt):
|
114 |
-
# Ajustando as dimensões para serem divisíveis por 8
|
115 |
-
adjusted_width = IMAGE_WIDTH + (8 - IMAGE_WIDTH % 8) if IMAGE_WIDTH % 8 != 0 else IMAGE_WIDTH
|
116 |
-
adjusted_height = IMAGE_HEIGHT + (8 - IMAGE_HEIGHT % 8) if IMAGE_HEIGHT % 8 != 0 else IMAGE_HEIGHT
|
117 |
-
|
118 |
# Geração da imagem
|
119 |
image = pipe(
|
120 |
prompt=prompt,
|
121 |
num_inference_steps=DEFAULT_INFERENCE_STEPS,
|
122 |
guidance_scale=GUIDANCE_SCALE,
|
123 |
-
height=
|
124 |
-
width=
|
125 |
).images[0]
|
126 |
-
|
|
|
127 |
return image
|
128 |
|
129 |
-
|
130 |
# Interface Gradio
|
131 |
with gr.Blocks() as demo:
|
132 |
with gr.Row():
|
@@ -148,4 +87,4 @@ with gr.Blocks() as demo:
|
|
148 |
)
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
+
from datasets import load_dataset
|
5 |
from PIL import Image
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Configurações do dispositivo para uso apenas da CPU
|
9 |
+
device = "cpu"
|
10 |
+
model_repo_id = "stabilityai/sdxl-turbo" # Continuando com o modelo especificado
|
11 |
|
12 |
+
# Carregar o pipeline configurado para CPU
|
13 |
+
pipe = DiffusionPipeline.from_pretrained(model_repo_id)
|
14 |
+
pipe = pipe.to(device)
|
15 |
|
16 |
# Carregando o dataset do Hugging Face
|
17 |
dataset = load_dataset("LEIDIA/Data_Womleimg")
|
|
|
19 |
|
20 |
# Parâmetros para carregar o dataset personalizado
|
21 |
dataset_descriptions = [
|
22 |
+
"A woman wearing a full blue leather catsuit",
|
23 |
+
"A woman in black leather pants",
|
24 |
+
"A legs woman in tight high blue leather boots",
|
25 |
+
"A woman in a long red leather jacket, red leather shorts, and tight high red leather boots",
|
26 |
+
"A legs woman in cream color leather pants",
|
27 |
+
"A woman in purple leather leggings with tight high black leather boots",
|
28 |
+
"A woman in black leather top and a long black leather skirt",
|
29 |
+
"A blonde woman with long curly hair wearing a yellow mini tight leather skirt",
|
30 |
+
"A thin Asian woman wearing a thigh-long black leather dress",
|
31 |
+
"Simple high brown leather boots",
|
32 |
+
"A beautiful brunette woman wearing leather clothes",
|
33 |
+
"A beautiful brunette woman in a sleeveless black dress seated at a bar holding a glass of champagne, with a cozy and elegant atmosphere in the background.",
|
34 |
+
"A curly blonde woman wearing a bold red leather jacket paired with black leather tight pants and red high-heeled leather boots, creating a modern and edgy vibe.",
|
35 |
+
"An ebony woman standing outdoors against a backdrop of rolling hills and a cloudy sky, wearing a striking outfit of a red leather shirt, black leather mini corset, red plaid skirt, and knee-high red lace-up leather boots.",
|
36 |
+
"A blonde curly woman wearing a fitted, shiny blue leather outfit including a jacket and pants with metallic buttons, paired with knee-high boots, in a neutral-colored room.",
|
37 |
+
"A girl in a black leather outfit with a heart-shaped cutout top, high-waisted leggings, and a purple cape, giving a superhero vibe.",
|
38 |
+
"A girl in a sleek black leather cropped top with a zip closure and high-waisted bottom, paired with long black gloves and pink hair styled in a ponytail, creating a bold and fashion-forward look.",
|
39 |
+
"A girl wearing a form-fitting black leather top, with long pink hair cascading down, creating a striking contrast in a neutral background."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
]
|
41 |
|
|
|
|
|
42 |
# Definir parâmetros padrão para geração rápida
|
43 |
DEFAULT_PROMPT = "A beautiful brunette woman wearing a blue leather pants "
|
44 |
+
DEFAULT_INFERENCE_STEPS = 6
|
45 |
IMAGE_WIDTH = 512
|
46 |
IMAGE_HEIGHT = 812
|
47 |
+
GUIDANCE_SCALE = 5.5
|
48 |
|
49 |
def resize_to_divisible_by_8(image):
|
50 |
width, height = image.size
|
|
|
54 |
|
55 |
# Função simples para gerar imagem
|
56 |
def infer_simple(prompt):
|
|
|
|
|
|
|
|
|
57 |
# Geração da imagem
|
58 |
image = pipe(
|
59 |
prompt=prompt,
|
60 |
num_inference_steps=DEFAULT_INFERENCE_STEPS,
|
61 |
guidance_scale=GUIDANCE_SCALE,
|
62 |
+
height=IMAGE_HEIGHT,
|
63 |
+
width=IMAGE_WIDTH,
|
64 |
).images[0]
|
65 |
+
# Redimensionar a imagem para ser divisível por 8
|
66 |
+
image = resize_to_divisible_by_8(image)
|
67 |
return image
|
68 |
|
|
|
69 |
# Interface Gradio
|
70 |
with gr.Blocks() as demo:
|
71 |
with gr.Row():
|
|
|
87 |
)
|
88 |
|
89 |
if __name__ == "__main__":
|
90 |
+
demo.launch()
|