Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# 加载BLIP模型和处理器
|
| 6 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
|
@@ -25,3 +28,17 @@ if image_data is not None:
|
|
| 25 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 26 |
|
| 27 |
st.write(f"图像描述: {caption}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
from PIL import Image
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# 加载BLIP模型和处理器
|
| 9 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
|
|
|
| 28 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 29 |
|
| 30 |
st.write(f"图像描述: {caption}")
|
| 31 |
+
|
| 32 |
+
# 生成语音
|
| 33 |
+
tts = gTTS(text=caption, lang='zh')
|
| 34 |
+
|
| 35 |
+
# 创建临时文件来保存音频
|
| 36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 37 |
+
tts.save(fp.name)
|
| 38 |
+
audio_file = fp.name
|
| 39 |
+
|
| 40 |
+
# 在Streamlit中播放音频
|
| 41 |
+
st.audio(audio_file)
|
| 42 |
+
|
| 43 |
+
# 删除临时文件
|
| 44 |
+
os.remove(audio_file)
|