onnew commited on
Commit
b8ec2f8
·
verified ·
1 Parent(s): 58126da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -42
app.py CHANGED
@@ -1,59 +1,33 @@
1
- import os
2
- import random
3
- import uuid
4
  import gradio as gr
5
- import numpy as np
6
- from PIL import Image
7
- import torch
8
- from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
9
- from typing import Tuple
10
 
11
- css = '''
12
- .gradio-container{max-width: 575px !important}
13
- h1{text-align:center}
14
- footer {
15
- visibility: hidden
16
- }
17
- '''
18
 
19
- model_id = "stabilityai/stable-diffusion-xl-base-1.0"
20
- scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
21
- pipe = StableDiffusionXLPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16, revision="fp16")
22
- pipe = pipe.to("cuda")
23
 
24
- def generate_image(prompt: str, negative_prompt: str = "", height: int = 512, width: int = 512, num_inference_steps: int = 50, guidance_scale: float = 7.5, num_images_per_prompt: int = 1) -> Tuple[Image.Image, str]:
25
- generator = torch.Generator(device="cuda").manual_seed(random.randint(0, 2**32 - 1))
26
- image = pipe(
27
- prompt=prompt,
28
- negative_prompt=negative_prompt,
29
- height=height,
30
- width=width,
31
- num_inference_steps=num_inference_steps,
32
- guidance_scale=guidance_scale,
33
- num_images_per_prompt=num_images_per_prompt,
34
- generator=generator,
35
- ).images[0]
36
-
37
- image_id = str(uuid.uuid4())
38
- image_path = f"/tmp/{image_id}.png"
39
- image.save(image_path)
40
-
41
- return image, image_path
42
 
 
43
  def gradio_interface():
44
- with gr.Blocks(css=css) as demo:
45
- gr.Markdown("## Gere imagens usando Stable Diffusion XL")
46
  with gr.Row():
47
  with gr.Column():
48
  prompt = gr.Textbox(label="Prompt", placeholder="Digite o prompt aqui...")
49
- negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Digite o negative prompt aqui...")
50
  run_button = gr.Button("Gerar Imagem")
51
  with gr.Column():
52
  result = gr.Image(label="Imagem Gerada")
53
 
54
  run_button.click(
55
- fn=lambda p, np: generate_image(p, np)[0],
56
- inputs=[prompt, negative_prompt],
57
  outputs=result,
58
  )
59
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
 
 
 
 
3
 
4
+ # Função para gerar imagem usando a API do Hugging Face
5
+ def generate_image(prompt: str):
6
+ API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
7
+ headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY"}
 
 
 
8
 
9
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
 
 
 
10
 
11
+ if response.status_code == 200:
12
+ image_url = response.json()
13
+ return image_url
14
+ else:
15
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Função para criar a interface do Gradio
18
  def gradio_interface():
19
+ with gr.Blocks() as demo:
20
+ gr.Markdown("## Gere imagens usando Stable Diffusion")
21
  with gr.Row():
22
  with gr.Column():
23
  prompt = gr.Textbox(label="Prompt", placeholder="Digite o prompt aqui...")
 
24
  run_button = gr.Button("Gerar Imagem")
25
  with gr.Column():
26
  result = gr.Image(label="Imagem Gerada")
27
 
28
  run_button.click(
29
+ fn=generate_image,
30
+ inputs=prompt,
31
  outputs=result,
32
  )
33