Khunanya commited on
Commit
2bd71da
·
verified ·
1 Parent(s): 6a830ba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableDiffusionImg2ImgPipeline
3
+ import torch
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ # โหลดโมเดล Stable Diffusion สำหรับ img2img
8
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
9
+ "runwayml/stable-diffusion-v1-5",
10
+ torch_dtype=torch.float16
11
+ ).to("cuda")
12
+
13
+ def generate(prompt, image, strength=0.75, guidance=7.5):
14
+ init_image = Image.fromarray(np.array(image)).convert("RGB").resize((512, 512))
15
+ result = pipe(prompt=prompt, image=init_image, strength=strength, guidance_scale=guidance).images[0]
16
+ return result
17
+
18
+ # UI Gradio
19
+ gr.Interface(
20
+ fn=generate,
21
+ inputs=[
22
+ gr.Textbox(label="Prompt", placeholder="Describe the new style or concept"),
23
+ gr.Image(label="Input Image"),
24
+ gr.Slider(0.2, 1.0, value=0.75, label="Strength"),
25
+ gr.Slider(1, 15, value=7.5, label="Guidance Scale")
26
+ ],
27
+ outputs="image",
28
+ title="Stable Diffusion Image-to-Image",
29
+ description="แปลงภาพต้นฉบับโดยอิงจากคำอธิบาย prompt และภาพต้นทาง (ใช้ Stable Diffusion)"
30
+ ).launch()