Skip to content

Commit 2727ded

Browse files
committed
feat: add processing files
1 parent ca98702 commit 2727ded

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

playground/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ For Stable Diffusion, you need to add your `STABILITY_AI_KEY`. You can find it i
122122

123123
- recreating a [simple ChatGPT clone](28-simple-chatgpt-clone.ipynb).
124124
- creating a [text summarization tool](29-text-summarization-tool.ipynb).
125+
- [processing text](30-processing-text-files.ipynb) from a file and creating a new file.
125126

126127
These examples are based on two courses:
127128

playground/input/text.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Atlas Shrugged is a 1957 novel by Ayn Rand. It was her longest novel, the fourth and final one published during her lifetime, and the one she considered her magnum opus in the realm of fiction writing.[1] Atlas Shrugged includes elements of science fiction, mystery, and romance, and it contains Rand's most extensive statement of Objectivism in any of her works of fiction. Rand described the theme of Atlas Shrugged as "the role of man's mind in existence". The book explores a number of philosophical themes from which Rand would subsequently develop Objectivism, including reason, property rights, individualism, libertarianism and capitalism, and depicts what Rand saw as the failures of governmental coercion.
2+
3+
The book depicts a dystopian United States in which private businesses suffer under increasingly burdensome laws and regulations. Railroad executive Dagny Taggart and her lover, steel magnate Hank Rearden, struggle against "looters" who want to exploit their productivity. They discover that a mysterious figure called John Galt is persuading other business leaders to abandon their companies and disappear as a strike of productive individuals against the looters. The novel ends with the strikers planning to build a new capitalist society based on Galt's philosophy.
4+
5+
Atlas Shrugged received largely negative reviews, but achieved enduring popularity and ongoing sales in the following decades. The novel has been cited as an influence on a variety of libertarian and conservative thinkers and politicians. After several unsuccessful attempts to adapt the novel for film or television, a film trilogy based on it was released from 2011 to 2014, and two theatrical adaptations have been staged.

playground/output/summary.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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.

0 commit comments

Comments
 (0)