Shreyansh234 commited on
Commit
2d80e92
Β·
verified Β·
1 Parent(s): bf8eb74

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Load model + processor (BLIP for image captioning)
 
 
 
 
6
  @st.cache_resource
7
  def load_model():
8
- processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
9
- model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
 
 
 
 
 
 
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
- inputs = processor(image, return_tensors="pt")
26
- out = model.generate(**inputs, max_new_tokens=30)
27
- caption = processor.decode(out[0], skip_special_tokens=True)
 
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)