Anurag Bhardwaj commited on
Commit
f2112c3
·
verified ·
1 Parent(s): 7245084

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -52
app.py CHANGED
@@ -1,42 +1,18 @@
1
- import streamlit as st
2
- from PIL import Image
3
  import torch
4
- import numpy as np
5
-
6
- # Import the Diffusers pipeline and a utility for LoRA (adjust as needed)
7
  from diffusers import StableDiffusionImg2ImgPipeline
8
 
9
- # Optional: If you have a helper to apply LoRA weights, import it here.
10
- # For example, if you use lora_diffusion, you might do:
11
- # from lora_diffusion import monkeypatch_lora, tune_lora_scale
12
-
13
- st.set_page_config(page_title="GHIBSKY Art Transformer", layout="centered")
14
-
15
- st.title("GHIBSKY Art Transformer")
16
- st.write(
17
- """
18
- Upload your portrait image and see it transformed into enchanting, Ghibli-inspired art.
19
-
20
- This demo applies the Flux Ghibsky Illustration model locally. The model blends serene, surreal skies
21
- with intricate, Ghibli-inspired details. To invoke the unique aesthetic, the prompt begins with **GHIBSKY style**.
22
-
23
- *Note: This demo caches files only within the session; no data is stored permanently.*
24
- """
25
- )
26
-
27
- @st.cache_resource
28
  def load_model():
29
  """
30
  Load the base Stable Diffusion model and apply the LoRA weights locally.
31
- Replace the placeholder code with your actual LoRA integration as needed.
32
  """
33
- model_id = "runwayml/stable-diffusion-v1-5" # Change if using a different base model.
34
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
35
  model_id,
36
  torch_dtype=torch.float16
37
  )
38
-
39
- # Send model to GPU if available.
40
  device = "cuda" if torch.cuda.is_available() else "cpu"
41
  pipe = pipe.to(device)
42
 
@@ -44,36 +20,47 @@ def load_model():
44
  lora_path = "./flux_ghibsky_lora.safetensors"
45
 
46
  # Apply the LoRA weights to the model.
47
- # The following is a placeholder. Use your actual LoRA integration.
48
- # Example using a hypothetical monkeypatch_lora:
49
  # monkeypatch_lora(pipe.unet, lora_path)
50
  # tune_lora_scale(pipe.unet, 1.0)
51
 
52
- st.write("Model loaded and LoRA weights applied successfully.")
53
  return pipe
54
 
 
55
  pipe = load_model()
56
 
57
- uploaded_file = st.file_uploader("Upload your portrait image", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- if uploaded_file:
60
- # Open and display the original image.
61
- original_image = Image.open(uploaded_file).convert("RGB")
62
- st.image(original_image, caption="Original Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- # Define the prompt with the trigger word.
65
- prompt = ("GHIBSKY style, a portrait transformed into dreamy, Ghibli-inspired art, "
66
- "featuring serene skies, surreal details, and intricate brush strokes")
67
-
68
- # Optional: Adjust strength and number of inference steps as needed.
69
- strength = st.slider("Transformation Strength", 0.1, 0.9, 0.6, step=0.05)
70
- num_inference_steps = st.slider("Inference Steps", 20, 100, 50, step=5)
71
-
72
- st.write("Transforming your image...")
73
-
74
- # Run image-to-image transformation.
75
- # Note: The image is resized internally by the pipeline if needed.
76
- result = pipe(prompt=prompt, image=original_image, strength=strength, num_inference_steps=num_inference_steps)
77
-
78
- transformed_image = result.images[0]
79
- st.image(transformed_image, caption="Ghibli-Inspired Art", use_column_width=True)
 
1
+ import gradio as gr
 
2
  import torch
3
+ from PIL import Image
 
 
4
  from diffusers import StableDiffusionImg2ImgPipeline
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def load_model():
7
  """
8
  Load the base Stable Diffusion model and apply the LoRA weights locally.
9
+ Replace the placeholder code with your actual LoRA integration.
10
  """
11
+ model_id = "runwayml/stable-diffusion-v1-5" # Change to your base model if needed.
12
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
13
  model_id,
14
  torch_dtype=torch.float16
15
  )
 
 
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
  pipe = pipe.to(device)
18
 
 
20
  lora_path = "./flux_ghibsky_lora.safetensors"
21
 
22
  # Apply the LoRA weights to the model.
23
+ # Replace the following placeholder with your actual integration code.
24
+ # For example, if using a helper like monkeypatch_lora:
25
  # monkeypatch_lora(pipe.unet, lora_path)
26
  # tune_lora_scale(pipe.unet, 1.0)
27
 
28
+ print("Model loaded and LoRA weights applied.")
29
  return pipe
30
 
31
+ # Load the model once at startup.
32
  pipe = load_model()
33
 
34
+ def transform_image(image: Image.Image, strength: float, steps: int) -> Image.Image:
35
+ """
36
+ Transforms the uploaded image into Ghibli-inspired art.
37
+ The prompt starts with the trigger word "GHIBSKY style".
38
+ """
39
+ prompt = (
40
+ "GHIBSKY style, a portrait transformed into dreamy, Ghibli-inspired art, "
41
+ "featuring serene skies, surreal details, and intricate brush strokes"
42
+ )
43
+
44
+ result = pipe(prompt=prompt, image=image, strength=strength, num_inference_steps=steps)
45
+ return result.images[0]
46
 
47
+ # Create a Gradio interface.
48
+ demo = gr.Interface(
49
+ fn=transform_image,
50
+ inputs=[
51
+ gr.Image(type="pil", label="Upload your portrait image"),
52
+ gr.Slider(0.1, 0.9, value=0.6, label="Transformation Strength"),
53
+ gr.Slider(20, 100, step=5, value=50, label="Inference Steps")
54
+ ],
55
+ outputs=gr.Image(type="pil", label="Ghibli-Inspired Art"),
56
+ title="GHIBSKY Art Transformer",
57
+ description=(
58
+ "Upload your portrait image and see it transformed into enchanting, Ghibli-inspired art. "
59
+ "This demo uses the Flux Ghibsky Illustration model locally. The prompt is automatically "
60
+ "prefixed with 'GHIBSKY style' to invoke the model's unique aesthetic. "
61
+ "No data is stored permanently."
62
+ )
63
+ )
64
 
65
+ if __name__ == "__main__":
66
+ demo.launch()