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,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
result = image_to_text(image)[0]
|
| 12 |
+
return f"图像描述:{result['generated_text']}"
|
| 13 |
+
|
| 14 |
+
# 搭建Gradio交互界面
|
| 15 |
+
with gr.Blocks(title="图像文本描述生成工具") as demo:
|
| 16 |
+
gr.Markdown("# 图像文本描述生成工具")
|
| 17 |
+
gr.Markdown("上传图片,自动生成对应的英文描述")
|
| 18 |
+
# 图片上传组件
|
| 19 |
+
image_input = gr.Image(type="pil", label="上传图片")
|
| 20 |
+
# 结果输出组件
|
| 21 |
+
text_output = gr.Textbox(label="生成的描述")
|
| 22 |
+
# 按钮绑定生成函数
|
| 23 |
+
gr.Button("生成描述").click(
|
| 24 |
+
fn=generate_caption,
|
| 25 |
+
inputs=image_input,
|
| 26 |
+
outputs=text_output
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
demo.launch()
|