Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="
|
| 7 |
|
| 8 |
-
# 定义生成描述的函数
|
| 9 |
def generate_caption(image):
|
| 10 |
-
# 处理图像并生成描述
|
| 11 |
result = image_to_text(image)[0]
|
| 12 |
return f"图像描述:{result['generated_text']}"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
gr.Markdown("# 图像文本描述生成工具")
|
| 17 |
-
gr.Markdown("上传图片,自动生成对应的英文描述")
|
| 18 |
-
# 图片上传组件
|
| 19 |
image_input = gr.Image(type="pil", label="上传图片")
|
| 20 |
-
|
| 21 |
-
|
| 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()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
# 强制安装依赖
|
| 3 |
+
os.system("pip install gradio==4.0.0 transformers==4.30.2 torch==1.13.1 pillow==9.4.0")
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import pipeline
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
+
# 加载模型
|
| 10 |
+
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 11 |
|
|
|
|
| 12 |
def generate_caption(image):
|
|
|
|
| 13 |
result = image_to_text(image)[0]
|
| 14 |
return f"图像描述:{result['generated_text']}"
|
| 15 |
|
| 16 |
+
with gr.Blocks(title="图像文本描述工具") as demo:
|
| 17 |
+
gr.Markdown("# 图像文本描述工具")
|
|
|
|
|
|
|
|
|
|
| 18 |
image_input = gr.Image(type="pil", label="上传图片")
|
| 19 |
+
text_output = gr.Textbox(label="生成描述")
|
| 20 |
+
gr.Button("生成").click(fn=generate_caption, inputs=image_input, outputs=text_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
if __name__ == "__main__":
|
| 23 |
demo.launch()
|