Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
os.system('pip install -r requirements.txt')
|
4 |
-
from PIL import Image
|
5 |
-
import io
|
6 |
-
import streamlit as st
|
7 |
-
from transformers import pipeline
|
8 |
-
|
9 |
-
# 初始化视觉问题回答和文本到语音的管道
|
10 |
-
# 这里需要替换为你自己的模型,如果使用默认模型则可以省略 model 参数
|
11 |
-
vqa_pipeline = pipeline("visual-question-answering")
|
12 |
-
tts_pipeline = pipeline("text-to-speech")
|
13 |
-
|
14 |
def main():
|
15 |
st.title("Visual Question Answering & Text-to-Audio App")
|
16 |
|
@@ -23,13 +10,17 @@ def main():
|
|
23 |
|
24 |
# 对用户上传的图片和问题进行视觉问题回答
|
25 |
vqa_result = vqa_pipeline({"image": image, "question": question})
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
st.write(f"Answer: {answer}") # 显示回答
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
st.audio(audio_data, format="audio/ogg")
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def main():
|
2 |
st.title("Visual Question Answering & Text-to-Audio App")
|
3 |
|
|
|
10 |
|
11 |
# 对用户上传的图片和问题进行视觉问题回答
|
12 |
vqa_result = vqa_pipeline({"image": image, "question": question})
|
13 |
+
if vqa_result is None: # 确保返回结果不为空
|
14 |
+
answer = "No answer here!"
|
15 |
+
else:
|
16 |
+
answer = vqa_result[0]['answer'] # 获取回答
|
17 |
+
|
18 |
st.write(f"Answer: {answer}") # 显示回答
|
19 |
|
20 |
+
# 将回答转换为音频
|
21 |
+
tts_result = tts_pipeline(answer)
|
22 |
+
audio_data = tts_result['audio'] # 获取音频数据
|
|
|
23 |
|
24 |
+
# 添加一个按钮,让用户选择是否播放音频
|
25 |
+
if st.button('Play Answer Audio'):
|
26 |
+
st.audio(audio_data, format="audio/ogg")
|