phind commited on
Commit
9170c17
·
1 Parent(s): 8999ad7

image captioning

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -2,19 +2,41 @@ import streamlit as st
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
- pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
 
 
 
6
 
7
- st.title("Hot Dog? Or Not?")
 
 
 
8
 
9
- file_name = st.file_uploader("Upload a hot dog candidate image")
 
 
10
 
11
- if file_name is not None:
12
- col1, col2 = st.columns(2)
13
 
14
- image = Image.open(file_name)
15
- col1.image(image, use_column_width=True)
16
- predictions = pipeline(image)
17
 
18
- col2.header("Probabilities")
19
- for p in predictions:
20
- col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
+ @st.cache_resourse
6
+ def get_model_hotdog_classification_pipeline()
7
+ pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
8
+ return pipeline
9
 
10
+ @st.cache_resourse
11
+ def get_model_image_captioning_pipeline()
12
+ captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
13
+ return captioner
14
 
15
+ tabs1,tabs2 = st.tabs(['Hot Dog? Or Not?','Imaage Captioning'])
16
+ with tabs1:
17
+ st.title("Hot Dog? Or Not?")
18
 
19
+ file_name = st.file_uploader("Upload a hot dog candidate image",key="hotdog_image")
 
20
 
21
+ if file_name is not None:
22
+ col1, col2 = st.columns(2)
 
23
 
24
+ image = Image.open(file_name)
25
+ col1.image(image, use_column_width=True)
26
+ pipeline = get_model_hotdog_classification_pipeline()
27
+ predictions = pipeline(image)
28
+
29
+ col2.header("Probabilities")
30
+ for p in predictions:
31
+ col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
32
+
33
+ with tabs2:
34
+ st.title("Image Captioning")
35
+
36
+ file_name = st.file_uploader("Upload an image to caption",key="caption_image")
37
+
38
+ if file_name is not None:
39
+ image = Image.open(file_name)
40
+ st.image(image, use_column_width=True)
41
+ captioner = get_model_image_captioning_pipeline()
42
+ st.write(f"generated_text: \n{captioner(image)}")