jitubutwal1441 commited on
Commit
1d96670
·
verified ·
1 Parent(s): c3d46a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -13
app.py CHANGED
@@ -3,13 +3,41 @@ import torch
3
  import io
4
  import smtplib
5
  from email.message import EmailMessage
6
- from diffusers import StableDiffusionImg2ImgPipeline
7
 
8
- # Load SD3.5 pipeline
9
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
10
- "stabilityai/stable-diffusion-3.5-large", torch_dtype=torch.bfloat16
 
 
 
 
 
11
  ).to("cuda")
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  style_prompts = {
14
  "Single Photo With Dhaka Topi - Ghibli Style": "Create a Ghibli-style portrait using the uploaded photo as the person’s face to get a magical Ghibli-style portrait, wearing a traditional Nepali Dhaka topi. The dreamy background features Mount Everest, blooming laliguras flowers, Kathmandu’s Ghanta Ghar tower, elegant Newari window patterns, and colorful Nepali prayer flags waving in the wind. The art should blend Studio Ghibli’s enchanting style with authentic Nepali culture.",
15
  "Two Person Picture With Nepali Style Ghibli Art": "Create a Ghibli-style portrait using the uploaded photo as the person’s face. Background features Mount Everest, laliguras flowers, Kathmandu’s Ghanta Ghar tower, Newari window patterns, colorful Nepali prayer flags, the Nepali flag, Pashupatinath temple, and a flying Himalayan monal (Danfe). Make it vibrant, enchanting, and full of Nepali cultural details.",
@@ -20,18 +48,11 @@ style_prompts = {
20
  }
21
  sample_keys = list(style_prompts.keys())[:5]
22
 
23
- def generate_ghibli_style(image, prompt, strength=0.6):
24
- with torch.inference_mode():
25
- out = pipe(prompt=prompt,
26
- image=image,
27
- strength=strength,
28
- guidance_scale=6.5,
29
- num_inference_steps=25).images[0]
30
- return out
31
 
32
  def process(image, style, email):
33
  prompt = style_prompts.get(style, "")
34
- result = generate_ghibli_style(image, prompt)
35
  email_status = ""
36
  if email:
37
  email_status = send_email(email, result)
 
3
  import io
4
  import smtplib
5
  from email.message import EmailMessage
6
+ from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
7
 
8
+ from diffusers.utils import load_image
9
+ from PIL import Image
10
+
11
+
12
+ # Load the base (text-to-image) model
13
+ pipe_t2i = StableDiffusionXLPipeline.from_pretrained(
14
+ "stabilityai/stable-diffusion-xl-base-1.0",
15
+ torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
16
  ).to("cuda")
17
 
18
+ # Use from_pipe to initialize img2img from the same base
19
+ pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pipe(pipe_t2i).to("cuda")
20
+
21
+ def generate_from_image(image: Image.Image, prompt: str,
22
+ strength: float = 0.75,
23
+ guidance_scale: float = 10.5,
24
+ steps: int = 40):
25
+ """
26
+ Generates a new SDXL image conditioned on both prompt and input image.
27
+ """
28
+ print(f"Prompt: {prompt}, Strength: {strength}, Guidance: {guidance_scale}")
29
+
30
+ image = image.convert("RGB").resize((1024, 1024))
31
+ with torch.inference_mode():
32
+ output = pipe_i2i(
33
+ prompt=prompt,
34
+ image=image,
35
+ strength=strength,
36
+ guidance_scale=guidance_scale,
37
+ num_inference_steps=steps
38
+ ).images[0]
39
+ return output
40
+
41
  style_prompts = {
42
  "Single Photo With Dhaka Topi - Ghibli Style": "Create a Ghibli-style portrait using the uploaded photo as the person’s face to get a magical Ghibli-style portrait, wearing a traditional Nepali Dhaka topi. The dreamy background features Mount Everest, blooming laliguras flowers, Kathmandu’s Ghanta Ghar tower, elegant Newari window patterns, and colorful Nepali prayer flags waving in the wind. The art should blend Studio Ghibli’s enchanting style with authentic Nepali culture.",
43
  "Two Person Picture With Nepali Style Ghibli Art": "Create a Ghibli-style portrait using the uploaded photo as the person’s face. Background features Mount Everest, laliguras flowers, Kathmandu’s Ghanta Ghar tower, Newari window patterns, colorful Nepali prayer flags, the Nepali flag, Pashupatinath temple, and a flying Himalayan monal (Danfe). Make it vibrant, enchanting, and full of Nepali cultural details.",
 
48
  }
49
  sample_keys = list(style_prompts.keys())[:5]
50
 
51
+
 
 
 
 
 
 
 
52
 
53
  def process(image, style, email):
54
  prompt = style_prompts.get(style, "")
55
+ result = generate_from_image(image, prompt)
56
  email_status = ""
57
  if email:
58
  email_status = send_email(email, result)