g8a9 commited on
Commit
1aa769d
1 Parent(s): b0b99ab

Initial commit

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import VisionEncoderDecoderModel, AutoFeatureExtractor, AutoTokenizer
3
+ import requests
4
+ from PIL import Image
5
+ import torch
6
+
7
+
8
+ CHECKPOINT = "g8a9/vit-geppetto-captioning"
9
+ model = VisionEncoderDecoderModel.from_pretrained(CHECKPOINT)
10
+ feature_extractor = AutoFeatureExtractor.from_pretrained(CHECKPOINT)
11
+ tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT)
12
+
13
+ model.eval()
14
+
15
+
16
+ def generate_caption(url):
17
+ image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
18
+ inputs = feature_extractor(image, return_tensors="pt")
19
+ generated_ids = model.generate(
20
+ inputs["pixel_values"],
21
+ max_length=20,
22
+ num_beams=5,
23
+ early_stopping=True,
24
+ no_repeat_ngram_size=2,
25
+ num_return_sequences=3,
26
+ )
27
+ captions = tokenizer.batch_decode(
28
+ generated_ids,
29
+ skip_special_tokens=True,
30
+ )
31
+ return captions[0]
32
+
33
+
34
+ st.title("Captioning demo")
35
+
36
+ url = st.text_input(
37
+ "Insert your URL", "https://iheartcats.com/wp-content/uploads/2015/08/c84.jpg"
38
+ )
39
+
40
+ st.image(url)
41
+
42
+ if st.button("Run captioning"):
43
+ with st.spinner("Processing image..."):
44
+ caption = generate_caption(url)
45
+ st.text(caption)