Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +21 -7
src/streamlit_app.py
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
-
processor = BlipProcessor.from_pretrained(
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return processor, model
|
| 11 |
|
| 12 |
processor, model = load_model()
|
| 13 |
|
|
|
|
|
|
|
| 14 |
st.title("πΌοΈ Image to Text (Caption Generator)")
|
| 15 |
st.write("Upload an image and get a text caption generated by a Transformer model π")
|
| 16 |
|
|
@@ -21,10 +34,11 @@ if uploaded_file is not None:
|
|
| 21 |
image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 23 |
|
| 24 |
-
if st.button("Generate Caption"):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
st.subheader("π Generated Caption:")
|
| 30 |
st.success(caption)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
|
| 6 |
+
# β
Fix cache issue: force Hugging Face to use /tmp for model storage
|
| 7 |
+
os.environ["HF_HOME"] = "/tmp"
|
| 8 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
|
| 9 |
+
|
| 10 |
+
# Load BLIP model + processor (cached in /tmp)
|
| 11 |
@st.cache_resource
|
| 12 |
def load_model():
|
| 13 |
+
processor = BlipProcessor.from_pretrained(
|
| 14 |
+
"Salesforce/blip-image-captioning-base",
|
| 15 |
+
cache_dir="/tmp"
|
| 16 |
+
)
|
| 17 |
+
model = BlipForConditionalGeneration.from_pretrained(
|
| 18 |
+
"Salesforce/blip-image-captioning-base",
|
| 19 |
+
cache_dir="/tmp"
|
| 20 |
+
)
|
| 21 |
return processor, model
|
| 22 |
|
| 23 |
processor, model = load_model()
|
| 24 |
|
| 25 |
+
# Streamlit UI
|
| 26 |
+
st.set_page_config(page_title="Image β Text Captioning", page_icon="πΌοΈ")
|
| 27 |
st.title("πΌοΈ Image to Text (Caption Generator)")
|
| 28 |
st.write("Upload an image and get a text caption generated by a Transformer model π")
|
| 29 |
|
|
|
|
| 34 |
image = Image.open(uploaded_file).convert("RGB")
|
| 35 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 36 |
|
| 37 |
+
if st.button("β¨ Generate Caption"):
|
| 38 |
+
with st.spinner("Generating caption... please wait β³"):
|
| 39 |
+
inputs = processor(image, return_tensors="pt")
|
| 40 |
+
output_ids = model.generate(**inputs, max_new_tokens=30)
|
| 41 |
+
caption = processor.decode(output_ids[0], skip_special_tokens=True)
|
| 42 |
|
| 43 |
st.subheader("π Generated Caption:")
|
| 44 |
st.success(caption)
|