diff --git a/examples/ai_soulmate/README.md b/examples/ai_soulmate/README.md new file mode 100644 index 00000000..130aafe8 --- /dev/null +++ b/examples/ai_soulmate/README.md @@ -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 + +

+ Technical Architecture +

+ +### 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) diff --git a/examples/ai_soulmate/bark_requirements.txt b/examples/ai_soulmate/bark_requirements.txt new file mode 100644 index 00000000..5d159df2 --- /dev/null +++ b/examples/ai_soulmate/bark_requirements.txt @@ -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 \ No newline at end of file diff --git a/examples/ai_soulmate/bark_voice_out/app.py b/examples/ai_soulmate/bark_voice_out/app.py new file mode 100644 index 00000000..6d23a8b0 --- /dev/null +++ b/examples/ai_soulmate/bark_voice_out/app.py @@ -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() diff --git a/examples/ai_soulmate/bark_voice_out/utils/gen_avatar.py b/examples/ai_soulmate/bark_voice_out/utils/gen_avatar.py new file mode 100644 index 00000000..a6819cde --- /dev/null +++ b/examples/ai_soulmate/bark_voice_out/utils/gen_avatar.py @@ -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 diff --git a/examples/ai_soulmate/bark_voice_out/utils/gen_response.py b/examples/ai_soulmate/bark_voice_out/utils/gen_response.py new file mode 100644 index 00000000..4e44473e --- /dev/null +++ b/examples/ai_soulmate/bark_voice_out/utils/gen_response.py @@ -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 diff --git a/examples/ai_soulmate/bark_voice_out/utils/initialize.py b/examples/ai_soulmate/bark_voice_out/utils/initialize.py new file mode 100644 index 00000000..ad215158 --- /dev/null +++ b/examples/ai_soulmate/bark_voice_out/utils/initialize.py @@ -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 \ No newline at end of file diff --git a/examples/ai_soulmate/bark_voice_out/utils/transcribe.py b/examples/ai_soulmate/bark_voice_out/utils/transcribe.py new file mode 100644 index 00000000..3a53a0b0 --- /dev/null +++ b/examples/ai_soulmate/bark_voice_out/utils/transcribe.py @@ -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 diff --git a/examples/ai_soulmate/openai_requirements.txt b/examples/ai_soulmate/openai_requirements.txt new file mode 100644 index 00000000..4c909357 --- /dev/null +++ b/examples/ai_soulmate/openai_requirements.txt @@ -0,0 +1,5 @@ +nexaai +sounddevice + +# OpenAI API support +openai \ No newline at end of file diff --git a/examples/ai_soulmate/openai_voice_out/app.py b/examples/ai_soulmate/openai_voice_out/app.py new file mode 100644 index 00000000..fdb2d03f --- /dev/null +++ b/examples/ai_soulmate/openai_voice_out/app.py @@ -0,0 +1,139 @@ +import streamlit as st +import base64 +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) + + audio_path = generate_and_play_response(full_response) + + with open(audio_path, "rb") as audio_file: + audio_bytes = audio_file.read() + audio_base64 = base64.b64encode(audio_bytes).decode("utf-8") + st.markdown(f""" + + """, unsafe_allow_html=True) + + 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) + + audio_path = generate_and_play_response(full_response) + + with open(audio_path, "rb") as audio_file: + audio_bytes = audio_file.read() + audio_base64 = base64.b64encode(audio_bytes).decode("utf-8") + st.markdown(f""" + + """, unsafe_allow_html=True) + + st.session_state.messages.append({"role": "assistant", "content": full_response}) + +if __name__ == "__main__": + main() diff --git a/examples/ai_soulmate/openai_voice_out/utils/gen_avatar.py b/examples/ai_soulmate/openai_voice_out/utils/gen_avatar.py new file mode 100644 index 00000000..a6819cde --- /dev/null +++ b/examples/ai_soulmate/openai_voice_out/utils/gen_avatar.py @@ -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 diff --git a/examples/ai_soulmate/openai_voice_out/utils/gen_response.py b/examples/ai_soulmate/openai_voice_out/utils/gen_response.py new file mode 100644 index 00000000..cfdd9fbd --- /dev/null +++ b/examples/ai_soulmate/openai_voice_out/utils/gen_response.py @@ -0,0 +1,32 @@ +import streamlit as st +from typing import Iterator +from typing import Iterator +from nexa.gguf import NexaTextInference +from pathlib import Path +from openai import OpenAI + +client = OpenAI(api_key="YOUR_OPENAI_API_KEY") + + +def generate_and_play_response(response_text: str): + speech_file_path = Path(__file__).parent / "speech.mp3" + response = client.audio.speech.create( + model="tts-1", + voice="shimmer", + input=response_text + ) + + response.stream_to_file(speech_file_path) + return str(speech_file_path) # Return the path as a string + +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 diff --git a/examples/ai_soulmate/openai_voice_out/utils/initialize.py b/examples/ai_soulmate/openai_voice_out/utils/initialize.py new file mode 100644 index 00000000..e50314da --- /dev/null +++ b/examples/ai_soulmate/openai_voice_out/utils/initialize.py @@ -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. +""" + +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 \ No newline at end of file diff --git a/examples/ai_soulmate/openai_voice_out/utils/transcribe.py b/examples/ai_soulmate/openai_voice_out/utils/transcribe.py new file mode 100644 index 00000000..f27c8b7c --- /dev/null +++ b/examples/ai_soulmate/openai_voice_out/utils/transcribe.py @@ -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-tiny", + 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 diff --git a/examples/financial-advisor/README.md b/examples/financial-advisor/README.md new file mode 100644 index 00000000..1b222b87 --- /dev/null +++ b/examples/financial-advisor/README.md @@ -0,0 +1,37 @@ +## NexaAI SDK Demo: On-device Personal Finance advisor + +### Introduction: + +- Key features: + + - On-device processing for data privacy + - Adjustable parameters (model, temperature, max tokens, top-k, top-p, etc.) + - FAISS index for efficient similarity search + - Interactive chat interface for financial queries + +- File structure: + + - `app.py`: main Streamlit application + - `utils/text_generator.py`: handles similarity search and text generation + - `assets/fake_bank_statements`: fake bank statement for testing purpose + +### Setup: + +1. Install required packages: + +``` +pip install -r requirements.txt +``` + +2. Usage: + +- Run the Streamlit app: `streamlit run app.py` +- Upload PDF financial docs (bank statements, SEC filings, etc.) and process them +- Use the chat interface to query your financial data + +### Resources: + +- [NexaAI | Model Hub](https://nexaai.com/models) +- [NexaAI | Inference with GGUF models](https://docs.nexaai.com/sdk/inference/gguf) +- [GitHub | FAISS](https://github.com/facebookresearch/faiss) +- [Local RAG with Unstructured, Ollama, FAISS and LangChain](https://medium.com/@dirakx/local-rag-with-unstructured-ollama-faiss-and-langchain-35e9dfeb56f1) diff --git a/examples/financial-advisor/app.py b/examples/financial-advisor/app.py new file mode 100644 index 00000000..0043633b --- /dev/null +++ b/examples/financial-advisor/app.py @@ -0,0 +1,278 @@ +import sys +import os +import streamlit as st +import shutil +import pdfplumber +from sentence_transformers import SentenceTransformer +import faiss +import numpy as np +import re +import traceback +import logging +from utils.financial_analyzer import FinancialAnalyzer + +# set up logging: +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +# set a default model path & allow override from command line: +default_model = "gemma" +if len(sys.argv) > 1: + default_model = sys.argv[1] + +@st.cache_resource +def load_model(model_path): + return FinancialAnalyzer(model_path) + +def generate_response(query: str) -> str: + result = st.session_state.nexa_model.financial_analysis(query) + if isinstance(result, dict) and "error" in result: + return f"An error occurred: {result['error']}" + return result + +def extract_text_from_pdf(pdf_path): + try: + with pdfplumber.open(pdf_path) as pdf: + text = '' + for page in pdf.pages: + text += page.extract_text() + '\n' + return text + except Exception as e: + logger.error(f"Error extracting text from PDF {pdf_path}: {str(e)}") + return None + +def chunk_text(text, model, max_tokens=256, overlap=20): + try: + if not text: + logger.warning("Empty text provided to chunk_text function") + return [] + + sentences = re.split(r'(?<=[.!?])\s+', text) + chunks = [] + current_chunk = [] + current_tokens = 0 + + for sentence in sentences: + sentence_tokens = len(model.tokenize(sentence)) + if current_tokens + sentence_tokens > max_tokens: + if current_chunk: + chunks.append(' '.join(current_chunk)) + current_chunk = [sentence] + current_tokens = sentence_tokens + else: + current_chunk.append(sentence) + current_tokens += sentence_tokens + + if current_chunk: + chunks.append(' '.join(current_chunk)) + + logger.info(f"Created {len(chunks)} chunks from text") + return chunks + except Exception as e: + logger.error(f"Error chunking text: {str(e)}") + logger.error(traceback.format_exc()) + return [] + +def create_embeddings(chunks, model): + try: + if not chunks: + logger.warning("No chunks provided for embedding creation") + return None + embeddings = model.encode(chunks) + logger.info(f"Created embeddings of shape: {embeddings.shape}") + return embeddings + except Exception as e: + logger.error(f"Error creating embeddings: {str(e)}") + logger.error(traceback.format_exc()) + return None + +def build_faiss_index(embeddings): + try: + if embeddings is None or embeddings.shape[0] == 0: + logger.warning("No valid embeddings provided for FAISS index creation") + return None + dimension = embeddings.shape[1] + index = faiss.IndexFlatL2(dimension) + index.add(embeddings.astype('float32')) + logger.info(f"Built FAISS index with {index.ntotal} vectors") + return index + except Exception as e: + logger.error(f"Error building FAISS index: {str(e)}") + logger.error(traceback.format_exc()) + return None + +def process_pdfs(uploaded_files): + if not uploaded_files: + st.warning("Please upload PDF files first.") + return False + + input_dir = "./assets/input" + output_dir = "./assets/output/processed_data" + + # clear existing files in the input directory: + if os.path.exists(input_dir): + shutil.rmtree(input_dir) + os.makedirs(input_dir, exist_ok=True) + + # save uploaded files to the input directory: + for uploaded_file in uploaded_files: + with open(os.path.join(input_dir, uploaded_file.name), "wb") as f: + f.write(uploaded_file.getbuffer()) + + # process PDFs: + try: + model = SentenceTransformer('all-MiniLM-L6-v2') + all_chunks = [] + + for filename in os.listdir(input_dir): + if filename.endswith('.pdf'): + pdf_path = os.path.join(input_dir, filename) + text = extract_text_from_pdf(pdf_path) + if text is None: + logger.warning(f"Skipping {filename} due to extraction error") + continue + file_chunks = chunk_text(text, model) + if file_chunks: + all_chunks.extend(file_chunks) + st.write(f"Processed {filename}: {len(file_chunks)} chunks") + else: + logger.warning(f"No chunks created for {filename}") + + if not all_chunks: + st.warning("No valid content found in the uploaded PDFs.") + return False + + embeddings = create_embeddings(all_chunks, model) + if embeddings is None: + st.error("Failed to create embeddings.") + return False + + index = build_faiss_index(embeddings) + if index is None: + st.error("Failed to build FAISS index.") + return False + + # save the index and chunks: + os.makedirs(output_dir, exist_ok=True) + faiss.write_index(index, os.path.join(output_dir, 'pdf_index.faiss')) + np.save(os.path.join(output_dir, 'pdf_chunks.npy'), all_chunks) + + # verify files were saved & reload the FAISS index: + if os.path.exists(os.path.join(output_dir, 'pdf_index.faiss')) and \ + os.path.exists(os.path.join(output_dir, 'pdf_chunks.npy')): + # Reload the FAISS index + st.session_state.nexa_model.load_faiss_index() + st.success("PDFs processed and FAISS index reloaded successfully!") + return True + else: + st.error("Error: Processed files not found after saving.") + return False + + except Exception as e: + st.error(f"Error processing PDFs: {str(e)}") + logger.error(f"Error processing PDFs: {str(e)}") + logger.error(traceback.format_exc()) + return False + +def check_faiss_index(): + if "nexa_model" not in st.session_state: + return False + return (st.session_state.nexa_model.embeddings_model is not None and + st.session_state.nexa_model.index is not None and + st.session_state.nexa_model.stored_docs is not None) + +# Streamlit app: +def main(): + st.markdown("

On-Device Personal Finance Advisor

", unsafe_allow_html=True) + st.caption("Powered by Nexa AI SDK🐙") + + # add an empty line: + st.markdown("
", unsafe_allow_html=True) + + if "nexa_model" not in st.session_state: + st.session_state.nexa_model = load_model(default_model) + + # check if FAISS index exists: + if not check_faiss_index(): + st.info("No processed financial documents found. Please upload and process PDFs.") + + # step 1 - file upload: + uploaded_files = st.file_uploader("Choose PDF files", accept_multiple_files=True, type="pdf") + + # step 2 - process PDFs: + if st.button("Process PDFs"): + with st.spinner("Processing PDFs..."): + if process_pdfs(uploaded_files): + st.success("PDFs processed successfully! You can now use the chat feature.") + st.rerun() + else: + st.error("Failed to process PDFs. Please check the logs for more information.") + + # add a horizontal line: + st.markdown("---") + + # original sidebar configuration: + 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 "nexa_model" not in st.session_state or "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") + params = st.session_state.nexa_model.get_params() + temperature = st.sidebar.slider("Temperature", 0.0, 1.0, params["temperature"]) + max_new_tokens = st.sidebar.slider("Max New Tokens", 1, 500, params["max_new_tokens"]) + top_k = st.sidebar.slider("Top K", 1, 100, params["top_k"]) + top_p = st.sidebar.slider("Top P", 0.0, 1.0, params["top_p"]) + + st.session_state.nexa_model.set_params( + temperature=temperature, + max_new_tokens=max_new_tokens, + top_k=top_k, + top_p=top_p + ) + + # step 3 - interactive financial analysis chat: + st.header("Let's discuss your finances🧑‍💼") + + if check_faiss_index(): + if "messages" not in st.session_state: + st.session_state.messages = [] + + for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + + if prompt := st.chat_input("Ask about your financial documents..."): + st.session_state.messages.append({"role": "user", "content": prompt}) + with st.chat_message("user"): + st.markdown(prompt) + + with st.chat_message("assistant"): + response_placeholder = st.empty() + full_response = "" + for chunk in generate_response(prompt): + 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) + + st.session_state.messages.append({"role": "assistant", "content": full_response}) + else: + st.info("Please upload and process PDF files before using the chat feature.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/financial-advisor/assets/fake_bank_statements/bank_statement_feb.pdf b/examples/financial-advisor/assets/fake_bank_statements/bank_statement_feb.pdf new file mode 100644 index 00000000..6188019a Binary files /dev/null and b/examples/financial-advisor/assets/fake_bank_statements/bank_statement_feb.pdf differ diff --git a/examples/financial-advisor/assets/fake_bank_statements/bank_statement_mar.pdf b/examples/financial-advisor/assets/fake_bank_statements/bank_statement_mar.pdf new file mode 100644 index 00000000..db19f566 Binary files /dev/null and b/examples/financial-advisor/assets/fake_bank_statements/bank_statement_mar.pdf differ diff --git a/examples/financial-advisor/assets/fake_bank_statements/bank_tatement_jan.pdf b/examples/financial-advisor/assets/fake_bank_statements/bank_tatement_jan.pdf new file mode 100644 index 00000000..56639531 Binary files /dev/null and b/examples/financial-advisor/assets/fake_bank_statements/bank_tatement_jan.pdf differ diff --git a/examples/financial-advisor/requirements.txt b/examples/financial-advisor/requirements.txt new file mode 100644 index 00000000..cd17171c --- /dev/null +++ b/examples/financial-advisor/requirements.txt @@ -0,0 +1,7 @@ +nexaai +pdfplumber +sentence-transformers +faiss-cpu +langchain +langchain-community +streamlit \ No newline at end of file diff --git a/examples/financial-advisor/utils/financial_analyzer.py b/examples/financial-advisor/utils/financial_analyzer.py new file mode 100644 index 00000000..5723bd01 --- /dev/null +++ b/examples/financial-advisor/utils/financial_analyzer.py @@ -0,0 +1,111 @@ +import os +import faiss +import numpy as np +import logging +from nexa.gguf import NexaTextInference +from sentence_transformers import SentenceTransformer +from langchain_core.documents import Document + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +class FinancialAnalyzer: + def __init__(self, model_path="gemma"): + self.model_path = model_path + self.inference = NexaTextInference( + model_path=self.model_path, + stop_words=[], + temperature=0.7, + max_new_tokens=256, + top_k=50, + top_p=0.9, + profiling=False + ) + self.embeddings_model = SentenceTransformer('all-MiniLM-L6-v2') + self.index = None + self.stored_docs = None + self.load_faiss_index() + + def get_params(self): + return self.inference.params + + def set_params(self, **kwargs): + self.inference.params.update(kwargs) + + def load_faiss_index(self): + try: + faiss_index_dir = "./assets/output/processed_data" + if not os.path.exists(faiss_index_dir): + logger.warning(f"FAISS index directory not found: {faiss_index_dir}") + return + + index_file = os.path.join(faiss_index_dir, "pdf_index.faiss") + if not os.path.exists(index_file): + logger.warning(f"FAISS index file not found: {index_file}") + return + + self.index = faiss.read_index(index_file) + logger.info(f"FAISS index loaded successfully.") + + doc_file = os.path.join(faiss_index_dir, "pdf_chunks.npy") + self.stored_docs = np.load(doc_file, allow_pickle=True) + logger.info(f"Loaded {len(self.stored_docs)} documents") + + if not isinstance(self.stored_docs[0], Document): + self.stored_docs = [Document(page_content=doc) for doc in self.stored_docs] + + except Exception as e: + logger.error(f"Error loading FAISS index: {str(e)}") + + def custom_search(self, query, k=3): + if self.embeddings_model is None or self.index is None or self.stored_docs is None: + logger.error("FAISS index or embeddings model not properly loaded") + return [] + try: + query_vector = self.embeddings_model.encode([query])[0] + scores, indices = self.index.search(np.array([query_vector]), k) + docs = [self.stored_docs[i] for i in indices[0]] + return list(zip(docs, scores[0])) + except Exception as e: + logger.error(f"Error in custom_search: {str(e)}") + return [] + + def truncate_text(self, text, max_tokens=256): + tokens = text.split() + if len(tokens) <= max_tokens: + return text + return ' '.join(tokens[:max_tokens]) + + def financial_analysis(self, query): + try: + if self.embeddings_model is None or self.index is None or self.stored_docs is None: + logger.error("FAISS index not loaded. Please process PDF files first.") + return {"error": "FAISS index not loaded. Please process PDF files first."} + + relevant_docs = self.custom_search(query, k=1) + if not relevant_docs: + logger.warning("No relevant documents found for the query.") + return {"error": "No relevant documents found for the query."} + + context = "\n".join([doc.page_content for doc, _ in relevant_docs]) + truncated_context = self.truncate_text(context) + prompt = f"Financial context: {truncated_context}\n\nAnalyze: {query}" + + prompt_tokens = len(prompt.split()) + logger.info(f"Prompt length: {prompt_tokens} tokens") + + if prompt_tokens > 250: + prompt = self.truncate_text(prompt, 250) + logger.info(f"Truncated prompt length: {len(prompt.split())} tokens") + + llm_input = [ + {"role": "user", "content": prompt} + ] + + return self.inference.create_chat_completion(llm_input, stream=True) + + except Exception as e: + logger.error(f"Error in financial_analysis: {str(e)}") + import traceback + logger.error(traceback.format_exc()) + return {"error": str(e)} \ No newline at end of file diff --git a/examples/voice_transcription/README.md b/examples/voice_transcription/README.md new file mode 100644 index 00000000..9f7f9a34 --- /dev/null +++ b/examples/voice_transcription/README.md @@ -0,0 +1,71 @@ +# NexaAI SDK Demo: Voice Transcription + +## Introduction + +This demo application showcases the capabilities of the NexaAI SDK for real-time voice transcription. The application is built using Streamlit and leverages the NexaAI SDK to transcribe audio in real-time, providing features like language translation, text summarization, and on-device processing for enhanced data privacy. + +### Key Features + +- **Real-Time Voice Transcription**: Transcribe audio in real-time using the NexaAI SDK. +- **Language Translation**: Supports translation of transcribed audio into different languages. +- **Text Summarization**: Generate summaries of the transcribed text using the NexaAI Text Inference model. +- **On-device Processing**: Ensures data privacy by processing audio locally on the device. +- **Interactive User Interface**: A user-friendly interface for managing transcription, summarization, and file uploads. + +### File Structure + +- **`app.py`**: The main Streamlit application that handles the user interface and controls the transcription and summarization processes. +- **`utils/segmenter.py`**: A utility module for segmenting audio streams using WebRTC VAD (Voice Activity Detection). +- **`utils/transcriber.py`**: Contains classes for handling real-time transcription and text inference. + +## Setup + +### 1. Install Required Packages + +Install the required packages by running: + +```bash +pip install -r requirements.txt + +``` + +### 2. Usage + +#### Running the Application + +To start the Streamlit application, use the following command: + +```bash +streamlit run app.py +``` + +#### Features + +- **Real-Time Transcription**: Use the "Start Recording" button to begin transcribing audio in real-time. +- **Stop Recording**: Click "Stop" to end the recording session. +- **Upload Audio Files**: Upload `.wav` files for transcription using the file uploader. +- **Generate Summary**: After transcription, generate a summary of the text by clicking the "Generate Summary" button. +- **Download Transcription**: Download the transcribed text as a `.txt` file. + +### 3. File Processing + +- **Transcription**: The application can transcribe both live audio and pre-recorded `.wav` files. +- **Summarization**: Generate a concise summary of the transcribed text. +- **Translation**: If enabled, the application can translate the transcribed text into the specified language. + + +## Code Overview + +### `app.py` + +The main application script, handling the Streamlit interface, model configuration, and the transcription process. Users can start or stop recording, upload audio files, generate summaries, and download transcriptions directly from the app interface. + +### `utils/segmenter.py` + +A utility for segmenting audio streams using WebRTC's Voice Activity Detection (VAD). This module manages the audio stream, detecting when speech occurs and splitting the audio into manageable chunks for transcription. + +### `utils/transcriber.py` + +Handles the transcription process, including initializing the NexaAI Voice Inference model, processing audio chunks, and managing the transcription lifecycle. It also includes a `TextInference` class for generating summaries from the transcribed text using the NexaAI Text Inference model. + +--- diff --git a/examples/voice_transcription/app.py b/examples/voice_transcription/app.py new file mode 100644 index 00000000..a26cc517 --- /dev/null +++ b/examples/voice_transcription/app.py @@ -0,0 +1,96 @@ +import streamlit as st +from utils.transcriber import RealTimeTranscriber, TextInference # Ensure this import matches the correct module path + +def main(): + st.title("Nexa AI Voice Transcription") + st.caption("Powered by Nexa AI SDK🐙") + + st.sidebar.header("Model Configuration") + + # Sidebar inputs for model configuration + model_path = st.sidebar.text_input("Model Path", "faster-whisper-tiny") + beam_size = st.sidebar.slider("Beam Size", 1, 10, 5) + task = st.sidebar.selectbox("Task", ["transcribe", "translate"], index=0) + temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.0, step=0.1) + language = st.sidebar.text_input("Language", "").strip() + + # Use None if the language input is empty + language = language if language else None + + transcriber = RealTimeTranscriber( + model_path=model_path, + beam_size=beam_size, + task=task, + temperature=temperature, + language=language + ) + + # Initialize TextInference for generating summaries + text_inference = TextInference() + + # Initialize transcription in session state if not already initialized + if "transcription" not in st.session_state: + st.session_state["transcription"] = "" + + # Placeholder for transcription outside the columns for full width + transcription_container = st.empty() + transcription_container.text_area("Transcription", value=st.session_state["transcription"], height=300, key="transcription_area") + + # Start and Stop buttons + col1, col2, col3, col4 = st.columns(4) + with col1: + if st.button("Start Recording"): + transcriber.start_recording_foreground(transcription_container) + + with col2: + if st.button("Stop"): + transcriber.stop_recording_foreground(transcription_container) + + with col3: + if st.button("Reset"): + transcriber.reset_transcription() + + with col4: + # Display download button directly without requiring another click + if st.session_state["transcription"]: + transcription_bytes = st.session_state["transcription"].encode() + st.download_button( + label="Download Transcription", + data=transcription_bytes, + file_name="transcription.txt", + mime="text/plain", + ) + + # File uploader for transcription + uploaded_file = st.file_uploader("Upload a .wav file", type=["wav"]) + if uploaded_file is not None: + if st.button("Transcribe Uploaded Audio"): + with st.spinner("Transcribing uploaded audio..."): + transcription = transcriber.transcribe_audio(uploaded_file) + if transcription: + st.session_state["transcription"] += transcription + transcription_container.text_area("Transcription", value=st.session_state["transcription"], height=300, key="transcription_area_uploaded") + else: + st.error("Transcription failed. Please try again.") + + # Button to generate summary + if st.session_state["transcription"] and st.button("Generate Summary"): + with st.spinner("Generating summary..."): + summary_prompt = f"Please summarize the following transcription:\n\n{st.session_state['transcription']}\n\nSummary:" + summary = "" + summary_area = st.empty() + + for i, chunk in enumerate(text_inference.generate_summary(summary_prompt)): + if not chunk: + continue + + summary += chunk + summary_area.text_area("Summary", value=summary, height=200) + + st.session_state["summary"] = summary + + # Status message + st.write(st.session_state.get("recording_status", "Press 'Start Recording' to begin.")) + +if __name__ == "__main__": + main() diff --git a/examples/voice_transcription/requirements.txt b/examples/voice_transcription/requirements.txt new file mode 100644 index 00000000..2c0b9ef5 --- /dev/null +++ b/examples/voice_transcription/requirements.txt @@ -0,0 +1,9 @@ +nexaai +webrtcvad +streamlit +wave + +# Audio processing +# Note: PyAudio might require system-level dependencies. +# If installation fails, please refer to the README for additional instructions. +pyaudio \ No newline at end of file diff --git a/examples/voice_transcription/utils/segmenter.py b/examples/voice_transcription/utils/segmenter.py new file mode 100644 index 00000000..d05ffb03 --- /dev/null +++ b/examples/voice_transcription/utils/segmenter.py @@ -0,0 +1,92 @@ +import collections +import webrtcvad +import pyaudio +import streamlit as st +import logging + +# Set up logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +class Segmenter: + def __init__(self, vad_mode=3, sample_rate=16000, chunk_duration_ms=20, padding_duration_ms=200, min_segment_duration_s=1, frames_per_buffer=None): + self.vad = webrtcvad.Vad() + self.vad.set_mode(vad_mode) + self.sample_rate = sample_rate + self.chunk_duration_ms = chunk_duration_ms + self.padding_duration_ms = padding_duration_ms + self.min_segment_duration_s = min_segment_duration_s + self.frames_per_buffer = frames_per_buffer or int(self.sample_rate / 1000 * self.chunk_duration_ms) + self.stream = None + self.running = False + + def start_stream(self): + try: + p = pyaudio.PyAudio() + self.stream = p.open(format=pyaudio.paInt16, + channels=1, + rate=self.sample_rate, + input=True, + frames_per_buffer=self.frames_per_buffer) + self.running = True + logger.info(f"Audio stream started with format: {pyaudio.paInt16}, channels: 1, rate: {self.sample_rate}") + except Exception as e: + st.error(f"Error initializing audio stream: {e}") + self.running = False + + + def stop_stream(self): + try: + if self.stream and self.stream.is_active(): + self.stream.stop_stream() + self.stream.close() + self.running = False + except Exception as e: + st.error(f"Error stopping audio stream: {e}") + + def read_audio(self): + try: + return self.stream.read(self.frames_per_buffer, exception_on_overflow=False) + except IOError as e: + st.error(f"Error reading audio: {e}") + return None + + def vad_collector(self): + num_padding_chunks = int(self.padding_duration_ms / self.chunk_duration_ms) + ring_buffer = collections.deque(maxlen=num_padding_chunks) + triggered = False + voiced_frames = [] + segment_duration_ms = 0 + min_segment_duration_ms = self.min_segment_duration_s * 1000 + + while self.running: + frame = self.read_audio() + if frame is None: + continue + + is_speech = self.vad.is_speech(frame, self.sample_rate) + segment_duration_ms += self.chunk_duration_ms + + if not triggered: + ring_buffer.append((frame, is_speech)) + num_voiced = len([f for f, speech in ring_buffer if speech]) + if num_voiced > 0.9 * ring_buffer.maxlen: + triggered = True + voiced_frames.extend([f[0] for f in ring_buffer]) + ring_buffer.clear() + else: + voiced_frames.append(frame) + ring_buffer.append((frame, is_speech)) + num_unvoiced = len([f for f, speech in ring_buffer if not speech]) + + if num_unvoiced > 0.9 * ring_buffer.maxlen and segment_duration_ms >= min_segment_duration_ms: + triggered = False + if len(voiced_frames) > 0: # Only yield non-empty chunks + yield b''.join(voiced_frames) + ring_buffer.clear() + voiced_frames = [] + segment_duration_ms = 0 + + # Yield the final segment if it contains voiced frames + if voiced_frames: + yield b''.join(voiced_frames) diff --git a/examples/voice_transcription/utils/transcriber.py b/examples/voice_transcription/utils/transcriber.py new file mode 100644 index 00000000..250a6728 --- /dev/null +++ b/examples/voice_transcription/utils/transcriber.py @@ -0,0 +1,136 @@ +import os +import wave +import streamlit as st +from utils.segmenter import Segmenter +from nexa.gguf import NexaVoiceInference +from nexa.gguf import NexaTextInference # Import the text inference class +import logging + +# Set up logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +class RealTimeTranscriber: + def __init__(self, model_path, beam_size, task, temperature, output_directory="../transcriptions/audio/", verbose=False, language=None): + self.segmenter = Segmenter() + self.translation = task == "translate" + self.output_directory = output_directory + self.verbose = verbose + + # Initialize NexaVoiceInference with model and compute_type + if self.verbose: + logger.info(f"Loading model from {model_path}...") + self.inference = NexaVoiceInference( + model_path=model_path, + beam_size=beam_size, + language=language, + task=task, + temperature=temperature, + compute_type="default" + ) + if self.verbose: + st.write(f"Model loaded: {self.inference.downloaded_path}") + logger.info(f"Model loaded: {self.inference.downloaded_path}") + + def write_wav(self, filename, audio_data): + try: + with wave.open(filename, 'wb') as wf: + wf.setnchannels(1) + wf.setsampwidth(2) + wf.setframerate(self.segmenter.sample_rate) + wf.writeframes(audio_data) + if self.verbose: + st.write(f"Saved {filename}") + return filename + except Exception as e: + st.error(f"Error writing WAV file: {e}") + logger.error(f"Error writing WAV file: {e}") + return None + + def transcribe_audio(self, audio_data): + try: + if self.verbose: + if self.translation: + st.write(f"Translating audio to English...") + else: + st.write(f"Transcribing audio...") + + # Transcribe directly from the audio data (in-memory) + segments, _ = self.inference.model.transcribe( + audio_data, + beam_size=self.inference.params["beam_size"], + language=self.inference.params["language"], + task=self.inference.params["task"], + temperature=self.inference.params["temperature"], + vad_filter=True + ) + transcription = "".join(segment.text for segment in segments) + return transcription + + except Exception as e: + logger.error(f"Transcription error: {e}") + return None + + def process_chunks(self, transcription_container): + self.segmenter.start_stream() + audio_chunks = self.segmenter.vad_collector() + + if not os.path.exists(self.output_directory): + os.makedirs(self.output_directory) + + for i, chunk in enumerate(audio_chunks): + if not self.segmenter.running: + break + + filename = f"{self.output_directory}/chunk_{i}.wav" + saved_filename = self.write_wav(filename, chunk) + self.last_transcription_set = False + if saved_filename: + transcription = self.transcribe_audio(saved_filename) + logger.info(f"Adding transcription: {i} {transcription}") + if transcription: + st.session_state["transcription"] += transcription + transcription_container.text_area("Transcription", value=st.session_state["transcription"], height=300) + + self.segmenter.stop_stream() + + def start_recording_foreground(self, transcription_container): + """Start recording in the foreground.""" + if "transcription" not in st.session_state: + st.session_state["transcription"] = "" + self.segmenter.running = True + st.session_state["recording_status"] = "Recording..." + self.process_chunks(transcription_container) + st.session_state["recording_status"] = "Recording completed" + + def stop_recording_foreground(self, transcription_container): + """Stop recording in the foreground.""" + self.segmenter.running = False + st.session_state["recording_status"] = "Recording stopped." + + def reset_transcription(self): + """Reset the transcription.""" + st.session_state["transcription"] = "" + st.session_state["recording_status"] = "Transcription reset." + +class TextInference: + def __init__(self, model_path="gemma", temperature=0.7, max_new_tokens=512, top_k=50, top_p=0.9): + # Initialize NexaTextInference with the specified parameters + try: + self.inference = NexaTextInference( + model_path=model_path, + stop_words=[], + temperature=temperature, + max_new_tokens=max_new_tokens, + top_k=top_k, + top_p=top_p, + profiling=False + ) + logger.info(f"Text model loaded: {self.inference.downloaded_path}") + except ValueError as e: + logger.warning(str(e)) + raise e + + def generate_summary(self, prompt): + for chunk in self.inference.create_completion(prompt, stream=True): + yield chunk["choices"][0]["text"] \ No newline at end of file