Yuukinarak commited on
Commit
730a93e
·
verified ·
1 Parent(s): d5edc78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -40
app.py CHANGED
@@ -4,72 +4,65 @@ import math
4
  from PIL import Image
5
  from diffusers import AutoPipelineForImage2Image
6
 
7
- # ตรวจสอบว่าใช้ GPU (cuda) หรือไม่
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
- pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16) if torch.cuda.is_available() else AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo")
 
 
 
10
  pipe = pipe.to(device)
11
 
12
- # ฟังก์ชันปรับขนาดรูปภาพ
13
  def resize(value, img):
14
  img = Image.open(img)
15
  img = img.resize((value, value))
16
  return img
17
 
18
- # ฟังก์ชันการสร้างภาพ
19
- def infer(source_img, prompt, steps, seed, Strength):
20
- generator = torch.Generator(device).manual_seed(seed)
21
- if int(steps * Strength) < 1:
22
- steps = math.ceil(1 / max(0.10, Strength))
23
  source_image = resize(512, source_img)
24
  source_image.save('source.png')
25
- image = pipe(prompt, image=source_image, strength=Strength, guidance_scale=0.0, num_inference_steps=steps).images[0]
26
  return image
27
 
28
- # ฟังก์ชันอัปเดตข้อความลงในช่อง Prompt Input Textbox
29
- def update_prompt_textbox(image_text, prompt_state):
30
- prompt_state = image_text
31
- return prompt_state
32
 
33
- # สร้าง UI ของ Gradio
34
  with gr.Blocks() as demo:
35
-
36
- # ช่องข้อความที่ใช้รับ Prompt จากผู้ใช้
37
- prompt_input = gr.Textbox(label='Prompt Input Text. 77 Token (Keyword or Symbol) Maximum')
38
-
39
- # ใช้ State เพื่อจัดการค่า dynamic สำหรับ Prompt
40
- prompt_state = gr.State()
41
-
42
  # รูปภาพจากโฟลเดอร์
43
  image1 = gr.Image(value="Img/img1.jpg", label="Bedroom Image")
44
  image2 = gr.Image(value="Img/img2.jpg", label="Modern Image")
45
-
46
  # ปุ่มที่ผู้ใช้จะคลิกเพื่อส่งข้อความ
47
  button1 = gr.Button("Use 'bedroom' Prompt")
48
  button2 = gr.Button("Use 'modern' Prompt")
49
 
50
- # เชื่อมโยงปุ่มกับฟังก์ชันที่ส่งข้อความ
51
- button1.click(fn=update_prompt_textbox, inputs=["bedroom", prompt_state], outputs=prompt_input)
52
- button2.click(fn=update_prompt_textbox, inputs=["modern", prompt_state], outputs=prompt_input)
 
 
 
53
 
54
- # ส่วนประกอบอื่น ๆ ของ Gradio Interface
55
  gr.Interface(
56
  fn=infer,
57
  inputs=[
58
- gr.Image(sources=["upload", "webcam", "clipboard"], type="filepath", label="Raw Image."),
59
- prompt_input, # เชื่อมกับ prompt_input ที่จะถูกอัปเดต
60
  gr.Slider(1, 5, value=2, step=1, label='Number of Iterations'),
61
  gr.Slider(label="Seed", minimum=0, maximum=987654321987654321, step=1, randomize=True),
62
- gr.Slider(label='Strength', minimum=0.1, maximum=1, step=0.05, value=0.5)
63
- ],
64
- outputs='image',
65
- title="Stable Diffusion XL Turbo Image to Image Pipeline CPU",
66
- description=("For more information on Stable Diffusion XL Turbo see "
67
- "https://huggingface.co/stabilityai/sdxl-turbo <br><br>"
68
- "Upload an Image, Use your Cam, or Paste an Image. Then enter a Prompt, or let it just do its "
69
- "Thing, then click submit. For more information about Stable Diffusion or Suggestions for prompts, "
70
- "keywords, artists or styles see https://github.com/Maks-s/sd-akashic"),
71
- article="Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>"
72
- ).queue(max_size=10)
73
 
74
- # เริ่มการทำงานของ UI
75
  demo.launch()
 
4
  from PIL import Image
5
  from diffusers import AutoPipelineForImage2Image
6
 
7
+ # เช็คว่าใช้ GPU (CUDA) หรือไม่
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+
10
+ # โหลดโมเดล Stable Diffusion XL Turbo
11
+ pipe = AutoPipelineForImage2Image.from_pretrained(
12
+ "stabilityai/sdxl-turbo", torch_dtype=torch.float16) if torch.cuda.is_available() else AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo")
13
  pipe = pipe.to(device)
14
 
15
+ # ฟังก์ชันสำหรับการ resize รูปภาพ
16
  def resize(value, img):
17
  img = Image.open(img)
18
  img = img.resize((value, value))
19
  return img
20
 
21
+ # ฟังก์ชันสำหรับ inference
22
+ def infer(source_img, prompt, steps, seed, strength):
23
+ generator = torch.Generator(device).manual_seed(seed)
24
+ if int(steps * strength) < 1:
25
+ steps = math.ceil(1 / max(0.10, strength))
26
  source_image = resize(512, source_img)
27
  source_image.save('source.png')
28
+ image = pipe(prompt, image=source_image, strength=strength, guidance_scale=0.0, num_inference_steps=steps).images[0]
29
  return image
30
 
31
+ # ฟังก์ชันสำหรับการอัปเดต prompt จากการคลิกปุ่ม
32
+ def update_prompt_textbox(prompt_text):
33
+ return gr.update(value=prompt_text)
 
34
 
35
+ # เริ่มต้น context ของ Gradio Blocks
36
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
37
  # รูปภาพจากโฟลเดอร์
38
  image1 = gr.Image(value="Img/img1.jpg", label="Bedroom Image")
39
  image2 = gr.Image(value="Img/img2.jpg", label="Modern Image")
40
+
41
  # ปุ่มที่ผู้ใช้จะคลิกเพื่อส่งข้อความ
42
  button1 = gr.Button("Use 'bedroom' Prompt")
43
  button2 = gr.Button("Use 'modern' Prompt")
44
 
45
+ # ช่องข้อความสำหรับ prompt ที่จะรับค่าจากปุ่ม
46
+ prompt_input = gr.Textbox(label="Prompt Input Text. 77 Token (Keyword or Symbol) Maximum")
47
+
48
+ # การกำหนดการทำงานเมื่อคลิกปุ่ม
49
+ button1.click(fn=update_prompt_textbox, inputs=[], outputs=prompt_input, queue=False, _js="() => 'bedroom'")
50
+ button2.click(fn=update_prompt_textbox, inputs=[], outputs=prompt_input, queue=False, _js="() => 'modern'")
51
 
52
+ # อินเทอร์เฟซสำหรับทำงานร่วมกับโมเดล
53
  gr.Interface(
54
  fn=infer,
55
  inputs=[
56
+ gr.Image(sources=["upload", "webcam", "clipboard"], type="filepath", label="Raw Image."),
57
+ prompt_input,
58
  gr.Slider(1, 5, value=2, step=1, label='Number of Iterations'),
59
  gr.Slider(label="Seed", minimum=0, maximum=987654321987654321, step=1, randomize=True),
60
+ gr.Slider(label='Strength', minimum=0.1, maximum=1, step=.05, value=.5)
61
+ ],
62
+ outputs='image',
63
+ title="Stable Diffusion XL Turbo Image to Image Pipeline",
64
+ description="Click on the images or buttons to send a predefined prompt, then click Submit to generate the image."
65
+ )
 
 
 
 
 
66
 
67
+ # แสดงผลในหน้าเว็บ
68
  demo.launch()