CryptoCreeper commited on
Commit
f42acaf
·
verified ·
1 Parent(s): 56151a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -40
app.py CHANGED
@@ -3,57 +3,36 @@ import torch
3
  import re
4
  import time
5
  from diffusers import DiffusionPipeline
6
- from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
7
 
8
  device = "cpu"
9
  if torch.cuda.is_available():
10
  device = "cuda"
11
 
12
- prompt_enhancer_id = "succinctly/text2image-prompt-generator"
13
- enhancer_tokenizer = GPT2Tokenizer.from_pretrained(prompt_enhancer_id)
14
- enhancer_model = GPT2LMHeadModel.from_pretrained(prompt_enhancer_id)
15
- enhancer_pipe = pipeline("text-generation", model=enhancer_model, tokenizer=enhancer_tokenizer, device=device)
16
 
17
  image_model_id = "SimianLuo/LCM_Dreamshaper_v7"
18
  image_pipe = DiffusionPipeline.from_pretrained(image_model_id)
19
  image_pipe.to(device)
20
 
21
- def clean_and_format_prompt(generated_text, original_prompt):
22
- bad_words = [
23
- "4k", "8k", "high definition", "high res", "high resolution",
24
- "hd", "ultra detailed", "masterpiece", "photorealistic",
25
- "best quality", "vray", "unreal engine", "octane render"
26
- ]
27
-
28
- cleaned = generated_text
29
- instruction_trigger = "Enhanced prompt:"
30
- if instruction_trigger in cleaned:
31
- cleaned = cleaned.split(instruction_trigger)[-1]
32
-
33
- for word in bad_words:
34
- cleaned = re.sub(r'\b' + word + r'\b', "", cleaned, flags=re.IGNORECASE)
35
-
36
- cleaned = re.sub(r',\s*,', ',', cleaned)
37
- cleaned = re.sub(r'\s+', ' ', cleaned).strip().strip(',')
38
-
39
- if len(cleaned) < 5:
40
- cleaned = f"{original_prompt}, detailed, centered in frame"
41
-
42
- return cleaned
43
-
44
  def generate_workflow(prompt, width, height, steps):
45
  start_time = time.time()
46
- yield "🔍 Thinking (analysing AI)...", None, ""
47
 
48
- try:
49
- instructional_prompt = f"Enhance the user prompt so it is suitable for an image generator, and focus on the object, not on the quality, resolution etc. User prompt: {prompt}. Enhanced prompt:"
50
- enhanced_results = enhancer_pipe(instructional_prompt, max_new_tokens=40, num_return_sequences=1)
51
- raw_output = enhanced_results[0]['generated_text']
52
- final_prompt = clean_and_format_prompt(raw_output, prompt)
53
- except:
54
- final_prompt = f"{prompt}, detailed, centered in frame"
 
 
 
55
 
56
- yield "🎨 Generating (Image generator AI)...", None, final_prompt
57
 
58
  image = image_pipe(
59
  prompt=final_prompt,
@@ -68,7 +47,7 @@ def generate_workflow(prompt, width, height, steps):
68
  end_time = time.time()
69
  duration = round(end_time - start_time, 2)
70
 
71
- yield f"✅ Done in {duration}s", image, final_prompt
72
 
73
  with gr.Blocks(theme=gr.themes.Soft(), title="AI Image Lab") as demo:
74
  gr.Markdown("# 🎨 AI Image Lab")
@@ -92,12 +71,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Image Lab") as demo:
92
  with gr.Column(scale=1):
93
  status_bar = gr.Markdown("### Status: **Ready**")
94
  image_output = gr.Image(label="🖼️ Result")
95
- refined_prompt_display = gr.Textbox(label="📝 Enhanced Prompt Used", interactive=False)
96
 
97
  generate_btn.click(
98
  fn=generate_workflow,
99
  inputs=[prompt_input, width_slider, height_slider, steps_slider],
100
- outputs=[status_bar, image_output, refined_prompt_display]
101
  )
102
 
103
  demo.launch()
 
3
  import re
4
  import time
5
  from diffusers import DiffusionPipeline
6
+ # from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
7
 
8
  device = "cpu"
9
  if torch.cuda.is_available():
10
  device = "cuda"
11
 
12
+ # prompt_enhancer_id = "succinctly/text2image-prompt-generator"
13
+ # enhancer_tokenizer = GPT2Tokenizer.from_pretrained(prompt_enhancer_id)
14
+ # enhancer_model = GPT2LMHeadModel.from_pretrained(prompt_enhancer_id)
15
+ # enhancer_pipe = pipeline("text-generation", model=enhancer_model, tokenizer=enhancer_tokenizer, device=device)
16
 
17
  image_model_id = "SimianLuo/LCM_Dreamshaper_v7"
18
  image_pipe = DiffusionPipeline.from_pretrained(image_model_id)
19
  image_pipe.to(device)
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def generate_workflow(prompt, width, height, steps):
22
  start_time = time.time()
 
23
 
24
+ # yield "🔍 Thinking (analysing AI)...", None
25
+
26
+ # try:
27
+ # instructional_prompt = f"Enhance the user prompt... {prompt}"
28
+ # enhanced_results = enhancer_pipe(instructional_prompt, max_new_tokens=40, num_return_sequences=1)
29
+ # final_prompt = clean_and_format_prompt(enhanced_results[0]['generated_text'], prompt)
30
+ # except:
31
+ # final_prompt = f"{prompt}, centered and realistic (if applicable)"
32
+
33
+ final_prompt = f"{prompt}, centered and realistic (if applicable)"
34
 
35
+ yield "🎨 Generating (Image generator AI)...", None
36
 
37
  image = image_pipe(
38
  prompt=final_prompt,
 
47
  end_time = time.time()
48
  duration = round(end_time - start_time, 2)
49
 
50
+ yield f"✅ Done in {duration}s", image
51
 
52
  with gr.Blocks(theme=gr.themes.Soft(), title="AI Image Lab") as demo:
53
  gr.Markdown("# 🎨 AI Image Lab")
 
71
  with gr.Column(scale=1):
72
  status_bar = gr.Markdown("### Status: **Ready**")
73
  image_output = gr.Image(label="🖼️ Result")
74
+ # refined_prompt_display = gr.Textbox(label="📝 Enhanced Prompt Used", interactive=False)
75
 
76
  generate_btn.click(
77
  fn=generate_workflow,
78
  inputs=[prompt_input, width_slider, height_slider, steps_slider],
79
+ outputs=[status_bar, image_output]
80
  )
81
 
82
  demo.launch()