File size: 837 Bytes
7527590 f928d96 7527590 86c445c f928d96 86c445c 7527590 f928d96 86c445c f928d96 7527590 86c445c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
# 加载模型和处理器
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def generate_caption(image):
inputs = processor(image, return_tensors="pt")
outputs = model.generate(**inputs)
description = processor.decode(outputs[0], skip_special_tokens=True)
return description
# 创建Gradio接口
gradio_app = gr.Interface(
fn=generate_caption,
inputs=gr.Image(type="pil"),
outputs="text",
title="图片描述生成器",
description="上传一张图片,生成相应的描述。"
)
if __name__ == "__main__":
gradio_app.launch()
|