CryptoCreeper commited on
Commit
e105ecd
·
verified ·
1 Parent(s): bd7107d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -21
app.py CHANGED
@@ -1,19 +1,31 @@
1
  import gradio as gr
2
  import torch
3
  from diffusers import DiffusionPipeline
 
4
 
5
  device = "cpu"
6
  if torch.cuda.is_available():
7
  device = "cuda"
8
 
9
- model_id = "SimianLuo/LCM_Dreamshaper_v7"
 
 
 
10
 
11
- pipe = DiffusionPipeline.from_pretrained(model_id)
12
- pipe.to(device)
 
13
 
14
- def generate_image(prompt, width, height, steps):
15
- result = pipe(
16
- prompt=prompt,
 
 
 
 
 
 
 
17
  width=int(width),
18
  height=int(height),
19
  num_inference_steps=int(steps),
@@ -21,30 +33,37 @@ def generate_image(prompt, width, height, steps):
21
  lcm_origin_steps=50,
22
  output_type="pil"
23
  ).images[0]
24
- return result
 
25
 
26
- with gr.Blocks() as demo:
27
- gr.Markdown("## LCM Image Generator (CPU Optimized)")
28
 
29
  with gr.Row():
30
- with gr.Column():
31
- prompt_input = gr.Textbox(label="Prompt", placeholder="A futuristic city with neon lights...")
 
 
 
 
32
 
33
  with gr.Row():
34
- width_slider = gr.Slider(minimum=256, maximum=768, step=64, value=512, label="Width")
35
- height_slider = gr.Slider(minimum=256, maximum=768, step=64, value=512, label="Height")
 
 
36
 
37
- steps_slider = gr.Slider(minimum=1, maximum=15, step=1, value=4, label="Inference Steps (Quality)")
38
 
39
- generate_btn = gr.Button("Generate Image")
40
-
41
- with gr.Column():
42
- image_output = gr.Image(label="Generated Image")
43
 
44
  generate_btn.click(
45
- fn=generate_image,
46
  inputs=[prompt_input, width_slider, height_slider, steps_slider],
47
- outputs=image_output
48
  )
49
 
50
- demo.launch()
 
1
  import gradio as gr
2
  import torch
3
  from diffusers import DiffusionPipeline
4
+ from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
5
 
6
  device = "cpu"
7
  if torch.cuda.is_available():
8
  device = "cuda"
9
 
10
+ prompt_enhancer_id = "succinctly/text2image-prompt-generator"
11
+ enhancer_tokenizer = GPT2Tokenizer.from_pretrained(prompt_enhancer_id)
12
+ enhancer_model = GPT2LMHeadModel.from_pretrained(prompt_enhancer_id)
13
+ enhancer_pipe = pipeline("text-generation", model=enhancer_model, tokenizer=enhancer_tokenizer, device=device)
14
 
15
+ image_model_id = "SimianLuo/LCM_Dreamshaper_v7"
16
+ image_pipe = DiffusionPipeline.from_pretrained(image_model_id)
17
+ image_pipe.to(device)
18
 
19
+ def generate_workflow(prompt, width, height, steps):
20
+ yield "Thinking (analysing AI)...", None, ""
21
+
22
+ enhanced_results = enhancer_pipe(prompt, max_length=75, num_return_sequences=1)
23
+ refined_prompt = enhanced_results[0]['generated_text']
24
+
25
+ yield "Generating (Image generator AI)...", None, refined_prompt
26
+
27
+ image = image_pipe(
28
+ prompt=refined_prompt,
29
  width=int(width),
30
  height=int(height),
31
  num_inference_steps=int(steps),
 
33
  lcm_origin_steps=50,
34
  output_type="pil"
35
  ).images[0]
36
+
37
+ yield "Ready", image, refined_prompt
38
 
39
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
40
+ gr.Markdown("# AI Image Lab")
41
 
42
  with gr.Row():
43
+ with gr.Column(scale=1):
44
+ prompt_input = gr.Textbox(
45
+ label="Your Idea",
46
+ placeholder="e.g., a lonely robot on mars",
47
+ lines=3
48
+ )
49
 
50
  with gr.Row():
51
+ width_slider = gr.Slider(256, 768, 512, step=64, label="Width")
52
+ height_slider = gr.Slider(256, 768, 512, step=64, label="Height")
53
+
54
+ steps_slider = gr.Slider(1, 15, 4, step=1, label="Inference Steps")
55
 
56
+ generate_btn = gr.Button("Generate", variant="primary")
57
 
58
+ with gr.Column(scale=1):
59
+ status_bar = gr.Markdown("### Status: **Ready**")
60
+ image_output = gr.Image(label="Result")
61
+ refined_prompt_display = gr.Textbox(label="Enhanced Prompt Used", interactive=False)
62
 
63
  generate_btn.click(
64
+ fn=generate_workflow,
65
  inputs=[prompt_input, width_slider, height_slider, steps_slider],
66
+ outputs=[status_bar, image_output, refined_prompt_display]
67
  )
68
 
69
+ demo.launch()