rapid12k4 commited on
Commit
35da4ff
·
verified ·
1 Parent(s): 1dac7bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableDiffusionPipeline
3
+ from PIL import Image
4
+
5
+ # Load the Stable Diffusion model (replace with your desired model)
6
+ model_id = "runwayml/stable-diffusion-v1-5" # Example: Stable Diffusion v1.5
7
+ pipe = StableDiffusionPipeline.from_pretrained(model_id)
8
+ pipe.to("cuda") # If you have a CUDA-enabled GPU, use it for faster generation
9
+ # otherwise, remove this line to use your CPU (slower)
10
+
11
+ def generate_image(prompt):
12
+ """
13
+ Generates an image from a text prompt using Stable Diffusion.
14
+
15
+ Args:
16
+ prompt (str): The text prompt to guide image generation.
17
+
18
+ Returns:
19
+ PIL.Image.Image: The generated image.
20
+ """
21
+ try:
22
+ image = pipe(prompt).images[0] # Get the first image from the pipeline output
23
+ return image
24
+ except Exception as e:
25
+ print(f"Error generating image: {e}")
26
+ return None # Or return a default image if desired
27
+
28
+
29
+ # Gradio Interface
30
+ def gradio_interface(prompt):
31
+ """
32
+ Generates an image using the Stable Diffusion model and returns it for the Gradio interface.
33
+
34
+ Args:
35
+ prompt (str): The text prompt for image generation.
36
+
37
+ Returns:
38
+ PIL.Image.Image or str: The generated image or an error message.
39
+ """
40
+ image = generate_image(prompt)
41
+ if image:
42
+ return image
43
+ else:
44
+ return "Error: Image generation failed. Please try a different prompt or check the console for details."
45
+
46
+
47
+
48
+ if __name__ == "__main__":
49
+ # Create the Gradio interface
50
+ iface = gr.Interface(
51
+ fn=gradio_interface,
52
+ inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
53
+ outputs=gr.Image(label="Generated Image"),
54
+ title="Text-to-Image Generator",
55
+ description="Enter a text prompt and let the AI generate an image for you. Uses Stable Diffusion (or your specified model).",
56
+ examples=[
57
+ ["A photo of a cat wearing a hat"],
58
+ ["A futuristic cityscape at sunset"],
59
+ ["An abstract painting with vibrant colors"],
60
+ ]
61
+ )
62
+
63
+ # Launch the interface
64
+ iface.launch()