saklee commited on
Commit
246732c
·
1 Parent(s): d0feedb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import re
3
+ import gradio as gr
4
+ from gradio.components import Image, Textbox
5
+ from transformers import AutoTokenizer, ViTImageProcessor, VisionEncoderDecoderModel
6
+
7
+ device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+ local_path = "/app/model/nlpconnect_vit-gpt2-image-captioning/"
9
+ feature_extractor = ViTImageProcessor.from_pretrained(local_path)
10
+ tokenizer = AutoTokenizer.from_pretrained(local_path)
11
+ model = VisionEncoderDecoderModel.from_pretrained(local_path).to(device)
12
+
13
+ gen_kwargs = {"max_length": 128, "num_beams": 8}
14
+ def predict(image):
15
+ image = image.convert('RGB')
16
+ image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
17
+ clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
18
+ caption_ids = model.generate(image, **gen_kwargs)[0]
19
+ caption_text = clean_text(tokenizer.decode(caption_ids))
20
+ return caption_text
21
+
22
+
23
+ _input = Image(label="Upload any Image", type = 'pil')
24
+ _output = Textbox(type="text",label="Captions")
25
+ examples = [f"example{i}.jpg" for i in range(1,7)]
26
+
27
+ title = "Image Captioning "
28
+ description = "Made by : 炼丹侠"
29
+ iface = gr.Interface(
30
+
31
+ fn=predict,
32
+ description=description,
33
+ inputs=_input,
34
+ outputs=_output,
35
+ title=title,
36
+ examples = examples
37
+ )
38
+ gr.close_all() #关闭所有正在运行的端口
39
+ iface.launch(server_name="0.0.0.0", server_port=7860, debug=True)