Ollama is an open-source tool/framework that facilitates users in running large language models (LLMs) on their local computers, such as PCs, edge devices like Raspberry Pi, etc.
How to install it?
Downloads and installations are available for Mac, Linux, and Windows. Visit https://ollama.com/download for instructions.
Running Llama 3.2
Five versions of Llama 3.2 models are available: 1B, 3B, 11B, and 90B. ‘B’ indicates billions. For example, 1B means that the model has been trained on 1 billion parameters. 1B and 3B are text-only models, whereas 11B and 90B are multimodal (text and images).
Run 1B model: ollama run llama3.2:1b
Run 3B model: ollama run llama3.2
After running these models on the terminal, we can interact with the model using the terminal.
Access the deployed models using Web Browsers
Page Assist is an open-source browser extension that provides a sidebar and web UI for your local AI model. It allows you to interact with your model from any webpage.
Access the Llama model using HTTP API in Python Language
import jsonimport requestsdata ='{}'data = json.loads(data)data["model"] ="llama3.2:1b"data["stream"] =Falsedata["prompt"] ="What is Newton's law of motion?"+" Answer in short."# Sent to Chatbotr = requests.post('http://127.0.0.1:11434/api/generate', json=data)response_data = json.loads(json.dumps(r.json()))# Print User and Bot Messageprint(f'\nUser: {data["prompt"]}')bot_response = response_data['response']print(f'\nBot: {bot_response}')
PortAudio is a free, cross-platform, open-source, audio I/O library. It lets you write simple audio programs in ‘C’ or C++ that will compile and run on many platforms including Windows, Macintosh OS X, and Unix (OSS/ALSA).
PyAudio provides Python bindings for PortAudio. Following is the pip command. pip install pyaudio
Following is the code snippet to record the audio.
import pyaudioimport wavep = pyaudio.PyAudio()stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK) #bufferprint("* recording")frames = []for i inrange(0, int(RATE/CHUNK*RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) # 2 bytes(16 bits) per channelstream.stop_stream()stream.close()p.terminate()
pyaudio.PyAudio() method acquires system resources for PortAudio. pyaudio.PyAudio.open() sets up a pyaudio.PyAudio.Stream to play or record audio. pyaudio.PyAudio.Stream.read() is used to read audio data from the stream. In the above code, all the audio frames have been collected in the frames list frames []. These frames will be used for saving the audio file in the later part of the code.
Meaning of parameters to the function open:
FORMAT: PortAudio provides samples in raw PCM (Pulse-Code Modulation) format. That means each sample is an amplitude to be given to the DAC (digital-to-analog converter) in your sound card. For paInt16, this is a value from -32768 to 32767. For paFloat32, this is a floating-point value from -1.0 to 1.0. The sound card converts this values to a proportional voltage that then drives your audio equipment. paFloat32, paInt32, paInt24, paInt16, paInt8, paUInt8, paCustomFormat
CHANNELS means how many samples will be there for each frame.
RATE is the sampling rate (the number of frames per second)
CHUNK is the number of frames the signals are split into. This is arbitrarily chosen.
In the last, the stream should be closed and all the resources acquired must be released.
Saving the audio
Following is the code snippet to save the audio.
# Save the recorded audio filewf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')wf.setnchannels(CHANNELS)wf.setsampwidth(p.get_sample_size(FORMAT))wf.setframerate(RATE)wf.writeframes(b''.join(frames))wf.close()
wave module of Python3 is used for this purpose. Parameters are set as the same values that were used while recording the audio.
Play the audio
Following is the code snippet to play the audio. simpleaudio library I have used for this purpose. There are other libraries available that can be tried. simpleaudio I found to be simple enough.
# Play the recorded audio filewave_obj = sa.WaveObject.from_wave_file("pyaudio-output.wav")play_obj = wave_obj.play()play_obj.wait_done()
Complete Code
import pyaudioimport waveimport simpleaudio as saCHUNK=512FORMAT= pyaudio.paInt16CHANNELS=1RATE=44100RECORD_SECONDS=5WAVE_OUTPUT_FILENAME="myaudio.wav"p = pyaudio.PyAudio()stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK) #bufferprint("* recording")frames = []for i inrange(0, int(RATE/CHUNK*RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) # 2 bytes(16 bits) per channelstream.stop_stream()stream.close()p.terminate()# Save the recorded audio filewf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')wf.setnchannels(CHANNELS)wf.setsampwidth(p.get_sample_size(FORMAT))wf.setframerate(RATE)wf.writeframes(b''.join(frames))wf.close()# Play the recorded audio filewave_obj = sa.WaveObject.from_wave_file("myaudio.wav")play_obj = wave_obj.play()play_obj.wait_done()