Spaces:
Sleeping
Sleeping
File size: 10,409 Bytes
69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 1e32511 69c0ff1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"\n",
"import dotenv\n",
"from httpx import Timeout\n",
"from langchain_core.prompts import (\n",
" ChatPromptTemplate,\n",
" SystemMessagePromptTemplate,\n",
" HumanMessagePromptTemplate,\n",
")\n",
"from langchain_openai import ChatOpenAI\n",
"from pydantic import BaseModel\n",
"from langchain_community.callbacks import get_openai_callback\n",
"\n",
"dotenv.load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import data.samples_to_split as samples\n",
"from src.utils import GPTModels\n",
"from src.text_split_chain import create_split_text_chain_v2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"chain = create_split_text_chain_v2(llm_model=GPTModels.GPT_4o)\n",
"# chain = create_split_text_chain_v2(llm_model=GPTModels.GPT_4_TURBO_2024_04_09)\n",
"with get_openai_callback() as cb:\n",
" res = chain.invoke({'text': samples.GATSBY_2})"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SplitTextOutputV2(text_raw='<narrator>Inside, the crimson room bloomed with light. Tom and Miss Baker sat at either end of the long couch and she read aloud to him from the Saturday Evening Post—the words, murmurous and uninflected, running together in a soothing tune. The lamplight, bright on his boots and dull on the autumn-leaf yellow of her hair, glinted along the paper as she turned a page with a flutter of slender muscles in her arms.</narrator>\\n\\n<narrator>When we came in she held us silent for a moment with a lifted hand.</narrator>\\n\\n<Jordan>“To be continued,”</Jordan> <narrator>she said, tossing the magazine on the table,</narrator> <Jordan>“in our very next issue.”</Jordan>\\n\\n<narrator>Her body asserted itself with a restless movement of her knee, and she stood up.</narrator>\\n\\n<Jordan>“Ten o’clock,”</Jordan> <narrator>she remarked, apparently finding the time on the ceiling.</narrator> <Jordan>“Time for this good girl to go to bed.”</Jordan>\\n\\n<Daisy>“Jordan’s going to play in the tournament tomorrow,”</Daisy> <narrator>explained Daisy,</narrator> <Daisy>“over at Westchester.”</Daisy>\\n\\n<narrator>“Oh—you’re Jordan Baker.”</narrator>\\n\\n<narrator>I knew now why her face was familiar—its pleasing contemptuous expression had looked out at me from many rotogravure pictures of the sporting life at Asheville and Hot Springs and Palm Beach. I had heard some story of her too, a critical, unpleasant story, but what it was I had forgotten long ago.</narrator>\\n\\n<Jordan>“Good night,”</Jordan> <narrator>she said softly.</narrator> <Jordan>“Wake me at eight, won’t you.”</Jordan>\\n\\n<c1>“If you’ll get up.”</c1>\\n\\n<Jordan>“I will. Good night, Mr. Carraway. See you anon.”</Jordan>\\n\\n<Daisy>“Of course you will,”</Daisy> <narrator>confirmed Daisy.</narrator> <Daisy>“In fact I think I’ll arrange a marriage. Come over often, Nick, and I’ll sort of—oh—fling you together. You know—lock you up accidentally in linen closets and push you out to sea in a boat, and all that sort of thing—”</Daisy>\\n\\n<Jordan>“Good night,”</Jordan> <narrator>called Miss Baker from the stairs.</narrator> <Jordan>“I haven’t heard a word.”</Jordan>\\n\\n<Tom>“She’s a nice girl,”</Tom> <narrator>said Tom after a moment.</narrator> <Tom>“They oughtn’t to let her run around the country this way.”</Tom>\\n\\n<Daisy>“Who oughtn’t to?”</Daisy> <narrator>inquired Daisy coldly.</narrator>\\n\\n<Tom>“Her family.”</Tom>\\n\\n<Daisy>“Her family is one aunt about a thousand years old. Besides, Nick’s going to look after her, aren’t you, Nick? She’s going to spend lots of weekends out here this summer. I think the home influence will be very good for her.”</Daisy>\\n\\n<narrator>Daisy and Tom looked at each other for a moment in silence.</narrator>\\n\\n<narrator>“Is she from New York?”</narrator> <narrator>I asked quickly.</narrator>\\n\\n<Daisy>“From Louisville. Our white girlhood was passed together there. Our beautiful white—”</Daisy>\\n\\n<Tom>“Did you give Nick a little heart to heart talk on the veranda?”</Tom> <narrator>demanded Tom suddenly.</narrator>\\n\\n<Daisy>“Did I?”</Daisy> <narrator>She looked at me.</narrator> <Daisy>“I can’t seem to remember, but I think we talked about the Nordic race. Yes, I’m sure we did. It sort of crept up on us and first thing you know—”</Daisy>\\n\\n<Tom>“Don’t believe everything you hear, Nick,”</Tom> <narrator>he advised me.</narrator>')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"characters: ['c1', 'Daisy', 'Jordan', 'Tom', 'narrator']\n",
"--------------------\n",
"[narrator] Inside, the crimson room bloomed with light. Tom and Miss Baker sat at either end of the long couch and she read aloud to him from the Saturday Evening Post—the words, murmurous and uninflected, running together in a soothing tune. The lamplight, bright on his boots and dull on the autumn-leaf yellow of her hair, glinted along the paper as she turned a page with a flutter of slender muscles in her arms.\n",
"[narrator] When we came in she held us silent for a moment with a lifted hand.\n",
"[Jordan] “To be continued,”\n",
"[narrator] she said, tossing the magazine on the table,\n",
"[Jordan] “in our very next issue.”\n",
"[narrator] Her body asserted itself with a restless movement of her knee, and she stood up.\n",
"[Jordan] “Ten o’clock,”\n",
"[narrator] she remarked, apparently finding the time on the ceiling.\n",
"[Jordan] “Time for this good girl to go to bed.”\n",
"[Daisy] “Jordan’s going to play in the tournament tomorrow,”\n",
"[narrator] explained Daisy,\n",
"[Daisy] “over at Westchester.”\n",
"[narrator] “Oh—you’re Jordan Baker.”\n",
"[narrator] I knew now why her face was familiar—its pleasing contemptuous expression had looked out at me from many rotogravure pictures of the sporting life at Asheville and Hot Springs and Palm Beach. I had heard some story of her too, a critical, unpleasant story, but what it was I had forgotten long ago.\n",
"[Jordan] “Good night,”\n",
"[narrator] she said softly.\n",
"[Jordan] “Wake me at eight, won’t you.”\n",
"[c1] “If you’ll get up.”\n",
"[Jordan] “I will. Good night, Mr. Carraway. See you anon.”\n",
"[Daisy] “Of course you will,”\n",
"[narrator] confirmed Daisy.\n",
"[Daisy] “In fact I think I’ll arrange a marriage. Come over often, Nick, and I’ll sort of—oh—fling you together. You know—lock you up accidentally in linen closets and push you out to sea in a boat, and all that sort of thing—”\n",
"[Jordan] “Good night,”\n",
"[narrator] called Miss Baker from the stairs.\n",
"[Jordan] “I haven’t heard a word.”\n",
"[Tom] “She’s a nice girl,”\n",
"[narrator] said Tom after a moment.\n",
"[Tom] “They oughtn’t to let her run around the country this way.”\n",
"[Daisy] “Who oughtn’t to?”\n",
"[narrator] inquired Daisy coldly.\n",
"[Tom] “Her family.”\n",
"[Daisy] “Her family is one aunt about a thousand years old. Besides, Nick’s going to look after her, aren’t you, Nick? She’s going to spend lots of weekends out here this summer. I think the home influence will be very good for her.”\n",
"[narrator] Daisy and Tom looked at each other for a moment in silence.\n",
"[narrator] “Is she from New York?”\n",
"[narrator] I asked quickly.\n",
"[Daisy] “From Louisville. Our white girlhood was passed together there. Our beautiful white—”\n",
"[Tom] “Did you give Nick a little heart to heart talk on the veranda?”\n",
"[narrator] demanded Tom suddenly.\n",
"[Daisy] “Did I?”\n",
"[narrator] She looked at me.\n",
"[Daisy] “I can’t seem to remember, but I think we talked about the Nordic race. Yes, I’m sure we did. It sort of crept up on us and first thing you know—”\n",
"[Tom] “Don’t believe everything you hear, Nick,”\n",
"[narrator] he advised me.\n"
]
}
],
"source": [
"annotated_text = res.to_character_annotated_text()\n",
"print(annotated_text.to_pretty_text())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LLM usage:\n",
"\n",
"Tokens Used: 1817\n",
"\tPrompt Tokens: 877\n",
"\tCompletion Tokens: 940\n",
"Successful Requests: 1\n",
"Total Cost (USD): $0.0115925\n"
]
}
],
"source": [
"print(f'LLM usage:\\n\\n{cb}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "ai-audio-books",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|