ginipick commited on
Commit
a30ff0a
Β·
verified Β·
1 Parent(s): d4545dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -22
app.py CHANGED
@@ -4,8 +4,18 @@ import random
4
  import spaces
5
  import torch
6
  import time
 
7
  from diffusers import DiffusionPipeline
8
  from custom_pipeline import FLUXPipelineWithIntermediateOutputs
 
 
 
 
 
 
 
 
 
9
 
10
  # Constants
11
  MAX_SEED = np.iinfo(np.int32).max
@@ -17,14 +27,35 @@ DEFAULT_INFERENCE_STEPS = 1
17
  # Device and model setup
18
  dtype = torch.float16
19
  pipe = FLUXPipelineWithIntermediateOutputs.from_pretrained(
20
- "black-forest-labs/FLUX.1-schnell", torch_dtype=dtype
21
  ).to("cuda")
22
  torch.cuda.empty_cache()
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  # Inference function
25
  @spaces.GPU(duration=25)
26
  def generate_image(prompt, seed=42, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False, num_inference_steps=DEFAULT_INFERENCE_STEPS):
27
 
 
 
28
  if randomize_seed:
29
  seed = random.randint(0, MAX_SEED)
30
  generator = torch.Generator().manual_seed(seed)
@@ -40,18 +71,17 @@ def generate_image(prompt, seed=42, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT,
40
  height=height,
41
  generator=generator
42
  ):
43
- latency = f"Latency: {(time.time()-start_time):.2f} seconds"
44
  yield img, seed, latency
45
 
46
-
47
  # Example prompts
48
  examples = [
49
- "a tiny astronaut hatching from an egg on the moon",
50
- "a cat holding a sign that says hello world",
51
- "an anime illustration of a wiener schnitzel",
52
- "a futuristic cityscape with flying cars and neon lights",
53
- "Photo of a young woman with long, wavy brown hair tied in a bun and glasses. She has a fair complexion and is wearing subtle makeup, emphasizing her eyes and lips. She is dressed in a black top. The background appears to be an urban setting with a building facade, and the sunlight casts a warm glow on her face.",
54
- "Imagine steve jobs as Star Wars movie character"
55
  ]
56
 
57
  css = """
@@ -60,37 +90,36 @@ footer {
60
  }
61
  """
62
 
63
-
64
  # --- Gradio UI ---
65
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
66
  with gr.Column(elem_id="app-container"):
67
 
68
  with gr.Row():
69
  with gr.Column(scale=3):
70
- result = gr.Image(label="Generated Image", show_label=False, interactive=False)
71
  with gr.Column(scale=1):
72
  prompt = gr.Text(
73
- label="Prompt",
74
- placeholder="Describe the image you want to generate...",
75
  lines=3,
76
  show_label=False,
77
  container=False,
78
  )
79
- enhanceBtn = gr.Button("πŸš€ Enhance Image")
80
 
81
- with gr.Column("Advanced Options"):
82
  with gr.Row():
83
  latency = gr.Text(show_label=False)
84
  with gr.Row():
85
- seed = gr.Number(label="Seed", value=42, precision=0)
86
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
87
  with gr.Row():
88
- width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
89
- height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
90
- num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=4, step=1, value=DEFAULT_INFERENCE_STEPS)
91
 
92
  with gr.Row():
93
- gr.Markdown("### 🌟 Inspiration Gallery")
94
  with gr.Row():
95
  gr.Examples(
96
  examples=examples,
@@ -122,4 +151,4 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
122
  )
123
 
124
  # Launch the app
125
- demo.launch()
 
4
  import spaces
5
  import torch
6
  import time
7
+ import os
8
  from diffusers import DiffusionPipeline
9
  from custom_pipeline import FLUXPipelineWithIntermediateOutputs
10
+ from transformers import pipeline
11
+
12
+ # Hugging Face 토큰 κ°€μ Έμ˜€κΈ°
13
+ hf_token = os.getenv("HF_TOKEN")
14
+ if not hf_token:
15
+ raise ValueError("HF_TOKEN environment variable is not set. Please set it to your Hugging Face token.")
16
+
17
+ # λ²ˆμ—­ λͺ¨λΈ λ‘œλ“œ (토큰 인증 μΆ”κ°€)
18
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", use_auth_token=hf_token)
19
 
20
  # Constants
21
  MAX_SEED = np.iinfo(np.int32).max
 
27
  # Device and model setup
28
  dtype = torch.float16
29
  pipe = FLUXPipelineWithIntermediateOutputs.from_pretrained(
30
+ "black-forest-labs/FLUX.1-schnell", torch_dtype=dtype, use_auth_token=hf_token
31
  ).to("cuda")
32
  torch.cuda.empty_cache()
33
 
34
+ # ν•œκΈ€ 메뉴 이름 dictionary
35
+ korean_labels = {
36
+ "Generated Image": "μƒμ„±λœ 이미지",
37
+ "Prompt": "ν”„λ‘¬ν”„νŠΈ",
38
+ "Enhance Image": "이미지 ν–₯상",
39
+ "Advanced Options": "κ³ κΈ‰ μ˜΅μ…˜",
40
+ "Seed": "μ‹œλ“œ",
41
+ "Randomize Seed": "μ‹œλ“œ λ¬΄μž‘μœ„ν™”",
42
+ "Width": "λ„ˆλΉ„",
43
+ "Height": "높이",
44
+ "Inference Steps": "μΆ”λ‘  단계",
45
+ "Inspiration Gallery": "영감 가러리"
46
+ }
47
+
48
+ def translate_if_korean(text):
49
+ if any('\u3131' <= char <= '\u3163' or '\uac00' <= char <= '\ud7a3' for char in text):
50
+ return translator(text, use_auth_token=hf_token)[0]['translation_text']
51
+ return text
52
+
53
  # Inference function
54
  @spaces.GPU(duration=25)
55
  def generate_image(prompt, seed=42, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False, num_inference_steps=DEFAULT_INFERENCE_STEPS):
56
 
57
+ prompt = translate_if_korean(prompt)
58
+
59
  if randomize_seed:
60
  seed = random.randint(0, MAX_SEED)
61
  generator = torch.Generator().manual_seed(seed)
 
71
  height=height,
72
  generator=generator
73
  ):
74
+ latency = f"처리 μ‹œκ°„: {(time.time()-start_time):.2f} 초"
75
  yield img, seed, latency
76
 
 
77
  # Example prompts
78
  examples = [
79
+ "λ‹¬μ—μ„œ μ•Œμ—μ„œ λΆ€ν™”ν•˜λŠ” μž‘μ€ 우주 비행사",
80
+ "μ•ˆλ…•ν•˜μ„Έμš” 세상이라고 쓰인 ν‘œμ§€νŒμ„ λ“€κ³  μžˆλŠ” 고양이",
81
+ "λΉ„λ„ˆ μŠˆλ‹ˆμ²Όμ˜ μ• λ‹ˆλ©”μ΄μ…˜ μΌλŸ¬μŠ€νŠΈλ ˆμ΄μ…˜",
82
+ "ν•˜λŠ˜μ„ λ‚˜λŠ” μžλ™μ°¨μ™€ λ„€μ˜¨ λΆˆλΉ›μ΄ μžˆλŠ” 미래적인 λ„μ‹œ 풍경",
83
+ "κΈ΄ κ°ˆμƒ‰ μ›¨μ΄λΈŒ 머리λ₯Ό 올렀 λ¬Άκ³  μ•ˆκ²½μ„ μ“΄ μ Šμ€ μ—¬μ„±μ˜ 사진. κ·Έλ…€λŠ” 흰 피뢀에 눈과 μž…μˆ μ„ κ°•μ‘°ν•œ μ€μ€ν•œ ν™”μž₯을 ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλ…€λŠ” 검은색 μƒμ˜λ₯Ό μž…μ—ˆμŠ΅λ‹ˆλ‹€. 배경은 λ„μ‹œ 건물 μ™Έκ΄€μœΌλ‘œ 보이며, 햇빛이 κ·Έλ…€μ˜ 얼꡴에 λ”°λœ»ν•œ 빛을 λΉ„μΆ”κ³  μžˆμŠ΅λ‹ˆλ‹€.",
84
+ "μŠ€ν‹°λΈŒ 작슀λ₯Ό μŠ€νƒ€μ›Œμ¦ˆ μ˜ν™” μΊλ¦­ν„°λ‘œ μƒμƒν•΄λ³΄μ„Έμš”"
85
  ]
86
 
87
  css = """
 
90
  }
91
  """
92
 
 
93
  # --- Gradio UI ---
94
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
95
  with gr.Column(elem_id="app-container"):
96
 
97
  with gr.Row():
98
  with gr.Column(scale=3):
99
+ result = gr.Image(label=korean_labels["Generated Image"], show_label=False, interactive=False)
100
  with gr.Column(scale=1):
101
  prompt = gr.Text(
102
+ label=korean_labels["Prompt"],
103
+ placeholder="μƒμ„±ν•˜κ³  싢은 이미지λ₯Ό μ„€λͺ…ν•˜μ„Έμš”...",
104
  lines=3,
105
  show_label=False,
106
  container=False,
107
  )
108
+ enhanceBtn = gr.Button(f"πŸš€ {korean_labels['Enhance Image']}")
109
 
110
+ with gr.Column(korean_labels["Advanced Options"]):
111
  with gr.Row():
112
  latency = gr.Text(show_label=False)
113
  with gr.Row():
114
+ seed = gr.Number(label=korean_labels["Seed"], value=42, precision=0)
115
+ randomize_seed = gr.Checkbox(label=korean_labels["Randomize Seed"], value=False)
116
  with gr.Row():
117
+ width = gr.Slider(label=korean_labels["Width"], minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
118
+ height = gr.Slider(label=korean_labels["Height"], minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
119
+ num_inference_steps = gr.Slider(label=korean_labels["Inference Steps"], minimum=1, maximum=4, step=1, value=DEFAULT_INFERENCE_STEPS)
120
 
121
  with gr.Row():
122
+ gr.Markdown(f"### 🌟 {korean_labels['Inspiration Gallery']}")
123
  with gr.Row():
124
  gr.Examples(
125
  examples=examples,
 
151
  )
152
 
153
  # Launch the app
154
+ demo.launch()