import streamlit as st from transformers import BlipProcessor, BlipForQuestionAnswering from PIL import Image import torch # 제목 st.title("🖼️ 이미지 질문 답변 AI") st.write("이미지를 업로드하고 질문을 입력하면 AI가 답해줍니다!") # 모델 로드 (한 번만 로드) @st.cache_resource def load_model(): processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base") model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base") return processor, model processor, model = load_model() # 이미지 업로드 uploaded_file = st.file_uploader("이미지를 업로드하세요", type=["jpg", "jpeg", "png"]) # 질문 입력 question = st.text_input("질문을 입력하세요 (예: 이 사진에 뭐가 있나요?)") # 실행 if uploaded_file and question: image = Image.open(uploaded_file).convert("RGB") st.image(image, caption="업로드한 이미지", use_column_width=True) if st.button("질문하기"): with st.spinner("AI가 생각 중..."): inputs = processor(image, question, return_tensors="pt") out = model.generate(**inputs) answer = processor.decode(out[0], skip_special_tokens=True) st.success("완료!") st.write("👉 답변:", answer)