aiqcamp commited on
Commit
6742856
Β·
verified Β·
1 Parent(s): 708b543

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -76
app.py CHANGED
@@ -1,82 +1,32 @@
1
- import gradio as gr
2
- from gradio_client import Client
3
  import os
4
- from dotenv import load_dotenv
 
 
 
 
 
 
5
  import warnings
6
 
7
  # κ²½κ³  λ©”μ‹œμ§€ 숨기기
8
  warnings.filterwarnings('ignore', category=UserWarning)
9
 
10
- # ν™˜κ²½ λ³€μˆ˜ λ‘œλ“œ
11
- load_dotenv()
12
- HF_TOKEN = os.getenv("HF_TOKEN")
 
13
 
14
- def generate_diagram(prompt, width=1024, height=1024):
15
- """FLUX AIλ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μ΄μ–΄κ·Έλž¨ 생성"""
16
- try:
17
- # API ν˜ΈμΆœμ„ 톡해 이미지 생성
18
- result = gr.Image.update(value=None) # 초기 μƒνƒœλŠ” 빈 이미지
19
-
20
- # Hugging Face API 호좜
21
- client = Client(
22
- "https://black-forest-labs-flux-1-schnell.hf.space", # API μ—”λ“œν¬μΈνŠΈ 직접 지정
23
- hf_token=HF_TOKEN,
24
- )
25
-
26
- # 이미지 생성
27
- result = client.predict(
28
- prompt,
29
- 1872187377, # seed
30
- False, # randomize_seed
31
- width,
32
- height,
33
- 4, # num_inference_steps
34
- api_name="/infer"
35
- )
36
- return result
37
- except Exception as e:
38
- raise gr.Error(f"λ‹€μ΄μ–΄κ·Έλž¨ 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
39
 
40
- # Convert example format for Gradio
41
- GRADIO_EXAMPLES = [
42
- [
43
- """A handrawn colorful mind map diagram, educational style, vibrant colors, clear hierarchy.
44
- KNOWLEDGE
45
- β”œβ”€β”€ ACQUISITION [Brain with Lightning ~60px]
46
- β”‚ β”œβ”€β”€ READING [Open Book with Glow]
47
- β”‚ └── PRACTICE [Hands-on Tools]
48
- └── APPLICATION
49
- β”œβ”€β”€ CREATION [Artist Palette]
50
- └── INNOVATION [Lightbulb]""",
51
- 1024,
52
- 1024
53
- ],
54
- [
55
- """A handrawn colorful mind map diagram, tech-focused style, neon accents.
56
- DIGITAL TRANSFORM
57
- β”œβ”€β”€ CLOUD [Cloud with Data ~55px]
58
- β”‚ β”œβ”€β”€ STORAGE [Database]
59
- β”‚ └── COMPUTING [Server]
60
- └── SECURITY
61
- β”œβ”€β”€ ENCRYPTION [Lock]
62
- └── MONITORING [Radar]""",
63
- 1024,
64
- 1024
65
- ],
66
- [
67
- """A handrawn colorful mind map diagram, creative style, flowing design.
68
- INNOVATION
69
- β”œβ”€β”€ IDEATION [Lightbulb ~60px]
70
- β”‚ β”œβ”€β”€ RESEARCH [Magnifier]
71
- β”‚ └── BRAINSTORM [Brain]
72
- └── EXECUTION
73
- β”œβ”€β”€ PROTOTYPE [Tools]
74
- └── TEST [Checklist]""",
75
- 1024,
76
- 1024
77
- ]
78
- ]
79
 
 
 
80
 
81
  # Enhanced examples with more detailed prompts and specific styling
82
  EXAMPLES = [
@@ -273,15 +223,41 @@ EXAMPLES = [
273
  }
274
  ]
275
 
276
-
277
-
278
  # Convert examples to Gradio format
279
  GRADIO_EXAMPLES = [
280
  [example["prompt"], example["width"], example["height"]]
281
  for example in EXAMPLES
282
  ]
283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
 
285
  demo = gr.Interface(
286
  fn=generate_diagram,
287
  inputs=[
@@ -293,14 +269,14 @@ demo = gr.Interface(
293
  gr.Slider(
294
  label="λ„ˆλΉ„",
295
  minimum=512,
296
- maximum=2048,
297
  step=128,
298
  value=1024
299
  ),
300
  gr.Slider(
301
  label="높이",
302
  minimum=512,
303
- maximum=2048,
304
  step=128,
305
  value=1024
306
  )
@@ -319,9 +295,9 @@ demo = gr.Interface(
319
  cache_examples=True
320
  )
321
 
322
- # μ•± μ‹€ν–‰ λΆ€λΆ„λ§Œ μˆ˜μ •
323
  if __name__ == "__main__":
324
- demo.queue() # 큐 κΈ°λŠ₯을 λ³„λ„λ‘œ ν™œμ„±ν™”
325
  demo.launch(
326
  server_name="0.0.0.0",
327
  server_port=7860,
 
1
+ import random
 
2
  import os
3
+ import uuid
4
+ from datetime import datetime
5
+ import gradio as gr
6
+ import numpy as np
7
+ import torch
8
+ from diffusers import DiffusionPipeline
9
+ from PIL import Image
10
  import warnings
11
 
12
  # κ²½κ³  λ©”μ‹œμ§€ 숨기기
13
  warnings.filterwarnings('ignore', category=UserWarning)
14
 
15
+ # μ €μž₯ 디렉토리 생성
16
+ SAVE_DIR = "saved_images"
17
+ if not os.path.exists(SAVE_DIR):
18
+ os.makedirs(SAVE_DIR, exist_ok=True)
19
 
20
+ # μž₯치 μ„€μ •
21
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # λͺ¨λΈ λ‘œλ“œ
24
+ repo_id = "black-forest-labs/FLUX.1-schnell"
25
+ pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float32)
26
+ pipeline = pipeline.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ MAX_SEED = np.iinfo(np.int32).max
29
+ MAX_IMAGE_SIZE = 2048
30
 
31
  # Enhanced examples with more detailed prompts and specific styling
32
  EXAMPLES = [
 
223
  }
224
  ]
225
 
 
 
226
  # Convert examples to Gradio format
227
  GRADIO_EXAMPLES = [
228
  [example["prompt"], example["width"], example["height"]]
229
  for example in EXAMPLES
230
  ]
231
 
232
+ def generate_diagram(prompt, width=1024, height=1024):
233
+ """FLUX AIλ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μ΄μ–΄κ·Έλž¨ 생성"""
234
+ try:
235
+ # μ‹œλ“œ μ„€μ •
236
+ seed = random.randint(0, MAX_SEED)
237
+ generator = torch.Generator(device=device).manual_seed(seed)
238
+
239
+ # 이미지 생성
240
+ image = pipeline(
241
+ prompt=prompt,
242
+ width=width,
243
+ height=height,
244
+ num_inference_steps=4,
245
+ generator=generator,
246
+ ).images[0]
247
+
248
+ # 이미지 μ €μž₯
249
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
250
+ unique_id = str(uuid.uuid4())[:8]
251
+ filename = f"diagram_{timestamp}_{unique_id}.png"
252
+ save_path = os.path.join(SAVE_DIR, filename)
253
+ image.save(save_path)
254
+
255
+ return image
256
+
257
+ except Exception as e:
258
+ raise gr.Error(f"λ‹€μ΄μ–΄κ·Έλž¨ 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
259
 
260
+ # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
261
  demo = gr.Interface(
262
  fn=generate_diagram,
263
  inputs=[
 
269
  gr.Slider(
270
  label="λ„ˆλΉ„",
271
  minimum=512,
272
+ maximum=MAX_IMAGE_SIZE,
273
  step=128,
274
  value=1024
275
  ),
276
  gr.Slider(
277
  label="높이",
278
  minimum=512,
279
+ maximum=MAX_IMAGE_SIZE,
280
  step=128,
281
  value=1024
282
  )
 
295
  cache_examples=True
296
  )
297
 
298
+ # μ•± μ‹€ν–‰
299
  if __name__ == "__main__":
300
+ demo.queue()
301
  demo.launch(
302
  server_name="0.0.0.0",
303
  server_port=7860,