Im-ai123's picture
requirements.txt
30a1716 verified
raw
history blame
975 Bytes
import gradio as gr
from transformers import pipeline
from PIL import Image
# 加载预训练图像文本描述模型
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
# 定义生成描述的函数
def generate_caption(image):
# 处理图像并生成描述
result = image_to_text(image)[0]
return f"图像描述:{result['generated_text']}"
# 搭建Gradio交互界面
with gr.Blocks(title="图像文本描述生成工具") as demo:
gr.Markdown("# 图像文本描述生成工具")
gr.Markdown("上传图片,自动生成对应的英文描述")
# 图片上传组件
image_input = gr.Image(type="pil", label="上传图片")
# 结果输出组件
text_output = gr.Textbox(label="生成的描述")
# 按钮绑定生成函数
gr.Button("生成描述").click(
fn=generate_caption,
inputs=image_input,
outputs=text_output
)
if __name__ == "__main__":
demo.launch()