yeecin commited on
Commit
f928d96
1 Parent(s): 43b3bf8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -1,6 +1,17 @@
1
  import gradio as gr
 
 
2
  from transformers import pipeline
3
 
 
 
 
 
 
 
 
 
 
4
  pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
5
 
6
  def predict(input_img):
@@ -8,10 +19,15 @@ def predict(input_img):
8
  return input_img, {p["label"]: p["score"] for p in predictions}
9
 
10
  gradio_app = gr.Interface(
11
- predict,
12
- inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
13
- outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
14
- title="Hot Dog? Or Not?",
 
 
 
 
 
15
  )
16
 
17
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
  from transformers import pipeline
5
 
6
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
7
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
8
+
9
+ def generate_caption(image):
10
+ inputs = processor(image, return_tensors="pt")
11
+ outputs = model.generate(**inputs)
12
+ description = processor.decode(outputs[0], skip_special_tokens=True)
13
+ return description
14
+
15
  pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
16
 
17
  def predict(input_img):
 
19
  return input_img, {p["label"]: p["score"] for p in predictions}
20
 
21
  gradio_app = gr.Interface(
22
+ # predict,
23
+ # inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
24
+ # outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
25
+ # title="Hot Dog? Or Not?",
26
+ fn=generate_caption,
27
+ inputs=gr.inputs.Image(type="pil"),
28
+ outputs="text",
29
+ title="图片描述生成器",
30
+ description="上传一张图片,生成相应的描述。"
31
  )
32
 
33
  if __name__ == "__main__":