Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qwen audio streamlit #233

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 66 additions & 37 deletions nexa/gguf/streamlit/streamlit_audio_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
from nexa.general import pull_model
from nexa.gguf.nexa_inference_audio_lm import NexaAudioLMInference

# Initialize session state
if "has_result" not in st.session_state:
st.session_state.has_result = False
st.session_state.prompt = ""
st.session_state.key = 0
st.rerun()

default_model = sys.argv[1]
is_local_path = False if sys.argv[2] == "False" else True
hf = False if sys.argv[3] == "False" else True
Expand Down Expand Up @@ -49,7 +56,12 @@ def process_audio(nexa_model, audio_file, prompt=""):
if os.path.exists(temp_audio_path):
os.unlink(temp_audio_path)

st.title("Nexa AI AudioLM Generation")
def start_new_callback():
st.session_state.has_result = False
st.session_state.prompt = ""
st.session_state.key += 1

st.markdown("# Nexa AI AudioLM Generation [![Nexa SDK](https://img.shields.io/badge/SDK-Nexa-blue)](https://github.com/NexaAI/nexa-sdk)")
st.caption("Powered by Nexa AI SDK🐙")

# Sidebar configuration
Expand All @@ -68,39 +80,56 @@ def process_audio(nexa_model, audio_file, prompt=""):
if st.session_state.nexa_model is None:
st.stop()

# Text prompt input
prompt = st.text_input("Enter optional prompt text:", "")

# Option 1: Upload Audio File
st.subheader("Option 1: Upload Audio File")
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"])

if uploaded_file is not None:
st.audio(uploaded_file, format="audio/wav")

if st.button("Process Audio"):
with st.spinner("Processing audio..."):
response = process_audio(st.session_state.nexa_model, uploaded_file, prompt)

if response:
st.subheader("Model Response:")
st.write(response)
else:
st.error("Processing failed. Please try again with a different audio file.")

# Option 2: Real-time Recording
st.subheader("Option 2: Record Audio")
wav_audio_data = st_audiorec()

if wav_audio_data:
if st.button("Process Recorded Audio"):
with st.spinner("Processing audio..."):
response = process_audio(st.session_state.nexa_model, io.BytesIO(wav_audio_data), prompt)

if response:
st.subheader("Model Response:")
st.write(response)
else:
st.error("Processing failed. Please try recording again.")
else:
st.warning("No audio recorded. Please record some audio before processing.")
# Wrap input sections
input_container = st.container()

with input_container:
# Text prompt input
prompt = st.text_input("Enter optional prompt text:", value=st.session_state.prompt, key=f"prompt_{st.session_state.key}")


# Option 1: Upload Audio File
st.subheader("Option 1: Upload Audio File")
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"], key=f"uploader_{st.session_state.key}")

if uploaded_file is not None:
st.audio(uploaded_file, format="audio/wav")

if st.button("Process Audio", key="process_upload"):
with st.spinner("Processing audio..."):
response = process_audio(st.session_state.nexa_model, uploaded_file, prompt)
if response:
st.session_state.has_result = True

if response:
st.subheader("Model Response:")
st.write(response)
if st.session_state.has_result:
if st.button("Start new", key="start_new_upload", on_click=start_new_callback):
pass
st.write("Start new conversation to try next prompt")
else:
st.error("Processing failed. Please try again with a different audio file.")

# Option 2: Real-time Recording
st.subheader("Option 2: Record Audio")
wav_audio_data = st_audiorec()

if wav_audio_data:
if st.button("Process Audio", key="process_record"):
with st.spinner("Processing audio..."):
response = process_audio(st.session_state.nexa_model, io.BytesIO(wav_audio_data), prompt)
if response:
st.session_state.has_result = True

if response:
st.subheader("Model Response:")
st.write(response)
if st.session_state.has_result:
if st.button("Start new", key="start_new_record", on_click=start_new_callback):
pass
st.write("Start new conversation to try next prompt")
else:
st.error("Processing failed. Please try recording again.")
else:
st.warning("No audio recorded. Please record some audio before processing.")
Loading