|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import openai" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 2, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from dotenv import dotenv_values\n", |
| 19 | + "config = dotenv_values(\".env\")" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": 3, |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "openai.api_key = config[\"OPENAI_API_KEY\"]" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 4, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stdout", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "Summarized text:\n", |
| 41 | + "\n", |
| 42 | + " Atlas Shrugged is a novel by Ayn Rand, exploring philosophical themes of reason, individualism, and capitalism, depicting the struggles of Dagny Taggart and Hank Rearden against government coercion, and featuring a mysterious figure named John Galt, inspiring a strike of productive individuals against the looters.\n" |
| 43 | + ] |
| 44 | + } |
| 45 | + ], |
| 46 | + "source": [ |
| 47 | + "\n", |
| 48 | + "input_text = \"\"\n", |
| 49 | + "\n", |
| 50 | + "with open(\"input/text.txt\", \"r\") as file:\n", |
| 51 | + " input_text = file.read()\n", |
| 52 | + "\n", |
| 53 | + "response = openai.ChatCompletion.create(\n", |
| 54 | + " model=\"gpt-3.5-turbo\",\n", |
| 55 | + " messages=[{\"role\": \"system\", \"content\": \"You are a text summarizer bot. Your goal is to summarize in a single sentence the text provided by the user.\"}, {\"role\": \"user\", \"content\": input_text}],\n", |
| 56 | + " max_tokens=100\n", |
| 57 | + ")\n", |
| 58 | + "\n", |
| 59 | + "ai_response = response.choices[0].message.content\n", |
| 60 | + "print(\"Summarized text:\\n\\n \", ai_response)\n", |
| 61 | + "\n", |
| 62 | + "with open(\"output/summary.txt\", \"w\") as file:\n", |
| 63 | + " file.write(ai_response)" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [] |
| 72 | + } |
| 73 | + ], |
| 74 | + "metadata": { |
| 75 | + "kernelspec": { |
| 76 | + "display_name": ".venv", |
| 77 | + "language": "python", |
| 78 | + "name": "python3" |
| 79 | + }, |
| 80 | + "language_info": { |
| 81 | + "codemirror_mode": { |
| 82 | + "name": "ipython", |
| 83 | + "version": 3 |
| 84 | + }, |
| 85 | + "file_extension": ".py", |
| 86 | + "mimetype": "text/x-python", |
| 87 | + "name": "python", |
| 88 | + "nbconvert_exporter": "python", |
| 89 | + "pygments_lexer": "ipython3", |
| 90 | + "version": "3.10.5" |
| 91 | + }, |
| 92 | + "orig_nbformat": 4 |
| 93 | + }, |
| 94 | + "nbformat": 4, |
| 95 | + "nbformat_minor": 2 |
| 96 | +} |
0 commit comments