File size: 1,791 Bytes
b68a6b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ed9c1b
b68a6b4
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
import streamlit as st 
from transformers import pipeline
from datasets import load_dataset
import soundfile as sf
import torch

st.title('Audio Story Generator')

if 'cache' not in st.session_state:
    st.session_state.cache = {'TEXT_PIPE' : pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", torch_dtype=torch.bfloat16, device_map="auto"),
                            'AUDIO_PIPE': pipeline("text-to-speech", "microsoft/speecht5_tts"),
                            'EMBED':load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")}

if st.button('Generate Story'): 


    pipe = st.session_state.cache['TEXT_PIPE']

    # We use the tokenizer's chat template to format each message - see https://huggingface.co/docs/transformers/main/en/chat_templating
    messages = [
        {
            "role": "system",
            "content": "You are a friendly chatbot who always responds in the style of a pirate",
        },
        {"role": "user", "content": "Write a story. ..."},
    ]

    prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    outputs = pipe(prompt, max_new_tokens=500, do_sample=True, temperature=0.7, top_k=30, top_p=0.90)
    text = outputs[0]["generated_text"]
    # text = outputs = '<|assistant|> here we go, if u wanna try, lets do it'
    start = '<|assistant|>'
    text = text[text.index(start)+len(start):]

    embeddings_dataset = st.session_state.cache['EMBED']
    synthesiser = st.session_state.cache['AUDIO_PIPE']
    speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
    
    speech = synthesiser(text, forward_params={"speaker_embeddings": speaker_embedding})
    # st.write(speech)
    st.audio(speech['audio'], sample_rate = speech['sampling_rate'])