-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tutorial] Added multi-model tutorial and refactored
LLM
model class (
#511) <!-- Thank you for your contribution! Please review https://github.com/autonomi-ai/nos/blob/main/docs/CONTRIBUTING.md before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Summary <!-- Please give a short summary of the change and the problem this solves. --> ## Related issues <!-- For example: "Closes #1234" --> ## Checks - [ ] `make lint`: I've run `make lint` to lint the changes in this PR. - [ ] `make test`: I've made sure the tests (`make test-cpu` or `make test`) are passing. - Additional tests: - [ ] Benchmark tests (when contributing new models) - [ ] GPU/HW tests
- Loading branch information
Showing
5 changed files
with
92 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import rich.console | ||
|
||
from nos.client import Client | ||
|
||
|
||
if __name__ == "__main__": | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser(description="Summarize audio file.") | ||
parser.add_argument("--filename", type=str, help="Audio file to summarize.") | ||
args = parser.parse_args() | ||
|
||
console = rich.console.Console() | ||
path = Path(args.filename) | ||
|
||
# Create a client | ||
address = "[::]:50051" | ||
print(f"Connecting to client at {address} ...") | ||
client = Client(address) | ||
client.WaitForServer() | ||
|
||
# Transcribe with Whisper | ||
model_id = "distil-whisper/distil-small.en" | ||
model = client.Module(model_id) | ||
console.print() | ||
console.print(f"[bold white]Transcribe with [yellow]{model_id}[/yellow].[/bold white]") | ||
|
||
# Transcribe the audio file and print the text | ||
transcription_text = "" | ||
print(f"Transcribing audio file: {path}") | ||
with client.UploadFile(path) as remote_path: | ||
response = model.transcribe(path=remote_path, batch_size=8) | ||
for item in response["chunks"]: | ||
transcription_text += item["text"] | ||
sys.stdout.write(item["text"]) | ||
sys.stdout.flush() | ||
print() | ||
|
||
# Summarize the transcription with LLMs | ||
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" | ||
llm = client.Module(model_id) | ||
console.print() | ||
console.print("[bold white]Summarize with [yellow]TinyLlama/TinyLlama-1.1B-Chat-v1.0[/yellow].[/bold white]") | ||
|
||
prompt = f""" | ||
You are a useful transcribing assistant. | ||
Summarize the following text concisely with key points. | ||
Keep the sentences short, highlight key concepts in each bullet starting with a hyphen. | ||
{transcription_text} | ||
""" | ||
messages = [ | ||
{"role": "user", "content": prompt}, | ||
] | ||
for response in llm.chat(messages=messages, max_new_tokens=1024, _stream=True): | ||
sys.stdout.write(response) | ||
sys.stdout.flush() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
tests/models/test_llama2_chat.py → tests/models/test_llm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters