Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,10 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionPipeline
|
4 |
|
@@ -10,18 +16,82 @@ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float
|
|
10 |
pipe.to(device)
|
11 |
pipe.enable_attention_slicing() # Optimize memory usage
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
with torch.inference_mode(): # Disables gradient calculations for faster inference
|
16 |
result = pipe(prompt, image=image, strength=0.6, guidance_scale=6.5, num_inference_steps=25).images[0] # Reduced steps & optimized scale
|
17 |
return result
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import smtplib
|
4 |
+
from email.message import EmailMessage
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
|
8 |
import torch
|
9 |
from diffusers import StableDiffusionPipeline
|
10 |
|
|
|
16 |
pipe.to(device)
|
17 |
pipe.enable_attention_slicing() # Optimize memory usage
|
18 |
|
19 |
+
BACKEND_URL = "https://your-hf-space-url.hf.space/ghibli"
|
20 |
+
|
21 |
+
style_prompts = {
|
22 |
+
"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.",
|
23 |
+
"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.",
|
24 |
+
"Spirited Away Fantasy": "Studio Ghibli style portrait with fantasy elements, mysterious background, dreamy lighting, spirited away aesthetic",
|
25 |
+
"Kiki's Cozy Town": "A warm, cozy background with bakery and rooftops, Studio Ghibli anime style, charming and colorful",
|
26 |
+
"Princess Mononoke Wild": "Forest warrior aesthetic, powerful gaze, nature spirit background, strong composition, Ghibli anime style",
|
27 |
+
"Ghibli Vintage Portrait": "Classic Ghibli portrait, soft facial features, minimal background, vintage anime color palette, watercolor ink"
|
28 |
+
}
|
29 |
+
|
30 |
+
def send_to_backend(image, prompt):
|
31 |
+
buffered = io.BytesIO()
|
32 |
+
image.save(buffered, format="PNG")
|
33 |
+
response = requests.post(
|
34 |
+
BACKEND_URL,
|
35 |
+
files={"file": ("upload.png", buffered.getvalue(), "image/png")},
|
36 |
+
data={"prompt": prompt}
|
37 |
+
)
|
38 |
+
return Image.open(io.BytesIO(response.content))
|
39 |
+
|
40 |
+
def generate_image(image, prompt):
|
41 |
+
return f"Generated Ghibli style image"
|
42 |
+
|
43 |
+
def generate_ghibli_style(image, prompt):
|
44 |
+
print(f"Prompt: {prompt}")
|
45 |
with torch.inference_mode(): # Disables gradient calculations for faster inference
|
46 |
result = pipe(prompt, image=image, strength=0.6, guidance_scale=6.5, num_inference_steps=25).images[0] # Reduced steps & optimized scale
|
47 |
return result
|
48 |
|
49 |
+
def send_email(email, image):
|
50 |
+
msg = EmailMessage()
|
51 |
+
msg["Subject"] = "Your Ghibli-style Portrait"
|
52 |
+
msg["From"] = "your@email.com"
|
53 |
+
msg["To"] = email
|
54 |
+
buf = io.BytesIO()
|
55 |
+
image.save(buf, format="PNG")
|
56 |
+
buf.seek(0)
|
57 |
+
msg.add_attachment(buf.read(), maintype='image', subtype='png', filename='portrait.png')
|
58 |
+
|
59 |
+
with smtplib.SMTP_SSL("smtp.example.com", 465) as server:
|
60 |
+
server.login("[email protected]", "yourpassword")
|
61 |
+
server.send_message(msg)
|
62 |
+
return "✅ Sent to email!"
|
63 |
+
|
64 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
65 |
+
gr.Markdown(
|
66 |
+
"""
|
67 |
+
# Nepali Roots Ghibli Art Booth
|
68 |
+
Welcome to the magical world of Studio Ghibli!
|
69 |
+
Take a photo or upload one, select your art style, and transform into a dreamy anime character ✨
|
70 |
+
"""
|
71 |
+
)
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column():
|
75 |
+
style_choice = gr.Dropdown(
|
76 |
+
choices=list(style_prompts.keys()),
|
77 |
+
value="Totoro Forest Dream",
|
78 |
+
label="🎨 Choose a Ghibli Style"
|
79 |
+
)
|
80 |
+
# image_input = gr.Image(sources=["webcam"], type="pil", label="📸 Take or Upload Image")
|
81 |
+
image_input = gr.Image(sources=["upload"], type="pil", label="📸 Take or Upload Image")
|
82 |
+
email_input = gr.Textbox(label="📧 Enter your email to receive your portrait")
|
83 |
+
send_button = gr.Button("🎆 Generate")
|
84 |
+
with gr.Column():
|
85 |
+
image_output = gr.Image(label="🌠 Ghibli-Styled Result")
|
86 |
+
status = gr.Textbox(label="Status")
|
87 |
+
|
88 |
+
def process(image, style, email):
|
89 |
+
prompt = style_prompts.get(style, list(style_prompts.values())[0])
|
90 |
+
# result = send_to_backend(image, prompt)
|
91 |
+
result = generate_ghibli_style(image, prompt)
|
92 |
+
# send_email(email, result)
|
93 |
+
return result, "✅ Portrait generated"
|
94 |
+
|
95 |
+
send_button.click(fn=process, inputs=[image_input, style_choice, email_input], outputs=[image_output, status])
|
96 |
|
97 |
+
demo.launch()
|