File size: 1,117 Bytes
981397f
395219d
 
 
 
981397f
395219d
 
 
 
 
 
 
981397f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import gradio as gr
from diffusers import StableDiffusionImg2ImgPipeline
import torch
from PIL import Image

# Load model (CPU fallback)
device = "cuda" if torch.cuda.is_available() else "cpu"

pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16 if device == "cuda" else torch.float32
).to(device)

def edit_expression(image, prompt):
    if image is None or prompt.strip() == "":
        return None
    final_prompt = f"The same person but {prompt}"
    result = pipe(
        prompt=final_prompt,
        image=image,
        strength=0.7,
        guidance_scale=7.5
    ).images[0]
    return result

iface = gr.Interface(
    fn=edit_expression,
    inputs=[
        gr.Image(type="pil", label="Upload a face image"),
        gr.Textbox(label="Describe the expression change (e.g., 'smiling', 'angry')")
    ],
    outputs=gr.Image(type="pil", label="Edited Image"),
    title="🪄 Face Expression Editor",
    description="Upload a face photo and change the expression with a text prompt."
)

if __name__ == "__main__":
    iface.launch()