nevreal commited on
Commit
216062e
·
verified ·
1 Parent(s): 78f6a44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -62
app.py CHANGED
@@ -1,74 +1,36 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline, DiffusionPipeline
3
- import torch
4
 
5
- # Function to automatically switch between GPU and CPU
6
- def load_model(base_model_id, adapter_model_id):
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
8
- info = f"Running on {'GPU (CUDA) 🔥' if device == 'cuda' else 'CPU 🥶'}"
9
-
10
- try:
11
- # Load the base model dynamically on the correct device
12
- pipe = StableDiffusionPipeline.from_pretrained(
13
- base_model_id,
14
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
15
- ).to(device)
16
-
17
- # If an adapter model is provided, load and merge the adapter model
18
- if adapter_model_id:
19
- adapter_pipe = DiffusionPipeline.from_pretrained(adapter_model_id)
20
- adapter_pipe.load_lora_weights(base_model_id)
21
- pipe = pipe.to(device)
22
 
23
- return pipe, info
24
- except Exception as e:
25
- return None, f"Error loading model: {str(e)}"
26
 
27
- # Function for text-to-image generation
28
- def generate_image(base_model_id, adapter_model_id, prompt):
29
- pipe, info = load_model(base_model_id, adapter_model_id)
30
 
31
- if pipe is None:
32
- return None, info
33
 
34
- # Generate image based on the prompt
35
- try:
36
- image = pipe(prompt).images[0]
37
- return image, info
38
- except Exception as e:
39
- return None, f"Error generating image: {str(e)}"
40
 
41
- # Create the Gradio interface
42
  with gr.Blocks() as demo:
43
- gr.Markdown("## Custom Text-to-Image Generator with Adapter Support")
44
-
 
 
45
  with gr.Row():
46
- with gr.Column():
47
- base_model_id = gr.Textbox(
48
- label="Enter Base Model ID (e.g., CompVis/stable-diffusion-v1-4)",
49
- placeholder="Base Model ID"
50
- )
51
- adapter_model_id = gr.Textbox(
52
- label="Enter Adapter Model ID (optional, e.g., nevreal/vMurderDrones-Lora)",
53
- placeholder="Adapter Model ID (optional)",
54
- value=""
55
- )
56
- prompt = gr.Textbox(
57
- label="Enter your prompt",
58
- placeholder="Describe the image you want to generate"
59
- )
60
- generate_btn = gr.Button("Generate Image")
61
-
62
- with gr.Column():
63
- output_image = gr.Image(label="Generated Image")
64
- device_info = gr.Markdown() # To display device info and any error messages
65
 
66
- # Link the button to the image generation function
67
- generate_btn.click(
68
- fn=generate_image,
69
- inputs=[base_model_id, adapter_model_id, prompt],
70
- outputs=[output_image, device_info]
71
- )
 
 
72
 
73
- # Launch the app
74
  demo.launch()
 
1
  import gradio as gr
2
+ from diffusers import DiffusionPipeline
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
 
 
 
5
 
6
+ # Function to generate image based on input text
7
+ def generate_image(prompt):
8
+ # Load the pipeline
9
 
10
+ pipeline = DiffusionPipeline.from_pretrained("John6666/mala-anime-mix-nsfw-pony-xl-v3-sdxl")
11
+ pipeline.load_lora_weights("nevreal/vMurderDrones")
12
 
13
+ # Generate the image using the text prompt
14
+ image = pipeline(prompt).images[0]
15
+ return image
 
 
 
16
 
17
+ # Create Gradio interface
18
  with gr.Blocks() as demo:
19
+ # Title
20
+ gr.Markdown("# Text-to-Image Generation WebUI")
21
+
22
+ # Input for text prompt
23
  with gr.Row():
24
+ prompt = gr.Textbox(label="Enter your prompt here", placeholder="Type your text prompt...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Output image display
27
+ output_image = gr.Image(label="Generated Image")
28
+
29
+ # Button to trigger the image generation
30
+ generate_button = gr.Button("Generate Image")
31
+
32
+ # When the button is clicked, call the generate_image function
33
+ generate_button.click(fn=generate_image, inputs=prompt, outputs=output_image)
34
 
35
+ # Launch the interface
36
  demo.launch()