Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,46 @@
|
|
1 |
import os
|
2 |
-
os.system("pip install scipy")
|
3 |
-
|
4 |
|
5 |
-
from PIL import Image
|
6 |
-
import io
|
7 |
import streamlit as st
|
|
|
|
|
|
|
|
|
8 |
from transformers import pipeline
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
1 |
import os
|
2 |
+
# os.system("pip install scipy")
|
3 |
+
os.system('pip install -r requirements.txt')
|
4 |
|
|
|
|
|
5 |
import streamlit as st
|
6 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
7 |
+
from datasets import load_dataset
|
8 |
+
import torch
|
9 |
+
import soundfile as sf
|
10 |
from transformers import pipeline
|
11 |
+
from PIL import Image
|
12 |
+
import io
|
13 |
+
|
14 |
+
st.title('Video to text and then text to speech app')
|
15 |
+
|
16 |
+
|
17 |
+
image = st.file_uploader("Upload an image", type=["jpg", "png"])
|
18 |
+
|
19 |
+
question = st.text_input(
|
20 |
+
label="Enter your question",
|
21 |
+
value = "How many people and what is the color of this image?"
|
22 |
+
)
|
23 |
+
|
24 |
+
def generate_speech(text):
|
25 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
26 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
27 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
28 |
+
inputs = processor(text=text, return_tensors="pt")
|
29 |
+
|
30 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
31 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
32 |
+
|
33 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
34 |
+
|
35 |
+
sf.write("speech.wav", speech.numpy(), samplerate=16000)
|
36 |
+
|
37 |
+
if st.button("Generate"):
|
38 |
+
image = Image.open(io.BytesIO(image.getvalue()))
|
39 |
+
vqa_pipeline = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
40 |
+
vqa_result = vqa_pipeline({"image": image, "question": question})
|
41 |
+
answer = vqa_result[0]['answer']
|
42 |
+
st.write(f"Question: {question} Answer: {answer}") # 显示回答
|
43 |
+
generate_speech(f"Question: {question}, Answer: {answer}")
|
44 |
+
audio_file = open("speech.wav", 'rb')
|
45 |
+
audio_bytes = audio_file.read()
|
46 |
+
st.audio(audio_bytes, format="audio/wav")
|