monster119120 commited on
Commit
82ea5b6
·
verified ·
1 Parent(s): cfd7d35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -20
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
- answer = vqa_result[0]['answer'] # 获取回答
 
 
 
 
27
  st.write(f"Answer: {answer}") # 显示回答
28
 
29
- if st.button("Convert Answer to Audio"):
30
- tts_result = tts_pipeline(answer)
31
- audio_data = tts_result['audio'] # 获取音频数据
32
- st.audio(audio_data, format="audio/ogg")
33
 
34
- if __name__ == "__main__":
35
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")