Spaces:
Runtime error
Runtime error
requirements.txt
Browse filesgradio==4.39.0
transformers==4.44.2
torch==2.4.1
pillow==10.4.0
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# 加载预训练的图像文本描述模型
|
| 6 |
+
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
| 7 |
+
|
| 8 |
+
# 定义核心生成函数
|
| 9 |
+
def generate_caption(image):
|
| 10 |
+
# 处理图像并生成描述
|
| 11 |
+
results = image_to_text(image)
|
| 12 |
+
# 提取生成的文本内容,格式化输出
|
| 13 |
+
return f"图像描述:{results[0]['generated_text']}"
|
| 14 |
+
|
| 15 |
+
# 搭建Gradio交互式界面
|
| 16 |
+
with gr.Blocks(title="图像文本描述生成工具") as demo:
|
| 17 |
+
gr.Markdown("# 图像文本描述生成工具")
|
| 18 |
+
gr.Markdown("上传任意图片,将自动生成对应的英文文本描述~")
|
| 19 |
+
# 图像上传组件
|
| 20 |
+
image_input = gr.Image(type="pil", label="上传图片")
|
| 21 |
+
# 结果输出组件
|
| 22 |
+
text_output = gr.Textbox(label="生成的文本描述")
|
| 23 |
+
# 按钮绑定函数,点击触发生成
|
| 24 |
+
gr.Button("生成描述").click(fn=generate_caption, inputs=image_input, outputs=text_output)
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
demo.launch()
|