Create File
Browse files
File
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import the required libraries
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Set up the model and device
|
| 8 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 10 |
+
|
| 11 |
+
# Move the model to the GPU if available
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
pipe.to(device)
|
| 14 |
+
|
| 15 |
+
# Define a function to generate an image from text
|
| 16 |
+
def generate_image(prompt):
|
| 17 |
+
# Generate the image
|
| 18 |
+
image = pipe(prompt).images[0]
|
| 19 |
+
|
| 20 |
+
# Return the image
|
| 21 |
+
return image
|
| 22 |
+
|
| 23 |
+
# Create a Gradio interface
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=generate_image,
|
| 26 |
+
inputs=[gr.Textbox(label="Text Prompt", placeholder="Enter a text prompt")],
|
| 27 |
+
outputs=[gr.Image(label="Generated Image")],
|
| 28 |
+
title="Text-to-Image AI",
|
| 29 |
+
description="Enter a text prompt to generate an image",
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch the Gradio interface
|
| 33 |
+
demo.launch()
|