Mediocreatmybest commited on
Commit
bca9f16
1 Parent(s): 6b8d35c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -1,19 +1,29 @@
1
  from PIL import Image
2
  import requests
 
3
  import gradio as gr
4
 
5
- from transformers import BlipProcessor, BlipForConditionalGeneration
6
 
7
- model_id = "Salesforce/blip-image-captioning-base"
 
 
 
 
8
 
9
- model = BlipForConditionalGeneration.from_pretrained(model_id)
10
- processor = BlipProcessor.from_pretrained(model_id)
 
 
 
 
 
 
 
 
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()
 
1
  from PIL import Image
2
  import requests
3
+ import torch
4
  import gradio as gr
5
 
6
+ from transformers import pipeline
7
 
8
+ CAPTION_MODELS = {
9
+ 'blip-base': 'Salesforce/blip-image-captioning-base',
10
+ 'blip-large': 'Salesforce/blip-image-captioning-large',
11
+ 'vit-gpt2-coco-en': 'ydshieh/vit-gpt2-coco-en',
12
+ }
13
 
14
+ captioner = pipeline(task="image-to-text",
15
+ model=CAPTION_MODELS['blip-base'],
16
+ max_new_tokens=30,
17
+ device_map="auto", use_fast=True
18
+ )
19
+
20
+ # Simple caption creation
21
+ def caption_image(captioner, image_path):
22
+ caption = captioner(image_path)[0]['generated_text']
23
+ return str(caption).strip()
24
 
25
  def launch(input):
26
+ return caption_image(captioner, input)
 
 
 
27
 
28
  iface = gr.Interface(launch, inputs="text", outputs="text")
29
+ iface.launch()