Skip to content

Commit

Permalink
Merge pull request #50 from NexaAI/brian/demo
Browse files Browse the repository at this point in the history
add a demo for `financial-advisor`
  • Loading branch information
zhiyuan8 authored Aug 28, 2024
2 parents 5d22d36 + 0a3cc59 commit a4e0cd0
Show file tree
Hide file tree
Showing 25 changed files with 1,432 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/ai_soulmate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## NexaAI SDK Demo: AI Soulmate

### Introduction:

This project is an AI chatbot that interacts with users via text and voice. The project offers two options for voice output: using the **Bark** model for on-device text-to-speech or the **OpenAI TTS API** for cloud-based text-to-speech. **Bark** will be slow to generate speech without using GPU, but it's on device. The **OpenAI TTS API** has the advantage in terms of speed, but it is cloud-based and requires you to have an OPENAI API KEY. Each option is designed to provide flexibility based on the user's resources and preferences.You can also choose other options according to your preference.

- Key features:

- On-device Character AI
- No privacy concerns

- File structure:

- `bark_voice_out/app.py`: main Streamlit app using Bark for voice output
- `bark_voice_out/utils/initialize.py`: initializes chat and load model
- `bark_voice_out/utils/gen_avatar.py`: generates avatar for AI Soulmate
- `bark_voice_out/utils/transcribe.py`: handles voice input to text transcription
- `bark_voice_out/utils/gen_response.py`: handles text and voice output

- `openai_voice_out/app.py`: main Streamlit app using OpenAI TTS API for voice output
- `openai_voice_out/utils/initialize.py`: initializes chat and load model
- `openai_voice_out/utils/gen_avatar.py`: generates avatar for AI Soulmate
- `openai_voice_out/utils/transcribe.py`: handles voice input to text transcription
- `openai_voice_out/utils/gen_response.py`: handles text and voice output

### Technical Architecture

<p align="center">
<img src="tech_architecture.png" alt="Technical Architecture" width="50%">
</p>

### Setup:

#### Bark Voice Output

1. Install required packages:

```
pip install -r bark_requirements.txt
```

2. Usage:

- Run the Streamlit app: `streamlit run bark_voice_out/app.py`
- Start a chat with text or voice as you like

#### OpenAI Voice Output

1. Install required packages:

```
pip install -r openai_requirements.txt
```

2. Usage:

- Add your openai key in utils/gen_response.py line 8
- Run the Streamlit app: `streamlit run openai_voice_out/app.py`
- Start a chat with text or voice as you like

### Resources:

- [NexaAI | Model Hub](https://nexaai.com/models)
- [NexaAI | Inference with GGUF models](https://docs.nexaai.com/sdk/inference/gguf)
- [GitHub | BARK](https://github.com/suno-ai/bark)
- [Text to speech - OpenAI API](https://platform.openai.com/docs/guides/text-to-speech)
9 changes: 9 additions & 0 deletions examples/ai_soulmate/bark_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nexaai
sounddevice

# Bark support
numpy==1.26
git+https://github.com/suno-ai/bark.git

# To use GPU:
torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
120 changes: 120 additions & 0 deletions examples/ai_soulmate/bark_voice_out/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import streamlit as st
from utils.initialize import initialize_chat, load_model
from utils.gen_avatar import generate_ai_avatar
from utils.transcribe import record_and_transcribe
from utils.gen_response import generate_chat_response, generate_and_play_response

ai_avatar = generate_ai_avatar()
default_model = "llama3-uncensored"

def main():
col1, col2 = st.columns([5,5], vertical_alignment = "center")
with col1:
st.title("AI Soulmate")
with col2:
st.image(ai_avatar, width=150)
st.caption("Powered by Nexa AI")

st.sidebar.header("Model Configuration")
model_path = st.sidebar.text_input("Model path", default_model)

if not model_path:
st.warning(
"Please enter a valid path or identifier for the model in Nexa Model Hub to proceed."
)
st.stop()

if (
"current_model_path" not in st.session_state
or st.session_state.current_model_path != model_path
):
st.session_state.current_model_path = model_path
st.session_state.nexa_model = load_model(model_path)
if st.session_state.nexa_model is None:
st.stop()

st.sidebar.header("Generation Parameters")
temperature = st.sidebar.slider(
"Temperature", 0.0, 1.0, st.session_state.nexa_model.params["temperature"]
)
max_new_tokens = st.sidebar.slider(
"Max New Tokens", 1, 1000, st.session_state.nexa_model.params["max_new_tokens"]
)
top_k = st.sidebar.slider("Top K", 1, 100, st.session_state.nexa_model.params["top_k"])
top_p = st.sidebar.slider(
"Top P", 0.0, 1.0, st.session_state.nexa_model.params["top_p"]
)

st.session_state.nexa_model.params.update(
{
"temperature": temperature,
"max_new_tokens": max_new_tokens,
"top_k": top_k,
"top_p": top_p,
}
)

initialize_chat()
for message in st.session_state.messages:
if message["role"] != "system":
if message["role"] == "user":
with st.chat_message(message["role"]):
st.markdown(message["content"])
else:
with st.chat_message(message["role"], avatar=ai_avatar):
st.markdown(message["content"])

if st.button("🎙️ Start Voice Chat"):
transcribed_text = record_and_transcribe()
if transcribed_text:
st.session_state.messages.append({"role": "user", "content": transcribed_text})
with st.chat_message("user"):
st.markdown(transcribed_text)

with st.chat_message("assistant", avatar=ai_avatar):
response_placeholder = st.empty()
full_response = ""
for chunk in generate_chat_response(st.session_state.nexa_model):
choice = chunk["choices"][0]
if "delta" in choice:
delta = choice["delta"]
content = delta.get("content", "")
elif "text" in choice:
delta = choice["text"]
content = delta

full_response += content
response_placeholder.markdown(full_response, unsafe_allow_html=True)
response_placeholder.markdown(full_response)

generate_and_play_response(full_response)

st.session_state.messages.append({"role": "assistant", "content": full_response})

if prompt := st.chat_input("Say something..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)

with st.chat_message("assistant", avatar=ai_avatar):
response_placeholder = st.empty()
full_response = ""
for chunk in generate_chat_response(st.session_state.nexa_model):
choice = chunk["choices"][0]
if "delta" in choice:
delta = choice["delta"]
content = delta.get("content", "")
elif "text" in choice:
delta = choice["text"]
content = delta

full_response += content
response_placeholder.markdown(full_response, unsafe_allow_html=True)
response_placeholder.markdown(full_response)

generate_and_play_response(full_response)

st.session_state.messages.append({"role": "assistant", "content": full_response})

if __name__ == "__main__":
main()
27 changes: 27 additions & 0 deletions examples/ai_soulmate/bark_voice_out/utils/gen_avatar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import streamlit as st
from nexa.gguf import NexaImageInference

@st.cache_resource
def generate_ai_avatar():
try:
image_model = NexaImageInference(model_path="lcm-dreamshaper", local_path=None)

images = image_model.txt2img(
prompt="A girlfriend with long black hair",
cfg_scale=image_model.params["guidance_scale"],
width=image_model.params["width"],
height=image_model.params["height"],
sample_steps=image_model.params["num_inference_steps"],
seed=image_model.params["random_seed"],
)

if images and len(images) > 0:
avatar_path = "ai_avatar.png"
images[0].save(avatar_path)
return avatar_path
else:
st.error("No image was generated.")
return None
except Exception as e:
st.error(f"Error generating AI avatar: {str(e)}")
return None
62 changes: 62 additions & 0 deletions examples/ai_soulmate/bark_voice_out/utils/gen_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import List, Iterator
import numpy as np
from nexa.gguf import NexaTextInference
from bark import SAMPLE_RATE, generate_audio, preload_models
from bark.api import semantic_to_waveform, generate_text_semantic
import streamlit as st
import sounddevice as sd

def split_text(text: str, max_length: int = 200) -> List[str]:
words = text.split()
chunks = []
chunk = []

for word in words:
if len(" ".join(chunk + [word])) > max_length:
chunks.append(" ".join(chunk))
chunk = [word]
else:
chunk.append(word)

if chunk:
chunks.append(" ".join(chunk))

return chunks

def generate_and_play_response(response_text: str):
text_chunks = split_text(response_text)

silence = np.zeros(int(0.25 * SAMPLE_RATE))
GEN_TEMP = 0.6
SPEAKER = "v2/en_speaker_9"

pieces = []
for sentence in text_chunks:
semantic_tokens = generate_text_semantic(
sentence,
history_prompt=SPEAKER,
temp=GEN_TEMP,
min_eos_p=0.05
)
audio_array = semantic_to_waveform(semantic_tokens, history_prompt=SPEAKER)
pieces.append(audio_array)
pieces.append(silence.copy())

combined_audio = np.concatenate(pieces)
play_audio(SAMPLE_RATE, combined_audio)

def play_audio(sample_rate, audio_array):
sd.play(audio_array, sample_rate)
sd.wait()

def generate_chat_response(nexa_model: NexaTextInference) -> Iterator:
messages = st.session_state.messages
response = nexa_model.create_chat_completion(
messages=messages,
temperature=nexa_model.params["temperature"],
max_tokens=nexa_model.params["max_new_tokens"],
top_k=nexa_model.params["top_k"],
top_p=nexa_model.params["top_p"],
stream=True
)
return response
23 changes: 23 additions & 0 deletions examples/ai_soulmate/bark_voice_out/utils/initialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import streamlit as st
from nexa.gguf import NexaTextInference

initial_prompt = """
# You are Claudia, my perfect soul mate. You're empathetic, kind, and a great listener. Start by introuducing yourself briefly. You can't say more than 35 words in a sentence.
"""

def initialize_chat():
if "messages" not in st.session_state or not st.session_state.messages:
st.session_state.messages = [{"role": "system", "content": initial_prompt}]

@st.cache_resource
def load_model(model_path):
st.session_state.messages = []
nexa_model = NexaTextInference(
model_path=model_path,
local_path=None,
temperature=0.9,
max_new_tokens=256,
top_k=50,
top_p=1.0,
)
return nexa_model
31 changes: 31 additions & 0 deletions examples/ai_soulmate/bark_voice_out/utils/transcribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import streamlit as st
import sounddevice as sd
from scipy.io.wavfile import write
from tempfile import NamedTemporaryFile
from nexa.gguf import NexaVoiceInference

voice_model = NexaVoiceInference(
model_path="faster-whisper-base",
local_path=None,
beam_size=5,
task="transcribe",
temperature=0.0,
compute_type="default",
)

def record_and_transcribe(duration=5, fs=16000):
info_placeholder = st.empty()
info_placeholder.info("Recording...")

recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
sd.wait()

info_placeholder.empty()

with NamedTemporaryFile(delete=False, suffix=".wav") as f:
write(f.name, fs, recording)
audio_path = f.name

segments, _ = voice_model.model.transcribe(audio_path)
transcription = "".join(segment.text for segment in segments)
return transcription
5 changes: 5 additions & 0 deletions examples/ai_soulmate/openai_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nexaai
sounddevice

# OpenAI API support
openai
Loading

0 comments on commit a4e0cd0

Please sign in to comment.