Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline, set_seed
|
| 4 |
+
|
| 5 |
+
# 加载第一个pipeline,用于图片描述
|
| 6 |
+
image_to_text_pipe = pipeline("image-to-text",
|
| 7 |
+
model="Salesforce/blip-image-captioning-base")
|
| 8 |
+
|
| 9 |
+
# 加载第二个pipeline,用于根据文本生成故事
|
| 10 |
+
text_to_story_pipe = pipeline("text-generation",
|
| 11 |
+
model="meta-llama/Llama-2-7b-hf")
|
| 12 |
+
|
| 13 |
+
# 设置随机种子以保证结果的一致性
|
| 14 |
+
set_seed(42)
|
| 15 |
+
|
| 16 |
+
def generate_description(image):
|
| 17 |
+
# 从图片生成描述
|
| 18 |
+
description = image_to_text_pipe(image)
|
| 19 |
+
description_text = description[0]['generated_text']
|
| 20 |
+
return description_text
|
| 21 |
+
|
| 22 |
+
def generate_story(text):
|
| 23 |
+
# 根据描述生成故事
|
| 24 |
+
story = text_to_story_pipe(text, max_length=300, num_return_sequences=1)
|
| 25 |
+
story_text = story[0]['generated_text']
|
| 26 |
+
return story_text
|
| 27 |
+
|
| 28 |
+
def process_image_and_generate_story(image):
|
| 29 |
+
# 串联两个处理过程:生成描述和根据描述生成故事
|
| 30 |
+
description_text = generate_description(image)
|
| 31 |
+
story_text = generate_story(description_text)
|
| 32 |
+
return story_text
|
| 33 |
+
|
| 34 |
+
# 设置Gradio接口
|
| 35 |
+
iface = gr.Interface(fn=process_image_and_generate_story,
|
| 36 |
+
inputs=gr.Image(type='pil'),
|
| 37 |
+
outputs="text",
|
| 38 |
+
title="Image to Story Generator",
|
| 39 |
+
description="Upload an image and the system will generate a story based on it.")
|
| 40 |
+
|
| 41 |
+
# 启动界面
|
| 42 |
+
iface.launch()
|