generated from mozilla-ai/Blueprint-template
-
Notifications
You must be signed in to change notification settings - Fork 5
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
76 add google colab notebook demo #77
Open
daavoo
wants to merge
7
commits into
main
Choose a base branch
from
76-add-google-colab-notebook-demo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+417
−32
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b7d39d
Add demo/notebook.ipynb
daavoo 30d6eac
Add Open in Colab to the README
daavoo 2c88170
Use tabs to document setup options
daavoo 33d9d5c
Clean notebook metadata
daavoo 941a15c
Use %pip install
daavoo d1af435
Use nb-clean
daavoo f99b76b
Install from pypi
daavoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,372 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Document to Podcast" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Source code: https://github.com/mozilla-ai/document-to-podcast" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Docs: https://mozilla-ai.github.io/document-to-podcast/" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This notebooks goes through the process of transforming documents into engaging podcast episodes involves an integration of pre-processing, LLM-powered transcript generation, and text-to-speech generation.\n", | ||
"\n", | ||
"For educational purposes, the \"low level\" API is used.\n", | ||
"\n", | ||
"You can check the [Command Line Interface](https://mozilla-ai.github.io/document-to-podcast/cli/) for a simpler usage." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Installing dependencies" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%pip install --quiet https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.4-cu122/llama_cpp_python-0.3.4-cp310-cp310-linux_x86_64.whl\n", | ||
"%pip install --quiet document-to-podcast" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Uploading data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from google.colab import files\n", | ||
"\n", | ||
"uploaded = files.upload()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Loading and cleaning data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"[Docs for this Step](https://mozilla-ai.github.io/document-to-podcast/step-by-step-guide/#step-1-document-pre-processing)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pathlib import Path\n", | ||
"from document_to_podcast.preprocessing import DATA_CLEANERS, DATA_LOADERS\n", | ||
"\n", | ||
"input_file = list(uploaded.keys())[0]\n", | ||
"suffix = Path(input_file).suffix\n", | ||
"\n", | ||
"data_loader = DATA_LOADERS[suffix]\n", | ||
"data_cleaner = DATA_CLEANERS[suffix]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"raw_text = data_loader(input_file)\n", | ||
"print(f\"Number of characters before cleaning: {len(raw_text)}\")\n", | ||
"print(raw_text[:200])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"clean_text = data_cleaner(raw_text)\n", | ||
"print(f\"Number of characters after cleaning: {len(clean_text)}\")\n", | ||
"print(clean_text[:200])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Downloading and loading models" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"[Docs for this Step](https://mozilla-ai.github.io/document-to-podcast/step-by-step-guide/#step-2-podcast-script-generation)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"For this demo, we are using the following models:\n", | ||
" - [OLMoE-1B-7B-0924-Instruct](https://huggingface.co/allenai/OLMoE-1B-7B-0924-Instruct-GGUF)\n", | ||
" - [OuteAI/OuteTTS-0.2-500M-GGUF/OuteTTS-0.2-500M-FP16.gguf](https://huggingface.co/OuteAI/OuteTTS-0.2-500M-GGUF)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can check the [Customization Guide](https://mozilla-ai.github.io/document-to-podcast/customization/) for more information on how to use different models." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from document_to_podcast.inference.model_loaders import (\n", | ||
" load_llama_cpp_model,\n", | ||
" load_outetts_model,\n", | ||
")\n", | ||
"\n", | ||
"text_model = load_llama_cpp_model(\n", | ||
" \"allenai/OLMoE-1B-7B-0924-Instruct-GGUF/olmoe-1b-7b-0924-instruct-q8_0.gguf\"\n", | ||
")\n", | ||
"speech_model = load_outetts_model(\n", | ||
" \"OuteAI/OuteTTS-0.2-500M-GGUF/OuteTTS-0.2-500M-FP16.gguf\"\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"max_characters = text_model.n_ctx() * 4\n", | ||
"if len(clean_text) > max_characters:\n", | ||
" print(\n", | ||
" f\"Input text is too big ({len(clean_text)}).\"\n", | ||
" f\" Using only a subset of it ({max_characters}).\"\n", | ||
" )\n", | ||
" clean_text = clean_text[:max_characters]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Podcast generation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"[Docs for this Step](https://mozilla-ai.github.io/document-to-podcast/step-by-step-guide/#step-3-audio-podcast-generation)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Speaker configuration" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from document_to_podcast.config import Speaker\n", | ||
"\n", | ||
"speakers = [\n", | ||
" {\n", | ||
" \"id\": 1,\n", | ||
" \"name\": \"Laura\",\n", | ||
" \"description\": \"The main host. She explains topics clearly using anecdotes and analogies, teaching in an engaging and captivating way.\",\n", | ||
" \"voice_profile\": \"female_1\",\n", | ||
" },\n", | ||
" {\n", | ||
" \"id\": 2,\n", | ||
" \"name\": \"Jon\",\n", | ||
" \"description\": \"The co-host. He keeps the conversation on track, asks curious follow-up questions, and reacts with excitement or confusion, often using interjections like hmm or umm.\",\n", | ||
" \"voice_profile\": \"male_1\",\n", | ||
" },\n", | ||
"]\n", | ||
"\n", | ||
"speakers_str = \"\\n\".join(\n", | ||
" str(Speaker.model_validate(speaker))\n", | ||
" for speaker in speakers\n", | ||
" if all(speaker.get(x, None) for x in [\"name\", \"description\", \"voice_profile\"])\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Prompt Configuration" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"PROMPT = \"\"\"\n", | ||
"You are a podcast scriptwriter generating engaging and natural-sounding conversations in JSON format.\n", | ||
"The script features the following speakers:\n", | ||
"{SPEAKERS}\n", | ||
"Instructions:\n", | ||
"- Write dynamic, easy-to-follow dialogue.\n", | ||
"- Include natural interruptions and interjections.\n", | ||
"- Avoid repetitive phrasing between speakers.\n", | ||
"- Format output as a JSON conversation.\n", | ||
"Example:\n", | ||
"{\n", | ||
" \"Speaker 1\": \"Welcome to our podcast! Today, we're exploring...\",\n", | ||
" \"Speaker 2\": \"Hi! I'm excited to hear about this. Can you explain...\",\n", | ||
" \"Speaker 1\": \"Sure! Imagine it like this...\",\n", | ||
" \"Speaker 2\": \"Oh, that's cool! But how does...\"\n", | ||
"}\n", | ||
"\"\"\"\n", | ||
"system_prompt = PROMPT.replace(\"{SPEAKERS}\", speakers_str)\n", | ||
"print(system_prompt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Model inference" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import re\n", | ||
"\n", | ||
"from document_to_podcast.inference.text_to_speech import text_to_speech\n", | ||
"from document_to_podcast.inference.text_to_text import text_to_text_stream\n", | ||
"from IPython.display import display, Audio\n", | ||
"\n", | ||
"podcast_audio = []\n", | ||
"podcast_script = \"\"\n", | ||
"text = \"\"\n", | ||
"for chunk in text_to_text_stream(\n", | ||
" clean_text, text_model, system_prompt=system_prompt.strip()\n", | ||
"):\n", | ||
" text += chunk\n", | ||
" if text.endswith(\"\\n\") and \"Speaker\" in text:\n", | ||
" podcast_script += text\n", | ||
" print(text)\n", | ||
"\n", | ||
" speaker_id = re.search(r\"Speaker (\\d+)\", text).group(1)\n", | ||
" voice_profile = next(\n", | ||
" speaker[\"voice_profile\"]\n", | ||
" for speaker in speakers\n", | ||
" if speaker[\"id\"] == int(speaker_id)\n", | ||
" )\n", | ||
" speech = text_to_speech(\n", | ||
" text.split(f'\"Speaker {speaker_id}\":')[-1],\n", | ||
" speech_model,\n", | ||
" voice_profile,\n", | ||
" )\n", | ||
" podcast_audio.append(speech)\n", | ||
" display(Audio(speech, rate=speech_model.audio_codec.sr))\n", | ||
" text = \"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Save the results" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can download the results from the file explorer." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"with open(\"podcast.txt\", \"w\") as f:\n", | ||
" f.write(podcast_script)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import soundfile as sf\n", | ||
"\n", | ||
"sf.write(\n", | ||
" \"podcast.wav\",\n", | ||
" np.concatenate(podcast_audio),\n", | ||
" samplerate=speech_model.audio_codec.sr,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest we put this badge together with the other badges on the top of the README.
Also we should add it as an option in the #Quick-start section