Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455561 unmodified line5859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061073 unmodified lines11111211311411511611711811912012112212312412512612712812913013113215 unmodified lines148149150151from fastapi import FastAPIfrom pydantic import BaseModelfrom pyngrok import ngrokimport nest_asyncioimport uvicornfrom diffusers import StableDiffusionPipelineimport torchfrom PIL import Imageimport ioimport base64# Instanciar a aplicação FastAPIapp = FastAPI()# Carregar o modelo da Hugging Facemodel_id = "stable-diffusion-v1-5/stable-diffusion-v1-5" # Substitua pelo ID do seu modelopipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)pipe = pipe.to("cuda") # Use GPU# Definir o modelo de dados de entradaclass InputData(BaseModel):prompt: str# Endpoint inicial para verificar o funcionamento@app.get("/")def read_root():return {"message": "API is running. Use /generate to create an image."}# Endpoint para gerar imagens@app.post("/generate")def generate_image(data: InputData):# Gerar imagem com o modelo Hugging Faceimage = pipe(data.prompt).images[0]# Converter a imagem para Base64buffered = io.BytesIO()image.save(buffered, format="PNG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")# Retornar o resultadoreturn {"result": f"Generated image for prompt: {data.prompt}", "image": img_str}# Expor o servidor publicamente com ngrokpublic_url = ngrok.connect(8000)print(f"Public URL: {public_url}")# Corrigir o loop de execução no Colabnest_asyncio.apply()# Iniciar o servidoruvicorn.run(app, host="0.0.0.0", port=8000)# Carregando o dataset do Hugging Facedataset = load_dataset("LEIDIA/Data_Womleimg")1 unmodified line# Parâmetros para carregar o dataset personalizadodataset_descriptions = ["Images/Image01.jpg,A blonde woman wearing a blue leather jumpsuit with leather gloves the same color. She is standing with one hand on her face and the other on her waist, in a confident pose. The background is dark and neutral, highlighting the woman's figure.","Images/Image02.jpg,A woman standing in an urban environment, possibly a street between buildings.She is dressed in a long-sleeved white blouse and tight black leather pants, with a wide belt at her waist. She wears high black boots and has long, loose hair. The background includes buildings and a street, suggesting an urban setting.","Images/Image03.jpg,A woman's legs wearing blue high leather boots with a zipper on the side and high heels.The background is a light blue color, highlighting the boots. The woman's legs is standing, with slightly apart, showing her boots prominently.","Images/Image04.jpg,A brunette woman wearing a long red leather coat combined with a leather shorts and tigh high leather boots of the same color. The coat is open, revealing the skirt and boots. The background is neutral and blurred, highlighting the woman's figure.","Images/Image05.jpg,The image shows the lower part of a woman's body, seen from the side. She is wearing tight beige leather pants and black high-heeled boots. is leaning against a brick wall, and the background includes a glass door. The lighting is natural, highlighting the woman's curves and clothing.","Images/Image06.jpg,A woman in purple leather leggings with tigh high black leather boots","Images/Image07.jpg,A woman in black leather top and a long black leather skirt","Images/Image08.jpg,A blonde long hair curly woman using a yellow mini tight leather skirt","Images/Image09.jpg,A thin asian woman using a thigh long black leather dress","Images/Image10.jpg,A simple high brown leather boots","Images/Image11.jpg,A beautiful face brunette woman using a leather clothes","Images/Image12.jpg,A beautiful brunette woman wearing a sleeveless black dress, seated at a bar, She is holding a glass champagne, The background is softly lit, with warm lighting and blurred bottles on shelves, creating a cozy and elegant atmosphere.","Images/Image13.jpg,A curly blonde woman is wearing a bold and stylish outfit red leather jacket paired with black leather tight pants and red high-heeled leather boots, The outfit has a modern and edgy vibe, with a focus on vibrant colors and sleek materials.","Images/Image14.jpg,Ebony woman standing outdoors against a backdrop of rolling hills and a cloudy sky,wearing a striking outfit consisting of a red leather shirt, a black leather mini corset, and a red plaid skirt with a long panel on one side,also wearing knee-high red lace-up leather boots, Their hair is voluminous and styled in natural curls, The setting appears to be a grassy landscape.","Images/Image15.jpg,Blonde curly woman is wearing a fitted, shiny blue outfit made of what appears to be leather or vinyl clothes, The ensemble includes a jacket and pants with metallic buttons and a belt at the waist, They are also wearing knee-high boots with lacing details, Their hair is styled in voluminous, curly blonde locks. The setting is a simple, neutral-colored room with a concrete or stone-like wall and floor.","Images/Image16.jpg,The girl is wearing a black leather outfit include top,legging and sleeves,consisting of a fitted top with a heart-shaped cutout and high-waisted pants, The ensemble includes a purple cape and the individual has long purple hair,The style is reminiscent of superhero or fantasy attire, emphasizing a bold and sleek look.","Images/Image17.jpg,The girl dressed in a sleek, black leather outfit. The attire includes a cropped top with a zip closure and a high-waisted bottom, both designed to accentuate the figure's silhouette, The individual has long, pink hair styled in a ponytail, and is wearing long black gloves that reach the upper arms. The background appears to be softly lit, enhancing the glossy texture of the leather. The overall look is bold and fashion-forward, with a striking color contrast between the black and pink elements.","Images/Image18.jpg,a girl wearing a form-fitting black leather top that highlights their physique, The top has a high neckline and is sleeveless, emphasizing the shoulders and arms, The individual has long, pink hair cascading down, adding a striking contrast to the outfit. The background is a light, neutral color, which helps to accentuate the subject, The overall aesthetic is bold and fashion-forward.","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","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.","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.","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.","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.","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.","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.","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.","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.","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.","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.","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.","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.","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.","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."]# Definir parâmetros padrão para geração rápidaDEFAULT_PROMPT = "A beautiful brunette woman wearing a blue leather pants "DEFAULT_INFERENCE_STEPS = 7IMAGE_WIDTH = 512IMAGE_HEIGHT = 812GUIDANCE_SCALE = 7def resize_to_divisible_by_8(image):width, height = image.size3 unmodified lines# Função simples para gerar imagemdef infer_simple(prompt):# Ajustando as dimensões para serem divisíveis por 8adjusted_width = IMAGE_WIDTH + (8 - IMAGE_WIDTH % 8) if IMAGE_WIDTH % 8 != 0 else IMAGE_WIDTHadjusted_height = IMAGE_HEIGHT + (8 - IMAGE_HEIGHT % 8) if IMAGE_HEIGHT % 8 != 0 else IMAGE_HEIGHT# Geração da imagemimage = pipe(prompt=prompt,num_inference_steps=DEFAULT_INFERENCE_STEPS,guidance_scale=GUIDANCE_SCALE,height=adjusted_height,width=adjusted_width,).images[0]return image# Interface Gradiowith gr.Blocks() as demo:with gr.Row():15 unmodified lines)if __name__ == "__main__":demo.launch()12345678910111213141516171 unmodified line19202122232425262728293031323334353637383940414243444546474849503 unmodified lines54555657585960616263646566676869707115 unmodified lines87888990import gradio as grimport torchfrom diffusers import DiffusionPipelinefrom datasets import load_datasetfrom PIL import Image# Configurações do dispositivo para uso apenas da CPUdevice = "cpu"model_repo_id = "stabilityai/sdxl-turbo" # Continuando com o modelo especificado# Carregar o pipeline configurado para CPUpipe = DiffusionPipeline.from_pretrained(model_repo_id)pipe = pipe.to(device)# Carregando o dataset do Hugging Facedataset = load_dataset("LEIDIA/Data_Womleimg")1 unmodified line# Parâmetros para carregar o dataset personalizadodataset_descriptions = ["A woman wearing a full blue leather catsuit","A woman in black leather pants","A legs woman in tight high blue leather boots","A woman in a long red leather jacket, red leather shorts, and tight high red leather boots","A legs woman in cream color leather pants","A woman in purple leather leggings with tight high black leather boots","A woman in black leather top and a long black leather skirt","A blonde woman with long curly hair wearing a yellow mini tight leather skirt","A thin Asian woman wearing a thigh-long black leather dress","Simple high brown leather boots","A beautiful brunette woman wearing leather clothes","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.","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.","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.","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.","A girl in a black leather outfit with a heart-shaped cutout top, high-waisted leggings, and a purple cape, giving a superhero vibe.","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.","A girl wearing a form-fitting black leather top, with long pink hair cascading down, creating a striking contrast in a neutral background."]# Definir parâmetros padrão para geração rápidaDEFAULT_PROMPT = "A beautiful brunette woman wearing a blue leather pants "DEFAULT_INFERENCE_STEPS = 6IMAGE_WIDTH = 512IMAGE_HEIGHT = 812GUIDANCE_SCALE = 5.5def resize_to_divisible_by_8(image):width, height = image.size3 unmodified lines# Função simples para gerar imagemdef infer_simple(prompt):# Geração da imagemimage = pipe(prompt=prompt,num_inference_steps=DEFAULT_INFERENCE_STEPS,guidance_scale=GUIDANCE_SCALE,height=IMAGE_HEIGHT,width=IMAGE_WIDTH,).images[0]# Redimensionar a imagem para ser divisível por 8image = resize_to_divisible_by_8(image)return image# Interface Gradiowith gr.Blocks() as demo:with gr.Row():15 unmodified lines)if __name__ == "__main__":demo.launch()
app.py+34-95