vivaceailab commited on
Commit
f38b747
Β·
verified Β·
1 Parent(s): 97582a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  import torch
3
  from PIL import Image
4
  from transformers import BlipProcessor, BlipForConditionalGeneration
@@ -18,16 +18,34 @@ def generate_caption(image):
18
  # 고속 처리λ₯Ό μœ„ν•œ λ¦¬μ‚¬μ΄μ¦ˆ
19
  image = image.resize((384, 384))
20
 
21
- # μ„€λͺ… 생성: μ΅œμ†Œ 20단어, λΉ” μ„œμΉ˜ ν™œμš©
22
  inputs = processor(images=image, return_tensors="pt").to(device)
 
23
  output_ids = model.generate(
24
  **inputs,
25
- max_length=100,
26
- min_length=20,
27
  num_beams=5,
28
  early_stopping=True
29
  )
30
- caption = processor.decode(output_ids[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # "Asian"이 ν¬ν•¨λœ 경우 "Korean"으둜 λŒ€μ²΄
33
  if "Asian" in caption:
@@ -45,7 +63,6 @@ with gr.Blocks(title="이미지 μ„€λͺ… 생성기") as demo:
45
  image_input = gr.Image(label="μž…λ ₯ 이미지", type="pil")
46
  with gr.Column():
47
  caption_output = gr.Textbox(label="μƒμ„±λœ μ„€λͺ…", lines=3, show_copy_button=True)
48
- # HTML둜 λ²„νŠΌ 생성
49
  gr.HTML("""
50
  <div style='margin-top: 10px; text-align: center;'>
51
  <a href="https://huggingface.co/spaces/VIDraft/stable-diffusion-3.5-large-turboX" target="_blank">
@@ -56,13 +73,11 @@ with gr.Blocks(title="이미지 μ„€λͺ… 생성기") as demo:
56
  </div>
57
  """)
58
 
59
- # μ—…λ‘œλ“œ β†’ μ„€λͺ… μžλ™ 생성 μ—°κ²°
60
  image_input.upload(fn=generate_caption, inputs=image_input, outputs=caption_output)
61
 
62
  # 5. μ•± μ‹€ν–‰
63
  demo.launch(debug=True)
64
 
65
-
66
  # import gradio as gr
67
  # import torch
68
  # from PIL import Image
 
1
+ # import gradio as gr
2
  import torch
3
  from PIL import Image
4
  from transformers import BlipProcessor, BlipForConditionalGeneration
 
18
  # 고속 처리λ₯Ό μœ„ν•œ λ¦¬μ‚¬μ΄μ¦ˆ
19
  image = image.resize((384, 384))
20
 
 
21
  inputs = processor(images=image, return_tensors="pt").to(device)
22
+ # μΆ©λΆ„νžˆ κΈ΄ 토큰 λ²”μœ„λ₯Ό μƒμ„±ν•˜κ³ , 단어 수둜 ν›„μ²˜λ¦¬
23
  output_ids = model.generate(
24
  **inputs,
25
+ max_length=200, # 토큰 길이 μ΅œλŒ€
26
+ min_length=60, # 토큰 길이 μ΅œμ†Œ
27
  num_beams=5,
28
  early_stopping=True
29
  )
30
+ caption = processor.decode(output_ids[0], skip_special_tokens=True).strip()
31
+
32
+ # 단어 수둜 30~50 사이에 λ§žμΆ”κΈ°
33
+ words = caption.split()
34
+ if len(words) > 50:
35
+ words = words[:50]
36
+ elif len(words) < 30:
37
+ # λ„ˆλ¬΄ 짧으면 토큰 μ΅œμ†Œ 길이 없이 μž¬μƒμ„±ν•΄λ³΄κ³ 
38
+ output_ids = model.generate(
39
+ **inputs,
40
+ max_length=200,
41
+ num_beams=5,
42
+ early_stopping=True
43
+ )
44
+ caption = processor.decode(output_ids[0], skip_special_tokens=True).strip()
45
+ words = caption.split()
46
+ if len(words) > 50:
47
+ words = words[:50]
48
+ caption = " ".join(words)
49
 
50
  # "Asian"이 ν¬ν•¨λœ 경우 "Korean"으둜 λŒ€μ²΄
51
  if "Asian" in caption:
 
63
  image_input = gr.Image(label="μž…λ ₯ 이미지", type="pil")
64
  with gr.Column():
65
  caption_output = gr.Textbox(label="μƒμ„±λœ μ„€λͺ…", lines=3, show_copy_button=True)
 
66
  gr.HTML("""
67
  <div style='margin-top: 10px; text-align: center;'>
68
  <a href="https://huggingface.co/spaces/VIDraft/stable-diffusion-3.5-large-turboX" target="_blank">
 
73
  </div>
74
  """)
75
 
 
76
  image_input.upload(fn=generate_caption, inputs=image_input, outputs=caption_output)
77
 
78
  # 5. μ•± μ‹€ν–‰
79
  demo.launch(debug=True)
80
 
 
81
  # import gradio as gr
82
  # import torch
83
  # from PIL import Image