Spaces:
Sleeping
Sleeping
File size: 1,703 Bytes
2d80e92 a3276a9 a7c3b50 2d80e92 a7c3b50 2d80e92 7e2ed5b 2d80e92 7e2ed5b a3276a9 7e2ed5b a3276a9 2d80e92 7e2ed5b a3276a9 7e2ed5b a3276a9 7e2ed5b a3276a9 2d80e92 a3276a9 7e2ed5b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import os
# โ
Fix permissions: move cache/config to /tmp (writable in Spaces)
os.environ["HF_HOME"] = "/tmp"
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
os.environ["STREAMLIT_CACHE_DIR"] = "/tmp"
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
os.environ["XDG_CONFIG_HOME"] = "/tmp"
import streamlit as st
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
# Load BLIP model + processor (cached in /tmp)
@st.cache_resource
def load_model():
processor = BlipProcessor.from_pretrained(
"Salesforce/blip-image-captioning-base",
cache_dir="/tmp"
)
model = BlipForConditionalGeneration.from_pretrained(
"Salesforce/blip-image-captioning-base",
cache_dir="/tmp"
)
return processor, model
processor, model = load_model()
# Streamlit UI
st.set_page_config(page_title="Image โ Text Captioning", page_icon="๐ผ๏ธ")
st.title("๐ผ๏ธ Image to Text (Caption Generator)")
st.write("Upload an image and get a text caption generated by a Transformer model ๐")
# Upload image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption="Uploaded Image", use_column_width=True)
if st.button("โจ Generate Caption"):
with st.spinner("Generating caption... please wait โณ"):
inputs = processor(image, return_tensors="pt")
output_ids = model.generate(**inputs, max_new_tokens=30)
caption = processor.decode(output_ids[0], skip_special_tokens=True)
st.subheader("๐ Generated Caption:")
st.success(caption)
|