Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
+
from cog_sdxl.dataset_and_utils import TokenEmbeddingsHandler
|
6 |
+
|
7 |
+
# Load the model pipeline
|
8 |
+
pipe = DiffusionPipeline.from_pretrained(
|
9 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
10 |
+
torch_dtype=torch.float16,
|
11 |
+
variant="fp16",
|
12 |
+
).to("cuda")
|
13 |
+
|
14 |
+
pipe.load_lora_weights("fofr/sdxl-emoji", weight_name="lora.safetensors")
|
15 |
+
|
16 |
+
# Load token embeddings
|
17 |
+
embedding_path = hf_hub_download(repo_id="fofr/sdxl-emoji", filename="embeddings.pti", repo_type="model")
|
18 |
+
text_encoders = [pipe.text_encoder, pipe.text_encoder_2]
|
19 |
+
tokenizers = [pipe.tokenizer, pipe.tokenizer_2]
|
20 |
+
embhandler = TokenEmbeddingsHandler(text_encoders, tokenizers)
|
21 |
+
embhandler.load_embeddings(embedding_path)
|
22 |
+
|
23 |
+
def generate_emoji(prompt):
|
24 |
+
"""Generate an emoji image based on the user's prompt."""
|
25 |
+
prompt = f"A <s0><s1> emoji of {prompt}"
|
26 |
+
images = pipe(
|
27 |
+
prompt,
|
28 |
+
cross_attention_kwargs={"scale": 0.8},
|
29 |
+
).images
|
30 |
+
return images[0]
|
31 |
+
|
32 |
+
# Gradio UI definition
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=generate_emoji,
|
35 |
+
inputs=gr.Textbox(label="Enter description for emoji"),
|
36 |
+
outputs=gr.Image(label="Generated Emoji"),
|
37 |
+
title="SDXL Emoji Generator",
|
38 |
+
description="Generate a custom emoji using SDXL model with LoRA fine-tuning."
|
39 |
+
)
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
iface.launch()
|