Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import requests
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
def load_models():
|
| 9 |
+
# Carga el modelo Stable Diffusion en CPU
|
| 10 |
+
pipe_sd = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
|
| 11 |
+
pipe_sd = pipe_sd.to("cpu")
|
| 12 |
+
|
| 13 |
+
# URLs ejemplo para simular DALL路E mini y Free Image Generation
|
| 14 |
+
url_dalle_mini = "https://huggingface.co/spaces/dalle-mini/dalle-mini/resolve/main/sample_image.jpg"
|
| 15 |
+
url_free_img_gen = "https://huggingface.co/spaces/aiyouthalliance/Free-Image-Generation/resolve/main/sample_image.jpg"
|
| 16 |
+
|
| 17 |
+
return pipe_sd, url_dalle_mini, url_free_img_gen
|
| 18 |
+
|
| 19 |
+
pipe_sd, url_dalle_mini, url_free_img_gen = load_models()
|
| 20 |
+
|
| 21 |
+
def generate_image_stable_diffusion(prompt):
|
| 22 |
+
image = pipe_sd(prompt, height=768, width=432).images[0] # Formato 9:16
|
| 23 |
+
return image
|
| 24 |
+
|
| 25 |
+
def generate_image_dalle_mini(prompt):
|
| 26 |
+
response = requests.get(url_dalle_mini)
|
| 27 |
+
image = Image.open(BytesIO(response.content))
|
| 28 |
+
return image.resize((432, 768))
|
| 29 |
+
|
| 30 |
+
def generate_image_free_img_gen(prompt):
|
| 31 |
+
response = requests.get(url_free_img_gen)
|
| 32 |
+
image = Image.open(BytesIO(response.content))
|
| 33 |
+
return image.resize((432, 768))
|
| 34 |
+
|
| 35 |
+
def generate_all_images(prompt):
|
| 36 |
+
img_sd = generate_image_stable_diffusion(prompt)
|
| 37 |
+
img_dalle = generate_image_dalle_mini(prompt)
|
| 38 |
+
img_free = generate_image_free_img_gen(prompt)
|
| 39 |
+
return img_sd, img_dalle, img_free
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# BATUTO-art")
|
| 43 |
+
prompt = gr.Textbox(label="Introduce tu prompt", lines=1)
|
| 44 |
+
with gr.Row():
|
| 45 |
+
img_sd = gr.Image(label="Stable Diffusion", interactive=True)
|
| 46 |
+
img_dalle = gr.Image(label="DALL路E mini", interactive=True)
|
| 47 |
+
img_free = gr.Image(label="Free Image Generation", interactive=True)
|
| 48 |
+
btn = gr.Button("Generar im谩genes")
|
| 49 |
+
btn.click(fn=generate_all_images, inputs=prompt, outputs=[img_sd, img_dalle, img_free])
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|
| 53 |
+
|