Spaces:
Running
on
Zero
Running
on
Zero
Create app-backup.py
Browse files- app-backup.py +171 -0
app-backup.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
import os
|
7 |
+
from diffusers import DiffusionPipeline
|
8 |
+
from transformers import pipeline
|
9 |
+
from huggingface_hub import login
|
10 |
+
|
11 |
+
# Login to Hugging Face Hub with token
|
12 |
+
# You need to set the HF_TOKEN environment variable
|
13 |
+
hf_token = os.getenv("HF_TOKEN")
|
14 |
+
if hf_token:
|
15 |
+
login(token=hf_token)
|
16 |
+
else:
|
17 |
+
print("Warning: HF_TOKEN environment variable not found. Authentication may fail.")
|
18 |
+
|
19 |
+
# Translation pipeline and hardware settings
|
20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device=device)
|
22 |
+
dtype = torch.bfloat16
|
23 |
+
|
24 |
+
# Load the model with token authentication
|
25 |
+
pipe = DiffusionPipeline.from_pretrained(
|
26 |
+
"black-forest-labs/FLUX.1-schnell",
|
27 |
+
torch_dtype=dtype,
|
28 |
+
use_auth_token=hf_token # Use the token for authentication
|
29 |
+
).to(device)
|
30 |
+
|
31 |
+
MAX_SEED = np.iinfo(np.int32).max
|
32 |
+
MAX_IMAGE_SIZE = 2048
|
33 |
+
|
34 |
+
@spaces.GPU()
|
35 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
36 |
+
if randomize_seed:
|
37 |
+
seed = random.randint(0, MAX_SEED)
|
38 |
+
generator = torch.Generator().manual_seed(seed)
|
39 |
+
|
40 |
+
# Korean input detection and translation
|
41 |
+
if any('\uAC00' <= char <= '\uD7A3' for char in prompt):
|
42 |
+
print("Translating Korean prompt...")
|
43 |
+
translated_prompt = translator(prompt, max_length=512)[0]['translation_text']
|
44 |
+
print("Translated prompt:", translated_prompt)
|
45 |
+
prompt = translated_prompt
|
46 |
+
|
47 |
+
image = pipe(
|
48 |
+
prompt = prompt,
|
49 |
+
width = width,
|
50 |
+
height = height,
|
51 |
+
num_inference_steps = num_inference_steps,
|
52 |
+
generator = generator,
|
53 |
+
guidance_scale=0.0
|
54 |
+
).images[0]
|
55 |
+
|
56 |
+
return image, seed
|
57 |
+
|
58 |
+
examples = [
|
59 |
+
["[ํ๊ธ] [์คํ์ผ: ๋ชจ๋] [์์: ๋นจ๊ฐ๊ณผ ๊ฒ์ ] [์ปจ์
: ์๋น] [ํ
์คํธ: '๋ง์๋์ง'] [๋ฐฐ๊ฒฝ: ์ฌํ]"],
|
60 |
+
["[Style: Corporate] [Color: Navy and Silver] [Concept: Finance] [Text: 'TRUST'] [Background: Professional]"],
|
61 |
+
["[Style: Dynamic] [Color: Purple and Orange] [Concept: Creative Agency] [Text: 'SPARK'] [Background: Abstract]"],
|
62 |
+
["[Style: Minimalist] [Color: Red and White] [Concept: Sports] [Text: 'POWER'] [Background: Clean]"]
|
63 |
+
]
|
64 |
+
|
65 |
+
css = """
|
66 |
+
footer {visibility: hidden}
|
67 |
+
.container {max-width: 850px; margin: auto; padding: 20px}
|
68 |
+
.title {text-align: center; margin-bottom: 20px}
|
69 |
+
#prompt {min-height: 50px}
|
70 |
+
#result {min-height: 400px}
|
71 |
+
.gr-box {border-radius: 10px; border: 1px solid #ddd}
|
72 |
+
"""
|
73 |
+
|
74 |
+
|
75 |
+
def create_snow_effect():
|
76 |
+
# CSS ์คํ์ผ ์ ์
|
77 |
+
snow_css = """
|
78 |
+
@keyframes snowfall {
|
79 |
+
0% {
|
80 |
+
transform: translateY(-10vh) translateX(0);
|
81 |
+
opacity: 1;
|
82 |
+
}
|
83 |
+
100% {
|
84 |
+
transform: translateY(100vh) translateX(100px);
|
85 |
+
opacity: 0.3;
|
86 |
+
}
|
87 |
+
}
|
88 |
+
.snowflake {
|
89 |
+
position: fixed;
|
90 |
+
color: white;
|
91 |
+
font-size: 1.5em;
|
92 |
+
user-select: none;
|
93 |
+
z-index: 1000;
|
94 |
+
pointer-events: none;
|
95 |
+
animation: snowfall linear infinite;
|
96 |
+
}
|
97 |
+
"""
|
98 |
+
|
99 |
+
# JavaScript ์ฝ๋ ์ ์
|
100 |
+
snow_js = """
|
101 |
+
function createSnowflake() {
|
102 |
+
const snowflake = document.createElement('div');
|
103 |
+
snowflake.innerHTML = '๐';
|
104 |
+
snowflake.className = 'snowflake';
|
105 |
+
snowflake.style.left = Math.random() * 100 + 'vw';
|
106 |
+
snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';
|
107 |
+
snowflake.style.opacity = Math.random();
|
108 |
+
document.body.appendChild(snowflake);
|
109 |
+
|
110 |
+
setTimeout(() => {
|
111 |
+
snowflake.remove();
|
112 |
+
}, 5000);
|
113 |
+
}
|
114 |
+
setInterval(createSnowflake, 200);
|
115 |
+
"""
|
116 |
+
|
117 |
+
# CSS์ JavaScript๋ฅผ ๊ฒฐํฉํ HTML
|
118 |
+
snow_html = f"""
|
119 |
+
<style>
|
120 |
+
{snow_css}
|
121 |
+
</style>
|
122 |
+
<script>
|
123 |
+
{snow_js}
|
124 |
+
</script>
|
125 |
+
"""
|
126 |
+
|
127 |
+
return gr.HTML(snow_html)
|
128 |
+
|
129 |
+
|
130 |
+
with gr.Blocks(theme="soft", css=css) as demo:
|
131 |
+
create_snow_effect()
|
132 |
+
gr.HTML("<h1 class='title'>LOGO Generator AI</h1>")
|
133 |
+
|
134 |
+
with gr.Column(elem_id="container"):
|
135 |
+
with gr.Group():
|
136 |
+
prompt = gr.Text(
|
137 |
+
label="PROMPT",
|
138 |
+
placeholder="Text input Prompt (Korean input supported)",
|
139 |
+
lines=2
|
140 |
+
)
|
141 |
+
run_button = gr.Button("Generate Logo", variant="primary")
|
142 |
+
|
143 |
+
with gr.Row():
|
144 |
+
result = gr.Image(label="Generated Logo", show_label=True)
|
145 |
+
|
146 |
+
with gr.Accordion("Advanced Settings", open=False):
|
147 |
+
with gr.Row():
|
148 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
149 |
+
randomize_seed = gr.Checkbox(label="Random Seed", value=True)
|
150 |
+
|
151 |
+
with gr.Row():
|
152 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
153 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
154 |
+
num_inference_steps = gr.Slider(label="Quality", minimum=1, maximum=50, step=1, value=4)
|
155 |
+
|
156 |
+
gr.Examples(
|
157 |
+
examples=examples,
|
158 |
+
fn=infer,
|
159 |
+
inputs=[prompt],
|
160 |
+
outputs=[result, seed],
|
161 |
+
cache_examples="lazy"
|
162 |
+
)
|
163 |
+
|
164 |
+
gr.on(
|
165 |
+
triggers=[run_button.click, prompt.submit],
|
166 |
+
fn=infer,
|
167 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
168 |
+
outputs=[result, seed]
|
169 |
+
)
|
170 |
+
|
171 |
+
demo.launch()
|