Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,33 @@
|
|
1 |
-
import os
|
2 |
-
import random
|
3 |
-
import uuid
|
4 |
import gradio as gr
|
5 |
-
import
|
6 |
-
from PIL import Image
|
7 |
-
import torch
|
8 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
9 |
-
from typing import Tuple
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
visibility: hidden
|
16 |
-
}
|
17 |
-
'''
|
18 |
|
19 |
-
|
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 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
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(
|
45 |
-
gr.Markdown("## Gere imagens usando Stable Diffusion
|
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=
|
56 |
-
inputs=
|
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 |
|