Spaces:
Sleeping
Sleeping
Leo Liu
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import streamlit as st
|
|
3 |
from transformers import pipeline
|
4 |
import math
|
5 |
import time # 新增time模块
|
|
|
|
|
6 |
|
7 |
# function part
|
8 |
def split_story_with_delay(story_text, total_duration, num_chunks=5):
|
@@ -30,12 +32,29 @@ def split_story_with_delay(story_text, total_duration, num_chunks=5):
|
|
30 |
chunk_duration = total_duration / len(chunks)
|
31 |
return list(zip(chunks, [chunk_duration]*len(chunks)))
|
32 |
|
33 |
-
def img2text(
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def text2story(text):
|
41 |
# 优化prompt模板
|
|
|
3 |
from transformers import pipeline
|
4 |
import math
|
5 |
import time # 新增time模块
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
|
9 |
# function part
|
10 |
def split_story_with_delay(story_text, total_duration, num_chunks=5):
|
|
|
32 |
chunk_duration = total_duration / len(chunks)
|
33 |
return list(zip(chunks, [chunk_duration]*len(chunks)))
|
34 |
|
35 |
+
def img2text(uploaded_file):
|
36 |
+
"""处理上传文件对象"""
|
37 |
+
try:
|
38 |
+
# 将上传文件转换为PIL Image
|
39 |
+
image = Image.open(io.BytesIO(uploaded_file.getvalue()))
|
40 |
+
|
41 |
+
# 显示调试信息(可选)
|
42 |
+
st.write(f"✅ 成功读取图片 | 格式: {image.format} | 尺寸: {image.size}")
|
43 |
+
|
44 |
+
# 创建图片转文本模型
|
45 |
+
image_to_text_model = pipeline(
|
46 |
+
"image-to-text",
|
47 |
+
model="Salesforce/blip-image-captioning-base",
|
48 |
+
device=0 if torch.cuda.is_available() else -1 # 添加GPU支持
|
49 |
+
)
|
50 |
+
|
51 |
+
# 直接传入PIL Image
|
52 |
+
result = image_to_text_model(image)
|
53 |
+
return result[0]["generated_text"]
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
st.error(f"❌ 图片处理失败: {str(e)}")
|
57 |
+
st.stop()
|
58 |
|
59 |
def text2story(text):
|
60 |
# 优化prompt模板
|