jaimin commited on
Commit
85ba2f7
·
1 Parent(s): 83e1d07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -9,11 +9,27 @@ from transformers import BlipProcessor, BlipForConditionalGeneration
9
  model = BlipForConditionalGeneration.from_pretrained('jaimin/Imagecap')
10
  processor = BlipProcessor.from_pretrained('jaimin/Imagecap')
11
 
12
- def launch(input):
13
- image = Image.open(requests.get(input, stream=True).raw).convert('RGB')
14
- inputs = processor(image, return_tensors="pt")
15
- out = model.generate(**inputs)
16
- return processor.decode(out[0], skip_special_tokens=True)
17
-
18
- iface = gr.Interface(launch, inputs="text", outputs="text")
19
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  model = BlipForConditionalGeneration.from_pretrained('jaimin/Imagecap')
10
  processor = BlipProcessor.from_pretrained('jaimin/Imagecap')
11
 
12
+
13
+
14
+ def predict(image,max_length=64, num_beams=4):
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, max_length = max_length)[0]
19
+ caption_text = tokenizer.decode(caption_ids)
20
+ return processor.decode(caption_ids[0], skip_special_tokens=True)
21
+
22
+
23
+
24
+ input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
25
+ output = gr.outputs.Textbox(label="Captions")
26
+
27
+ title = "ImageCap"
28
+
29
+ interface = gr.Interface(
30
+ fn=predict,
31
+ inputs = input,
32
+ outputs=output,
33
+ title=title,
34
+ )
35
+ interface.launch(debug=True)