Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| from PIL import Image | |
| from transformers import BlipProcessor, BlipForConditionalGeneration | |
| # 1. μ₯μΉ μ€μ | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| # 2. λͺ¨λΈ λ° νλ‘μΈμ λ‘λ | |
| processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
| model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device) | |
| # 3. μ΄λ―Έμ§ μ€λͺ μμ± ν¨μ | |
| def generate_caption(image): | |
| if image is None: | |
| return "μ΄λ―Έμ§λ₯Ό μ λ‘λν΄μ£ΌμΈμ." | |
| # κ³ μ μ²λ¦¬λ₯Ό μν 리μ¬μ΄μ¦ | |
| image = image.resize((384, 384)) | |
| # μ€λͺ μμ± | |
| inputs = processor(images=image, return_tensors="pt").to(device) | |
| output_ids = model.generate(**inputs, max_length=50) | |
| caption = processor.decode(output_ids[0], skip_special_tokens=True) | |
| print("β μμ±λ μ€λͺ :", caption) | |
| if "Asian" in caption: | |
| caption = caption.replace("Asian", "Korean") | |
| print("β μμ±λ μ€λͺ :", caption) | |
| return caption | |
| return caption | |
| # 4. Gradio μΈν°νμ΄μ€ κ΅¬μ± | |
| with gr.Blocks(title="μ΄λ―Έμ§ μ€λͺ μμ±κΈ°") as demo: | |
| gr.Markdown("## πΌοΈ μ΄λ―Έμ§λ₯Ό μ λ‘λνλ©΄ μ€λͺ μ΄ μλ μμ±λ©λλ€.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image(label="μ λ ₯ μ΄λ―Έμ§", type="pil") | |
| with gr.Column(): | |
| caption_output = gr.Textbox(label="μμ±λ μ€λͺ ", lines=3, show_copy_button=True) | |
| # HTMLλ‘ λ²νΌ μμ± | |
| gr.HTML(""" | |
| <div style='margin-top: 10px; text-align: center;'> | |
| <a href="https://huggingface.co/spaces/VIDraft/stable-diffusion-3.5-large-turboX" target="_blank"> | |
| <button style='padding: 10px 20px; background-color: #ff9900; color: white; border: none; border-radius: 10px; font-size: 16px; box-shadow: 2px 2px 8px rgba(0,0,0,0.3); cursor: pointer;'> | |
| π¨ μΊλ¦¬μ»€μ³ λ§λ€κΈ° | |
| </button> | |
| </a> | |
| </div> | |
| """) | |
| # μ λ‘λ β μ€λͺ μλ μμ± μ°κ²° | |
| image_input.upload(fn=generate_caption, inputs=image_input, outputs=caption_output) | |
| # 5. μ± μ€ν | |
| demo.launch(debug=True) | |