From 8b67498c587c89e6f6d89099156cb2c5291cb392 Mon Sep 17 00:00:00 2001 From: shreshthkapai Date: Sun, 15 Dec 2024 23:38:09 +0530 Subject: [PATCH] added day 1 assignments. --- ...h-paper-summarizer-using -openai-api.ipynb | 297 ++++++++++++++++++ .../day-1-to-do-list using-ollama.ipynb | 206 ++++++++++++ 2 files changed, 503 insertions(+) create mode 100644 week1/community-contributions/day-1-research-paper-summarizer-using -openai-api.ipynb create mode 100644 week1/community-contributions/day-1-to-do-list using-ollama.ipynb diff --git a/week1/community-contributions/day-1-research-paper-summarizer-using -openai-api.ipynb b/week1/community-contributions/day-1-research-paper-summarizer-using -openai-api.ipynb new file mode 100644 index 0000000..45d0914 --- /dev/null +++ b/week1/community-contributions/day-1-research-paper-summarizer-using -openai-api.ipynb @@ -0,0 +1,297 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "52dc600c-4c45-4803-81cb-f06347f4b2c3", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import requests\n", + "from dotenv import load_dotenv\n", + "from IPython.display import Markdown, display\n", + "from openai import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "4082f16f-d843-41c7-9137-cdfec093b2d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "API key found and looks good so far\n" + ] + } + ], + "source": [ + "load_dotenv()\n", + "api_key = os.getenv('OPENAI_API_KEY')\n", + "\n", + "if not api_key:\n", + " print('No API key was found')\n", + "elif not api_key.startswith(\"sk-proj-\"):\n", + " print(\"API key is found but is not in the proper format\")\n", + "else:\n", + " print(\"API key found and looks good so far\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "16c295ce-c57d-429e-8c03-f6610a8ddd42", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "9a548a52-0f7e-4fdf-ad68-0138b2445935", + "metadata": {}, + "outputs": [], + "source": [ + "system_prompt = \"\"\"You are a research summarizer. That summarizes the content of the research paper in no more than 1000 words. The research summary that you provide should include the following:\n", + "1) Title and Authors - Identify the study and contributors.\n", + "2) Objective/Problem - State the research goal or question.\n", + "3) Background - Briefly explain the context and significance.\n", + "4) Methods - Summarize the approach or methodology.\n", + "5) Key Findings - Highlight the main results or insights.\n", + "6) Conclusion - Provide the implications or contributions of the study.\n", + "7) Future Directions - Suggest areas for further research or exploration.\n", + "8) Limitations - Highlight constraints or challenges in the study.\n", + "9) Potential Applications - Discuss how the findings can be applied in real-world scenarios.\n", + "Keep all points concise, clear, and focused and generate output in markdown.\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "66b4411f-172e-46be-b6cd-a9e5b857fb28", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: ipywidgets in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (8.1.5)\n", + "Requirement already satisfied: pdfplumber in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (0.11.4)\n", + "Requirement already satisfied: comm>=0.1.3 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipywidgets) (0.2.2)\n", + "Requirement already satisfied: ipython>=6.1.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipywidgets) (8.30.0)\n", + "Requirement already satisfied: traitlets>=4.3.1 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipywidgets) (5.14.3)\n", + "Requirement already satisfied: widgetsnbextension~=4.0.12 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipywidgets) (4.0.13)\n", + "Requirement already satisfied: jupyterlab_widgets~=3.0.12 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipywidgets) (3.0.13)\n", + "Requirement already satisfied: pdfminer.six==20231228 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from pdfplumber) (20231228)\n", + "Requirement already satisfied: Pillow>=9.1 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from pdfplumber) (11.0.0)\n", + "Requirement already satisfied: pypdfium2>=4.18.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from pdfplumber) (4.30.0)\n", + "Requirement already satisfied: charset-normalizer>=2.0.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from pdfminer.six==20231228->pdfplumber) (3.4.0)\n", + "Requirement already satisfied: cryptography>=36.0.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from pdfminer.six==20231228->pdfplumber) (44.0.0)\n", + "Requirement already satisfied: colorama in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (0.4.6)\n", + "Requirement already satisfied: decorator in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (5.1.1)\n", + "Requirement already satisfied: jedi>=0.16 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (0.19.2)\n", + "Requirement already satisfied: matplotlib-inline in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (0.1.7)\n", + "Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (3.0.48)\n", + "Requirement already satisfied: pygments>=2.4.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (2.18.0)\n", + "Requirement already satisfied: stack_data in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3)\n", + "Requirement already satisfied: typing_extensions>=4.6 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from ipython>=6.1.0->ipywidgets) (4.12.2)\n", + "Requirement already satisfied: cffi>=1.12 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from cryptography>=36.0.0->pdfminer.six==20231228->pdfplumber) (1.17.1)\n", + "Requirement already satisfied: parso<0.9.0,>=0.8.4 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.4)\n", + "Requirement already satisfied: wcwidth in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13)\n", + "Requirement already satisfied: executing>=1.2.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (2.1.0)\n", + "Requirement already satisfied: asttokens>=2.1.0 in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (3.0.0)\n", + "Requirement already satisfied: pure_eval in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (0.2.3)\n", + "Requirement already satisfied: pycparser in c:\\users\\legion\\anaconda3\\envs\\research_summary\\lib\\site-packages (from cffi>=1.12->cryptography>=36.0.0->pdfminer.six==20231228->pdfplumber) (2.22)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip install ipywidgets pdfplumber" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d8cd8556-ad86-4949-9f15-09de2b8c712b", + "metadata": {}, + "outputs": [], + "source": [ + "import pdfplumber\n", + "from ipywidgets import widgets\n", + "from io import BytesIO" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "0eba3cee-d85c-4d75-9b27-70c8cd7587b1", + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, Markdown" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "53e270e1-c2e6-4bcc-9ada-90c059cd5a51", + "metadata": {}, + "outputs": [], + "source": [ + "def messages_for(user_prompt):\n", + " return [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + " ]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2f1807ec-c10b-4d26-9bee-89bd7a4bbb95", + "metadata": {}, + "outputs": [], + "source": [ + "def summarize(user_prompt):\n", + " # Generate messages using the user_prompt\n", + " messages = messages_for(user_prompt)\n", + " try:\n", + " response = openai.chat.completions.create(\n", + " model=\"gpt-4o-mini\", # Correct model name\n", + " messages=messages,\n", + " max_tokens = 1000 # Pass the generated messages\n", + " )\n", + " # Return the content from the API response correctly\n", + " return response.choices[0].message.content\n", + " except Exception as e:\n", + " # Instead of printing, return an error message that can be displayed\n", + " return f\"Error in OpenAI API call: {e}\"" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0dee8345-4eec-4a9c-ac4e-ad70e13cea44", + "metadata": {}, + "outputs": [], + "source": [ + "upload_widget = widgets.FileUpload(\n", + " accept='.pdf', \n", + " multiple=False,\n", + " description='Upload PDF',\n", + " layout=widgets.Layout(width='300px',height = '100px', border='2px dashed #cccccc', padding='10px')\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "1ff9c7b9-1a3a-4128-a33f-0e5bb2a93d33", + "metadata": {}, + "outputs": [], + "source": [ + "def extract_text_and_generate_summary(change):\n", + " print(\"extracting text\")\n", + " if upload_widget.value:\n", + " # Extract the first uploaded file\n", + " uploaded_file = list(upload_widget.value)[0]\n", + " pdf_file = uploaded_file['content']\n", + "\n", + " # Extract text from the PDF\n", + " try:\n", + " with pdfplumber.open(BytesIO(pdf_file)) as pdf:\n", + " extracted_text = \"\\n\".join(page.extract_text() for page in pdf.pages)\n", + "\n", + " # Generate the user prompt\n", + " user_prompt = (\n", + " f\"You are looking at the text from a research paper. Summarize it in no more than 1000 words. \"\n", + " f\"The output should be in markdown.\\n\\n{extracted_text}\"\n", + " )\n", + "\n", + " # Get the summarized response\n", + " response = summarize(user_prompt)\n", + " \n", + " if response:\n", + " # Use IPython's display method to show markdown below the cell\n", + " display(Markdown(response))\n", + " \n", + " except Exception as e:\n", + " # If there's an error, display it using Markdown\n", + " display(Markdown(f\"**Error:** {str(e)}\"))\n", + "\n", + " # Reset the upload widget\n", + " upload_widget.value = ()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "0c16fe3f-704e-4a87-acd9-42c4e6b0d2fa", + "metadata": {}, + "outputs": [], + "source": [ + "upload_widget.observe(extract_text_and_generate_summary, names='value')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "c2c2d2b2-1264-42d9-9271-c4700b4df80a", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "7304350377d845e78a9a758235e5eba1", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FileUpload(value=(), accept='.pdf', description='Upload PDF', layout=Layout(border_bottom='2px dashed #cccccc'…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(upload_widget)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70c76b90-e626-44b3-8d1f-6e995e8a938d", + "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 +} diff --git a/week1/community-contributions/day-1-to-do-list using-ollama.ipynb b/week1/community-contributions/day-1-to-do-list using-ollama.ipynb new file mode 100644 index 0000000..e01b5df --- /dev/null +++ b/week1/community-contributions/day-1-to-do-list using-ollama.ipynb @@ -0,0 +1,206 @@ +{ + "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": [ + "

You can download your to-do list by clicking the link below:

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "to_do_list.txt
" + ], + "text/plain": [ + "C:\\Users\\Legion\\to-do list using ollama\\to_do_list.txt" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(HTML(\"

You can download your to-do list by clicking the link below:

\"))\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 +}