|
import streamlit as st |
|
from io import BytesIO |
|
from urllib.request import urlopen |
|
import librosa |
|
from transformers import Qwen2AudioForConditionalGeneration, AutoProcessor |
|
import pyttsx3 |
|
|
|
|
|
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct") |
|
model = Qwen2AudioForConditionalGeneration.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct", device_map="auto") |
|
tts_engine = pyttsx3.init() |
|
|
|
|
|
st.title("Text-to-Audio App") |
|
st.text("This app generates audio from text input using Hugging Face models.") |
|
|
|
|
|
text_input = st.text_area("Enter some text for the model:") |
|
if st.button("Generate Audio"): |
|
conversation = [{"role": "user", "content": text_input}] |
|
|
|
|
|
text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False) |
|
inputs = processor(text=text, return_tensors="pt", padding=True) |
|
inputs.input_ids = inputs.input_ids.to("cuda") |
|
|
|
|
|
generate_ids = model.generate(**inputs, max_length=256) |
|
generate_ids = generate_ids[:, inputs.input_ids.size(1):] |
|
|
|
|
|
response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] |
|
st.text(f"Model Response: {response}") |
|
|
|
|
|
tts_engine.say(response) |
|
tts_engine.runAndWait() |
|
st.success("Audio generated and played!") |
|
|