File size: 6,056 Bytes
5fdb69e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "f61139a1-40e1-4273-b9a6-5a0a9d63a9bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "from reportlab.lib.pagesizes import letter\n",
    "from reportlab.pdfgen import canvas\n",
    "from IPython.display import display, FileLink\n",
    "from IPython.display import display, HTML, FileLink\n",
    "from reportlab.lib.pagesizes import A4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "e0858b96-fd41-4911-a333-814e4ed23279",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting reportlab\n",
      "  Downloading reportlab-4.2.5-py3-none-any.whl.metadata (1.5 kB)\n",
      "Requirement already satisfied: pillow>=9.0.0 in c:\\users\\legion\\anaconda3\\envs\\to_do_list\\lib\\site-packages (from reportlab) (11.0.0)\n",
      "Collecting chardet (from reportlab)\n",
      "  Downloading chardet-5.2.0-py3-none-any.whl.metadata (3.4 kB)\n",
      "Downloading reportlab-4.2.5-py3-none-any.whl (1.9 MB)\n",
      "   ---------------------------------------- 0.0/1.9 MB ? eta -:--:--\n",
      "   ---------------- ----------------------- 0.8/1.9 MB 6.7 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 1.9/1.9 MB 11.9 MB/s eta 0:00:00\n",
      "Downloading chardet-5.2.0-py3-none-any.whl (199 kB)\n",
      "Installing collected packages: chardet, reportlab\n",
      "Successfully installed chardet-5.2.0 reportlab-4.2.5\n"
     ]
    }
   ],
   "source": [
    "!pip install reportlab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "62cc9d37-c801-4e8a-ad2c-7b1450725a10",
   "metadata": {},
   "outputs": [],
   "source": [
    "OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
    "HEADERS = {\"Content-Type\":\"application/json\"}\n",
    "MODEL = \"llama3.2\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "525a81e7-30f8-4db7-bc8d-29948195bd4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = \"\"\"You are a to-do list generator. Based on the user's input, you will create a clear and descriptive to-do\n",
    "list using bullet points. Only generate the to-do list as bullet points with some explaination and time fraame only if asked for and nothing else. \n",
    "Be a little descriptive.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "id": "7fca3303-3add-468a-a6bd-be7a4d72c811",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_to_do_list(task_description):\n",
    "    payload = {\n",
    "        \"model\": MODEL,\n",
    "        \"messages\": [\n",
    "            {\"role\": \"system\", \"content\": system_prompt},\n",
    "            {\"role\": \"user\", \"content\": task_description}\n",
    "        ],\n",
    "        \"stream\": False\n",
    "    }\n",
    "\n",
    "    response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n",
    "\n",
    "    if response.status_code == 200:\n",
    "        try:\n",
    "            json_response = response.json()\n",
    "            to_do_list = json_response.get(\"message\", {}).get(\"content\", \"No to-do list found.\")\n",
    "            \n",
    "            formatted_output = \"Your To-Do List:\\n\\n\" + to_do_list\n",
    "            file_name = \"to_do_list.txt\"\n",
    "            \n",
    "            with open(file_name, \"w\", encoding=\"utf-8\") as file:\n",
    "                file.write(formatted_output)\n",
    "\n",
    "            return file_name\n",
    "        \n",
    "        except Exception as e:\n",
    "            return f\"Error parsing JSON: {e}\"\n",
    "    else:\n",
    "        return f\"Error: {response.status_code} - {response.text}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "id": "d45d6c7e-0e89-413e-8f30-e4975ea6d043",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter the task description of the to-do list: Give me a 4-week to-do list plan for a wedding reception party.\n"
     ]
    }
   ],
   "source": [
    "task_description = input(\"Enter the task description of the to-do list:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "id": "5493da44-e254-4d06-b973-a8069c2fc625",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "result = generate_to_do_list(task_description)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "id": "5e95c722-ce1a-4630-b21a-1e00e7ba6ab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>You can download your to-do list by clicking the link below:</p>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<a href='to_do_list.txt' target='_blank'>to_do_list.txt</a><br>"
      ],
      "text/plain": [
       "C:\\Users\\Legion\\to-do list using ollama\\to_do_list.txt"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(HTML(\"<p>You can download your to-do list by clicking the link below:</p>\"))\n",
    "display(FileLink(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3d0a44e-bca4-4944-8593-1761c2f73a70",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}