hazem3344 commited on
Commit
4a4ad35
·
verified ·
1 Parent(s): 60e8df8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -24
app.py CHANGED
@@ -1,24 +1,47 @@
1
- import gradio as gr
2
- from diffusers import DiffusionPipeline
3
-
4
- # Load the pre-trained model pipeline
5
- pipe = DiffusionPipeline.from_pretrained("ImageInception/ArtifyAI")
6
-
7
- # Define a function to generate the image based on user input text
8
- def generate_image(prompt):
9
- # Generate the image from the prompt
10
- image = pipe(prompt).images[0]
11
- return image
12
-
13
- # Define the Gradio interface
14
- iface = gr.Interface(
15
- fn=generate_image,
16
- inputs="text",
17
- outputs="image",
18
- title="ArtifyAI Generator",
19
- description="Enter a prompt and generate an image using the ArtifyAI model."
20
- )
21
-
22
- # Launch the Gradio app
23
- if __name__ == "__main__":
24
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import DiffusionPipeline
3
+ import torch
4
+
5
+ device = "cuda" if torch.cuda.is_available() else "cpu"
6
+
7
+ # Pre-load the models but do not load them yet
8
+ models = {
9
+ "ArtifyAI v1.1": "ImageInception/ArtifyAI-v1.1",
10
+ "ArtifyAI v1.0": "ImageInception/ArtifyAI-v1.0"
11
+ }
12
+
13
+ # Function to load the selected model
14
+ def load_model(model_name):
15
+ return DiffusionPipeline.from_pretrained(models[model_name], torch_dtype=torch.float16 if device == "cuda" else torch.float32).to(device)
16
+
17
+ # Initially load the first model
18
+ pipe = load_model("ArtifyAI v1.1")
19
+
20
+ def generate_image(prompt, selected_model):
21
+ global pipe
22
+
23
+ # Load the selected model if it's not already loaded
24
+ if selected_model in models:
25
+ pipe = load_model(selected_model)
26
+
27
+ if device == "cuda":
28
+ with torch.cuda.amp.autocast():
29
+ image = pipe(prompt).images[0]
30
+ else:
31
+ image = pipe(prompt).images[0]
32
+ return image
33
+
34
+ # Gradio interface
35
+ demo = gr.Interface(
36
+ fn=generate_image,
37
+ inputs=[
38
+ gr.Textbox(label="Enter your prompt"),
39
+ gr.Dropdown(choices=list(models.keys()), label="Select Model", value="ArtifyAI v1.1")
40
+ ],
41
+ outputs=gr.Image(type="pil"),
42
+ title="ArtifyAI",
43
+ description="Generate images using the selected model."
44
+ )
45
+
46
+ if __name__ == "__main__":
47
+ demo.launch()