Spaces:
Sleeping
Sleeping
| from transformers import BlipProcessor, BlipForConditionalGeneration | |
| from PIL import Image | |
| import torch | |
| import os | |
| import streamlit as st | |
| import tempfile | |
| # μμ ν μΊμ λλ ν 리 μ§μ | |
| HF_CACHE_DIR = os.path.join(tempfile.gettempdir(), "hf_cache") | |
| os.makedirs(HF_CACHE_DIR, exist_ok=True) | |
| # νκ²½ λ³μ μ€μ (ONLY HF_HOME) | |
| os.environ["HF_HOME"] = HF_CACHE_DIR | |
| # transformers.loadμμ cache_dir μ§μ | |
| processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base", cache_dir=HF_CACHE_DIR) | |
| model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base", cache_dir=HF_CACHE_DIR) | |
| def generate_caption(image_path): | |
| processor, model = load_blip_model() | |
| image = Image.open(image_path).convert("RGB") | |
| inputs = processor(image, return_tensors="pt") | |
| with torch.no_grad(): # β μ±λ₯ μ΅μ ν (inference μ gradient λΆνμ) | |
| out = model.generate(**inputs) | |
| return processor.decode(out[0], skip_special_tokens=True) | |