Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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=
|
| 26 |
-
min_length=
|
| 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
|