awacke1 commited on
Commit
cc48c86
·
1 Parent(s): bfc4942

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -1
app.py CHANGED
@@ -6,6 +6,10 @@ feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-ima
6
  tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
9
  model.to(device)
10
 
11
 
@@ -34,5 +38,28 @@ def predict_step(image_paths):
34
 
35
  torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/35-Favorite-Games.jpg', '35-Favorite-Games.jpg')
36
 
37
- result = predict_step(['35-Favorite-Games.jpg'])
 
 
 
 
 
 
 
 
 
 
 
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+
11
+
12
+
13
  model.to(device)
14
 
15
 
 
38
 
39
  torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/35-Favorite-Games.jpg', '35-Favorite-Games.jpg')
40
 
41
+ #result = predict_step(['35-Favorite-Games.jpg'])
42
+
43
+ def predict(image,max_length=64, num_beams=4):
44
+ image = image.convert('RGB')
45
+ image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
46
+ clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
47
+ caption_ids = model.generate(image, max_length = max_length)[0]
48
+ caption_text = clean_text(tokenizer.decode(caption_ids))
49
+ return caption_text
50
+
51
+ description= "NLP Image Understanding"
52
+ title = "NLP Image Understanding"
53
+ article = "nlpconnect/vit-gpt2-image-captioning"
54
 
55
+ interface = gr.Interface(
56
+ fn=predict,
57
+ inputs = input,
58
+ theme="grass",
59
+ outputs=output,
60
+ examples = ['35-Favorite-Games.jpg'],
61
+ title=title,
62
+ description=description,
63
+ article = article,
64
+ )
65
+ interface.launch(debug=True)