monster119120 commited on
Commit
29c2e62
·
verified ·
1 Parent(s): e9ac35c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -18,27 +18,26 @@ def main():
18
  image = st.file_uploader("Upload an image", type=["jpg", "png"])
19
  question = st.text_input("Enter your question")
20
 
21
- if image and question:
 
 
 
22
  # 将上传的文件转换为 PIL 图片
23
  image = Image.open(io.BytesIO(image.getvalue()))
24
 
25
  # 对用户上传的图片和问题进行视觉问题回答
26
  vqa_result = vqa_pipeline({"image": image, "question": question})
27
- if vqa_result is None: # 确保返回结果不为空
28
- answer = "No answer here!"
29
- else:
30
  answer = vqa_result[0]['answer'] # 获取回答
31
-
32
- st.write(f"Answer: {answer}") # 显示回答
33
-
34
- # 将回答转换为音频
35
- tts_result = tts_pipeline(answer)
36
- audio_data = tts_result['audio'] # 获取音频数据
37
-
38
- # 添加一个按钮,让用户选择是否播放音频
39
- if st.button('Play Answer Audio'):
40
- st.audio(audio_data, format="audio/ogg")
41
-
42
 
43
 
44
  if __name__ == "__main__":
 
18
  image = st.file_uploader("Upload an image", type=["jpg", "png"])
19
  question = st.text_input("Enter your question")
20
 
21
+ # 添加按钮以控制是否执行视觉问题回答
22
+ run_vqa = st.button('Run Visual Question Answering')
23
+
24
+ if image and question and run_vqa:
25
  # 将上传的文件转换为 PIL 图片
26
  image = Image.open(io.BytesIO(image.getvalue()))
27
 
28
  # 对用户上传的图片和问题进行视觉问题回答
29
  vqa_result = vqa_pipeline({"image": image, "question": question})
30
+ if vqa_result: # 确保返回结果不为空
 
 
31
  answer = vqa_result[0]['answer'] # 获取回答
32
+ st.write(f"Answer: {answer}") # 显示回答
33
+
34
+ # 添加按钮以控制是否将回答转换为音频并播放
35
+ run_tts = st.button('Convert Answer to Audio')
36
+ if run_tts:
37
+ # 将回答转换为音频
38
+ tts_result = tts_pipeline(answer)
39
+ audio_data = tts_result['audio'] # 获取音频数据
40
+ st.audio(audio_data, format="audio/ogg")
 
 
41
 
42
 
43
  if __name__ == "__main__":