Spaces:
Sleeping
Sleeping
File size: 5,003 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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "0b15b939-593a-4ccc-89bd-0cee09fe2f12",
"metadata": {},
"source": [
"# Python Code Summarizer\n",
"\n",
"The Below code will summarize the python code and example it in details which can help codes better understand a forigen code."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dcf353c-e4f2-4ce7-a3b5-71b29700a148",
"metadata": {},
"outputs": [],
"source": [
"# Imports\n",
"from IPython.display import Markdown, display\n",
"import os\n",
"import openai\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "111cf632-08e8-4246-a5bb-b56942789242",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4f5376f-5e6f-4d75-81bf-222e34bfe828",
"metadata": {},
"outputs": [],
"source": [
"def read_code(**kwargs):\n",
" \"\"\"\n",
" You can pass two types of key word arguments to this function.\n",
" code_path= Path to your complex python code.\n",
" code= Passing raw python code.\n",
" \"\"\"\n",
" code_path = kwargs.get('code_path',None)\n",
" code_raw = kwargs.get('code',None)\n",
" \n",
" if code_path:\n",
" with open(code_path, 'r') as code_file:\n",
" code = code_file.read()\n",
" return (True, code)\n",
"\n",
" if code_raw:\n",
" return (True, code_raw)\n",
"\n",
" return (False, None)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00743dac-0e70-45b7-879a-d7293a6f68a6",
"metadata": {},
"outputs": [],
"source": [
"# Model Prompt\n",
"system_prompt = (\n",
" \"You are a helpful assistant. The following input will be a Python code snippet. \"\n",
" \"Your task is to:\\n\\n\"\n",
" \"1. Summarize the overall purpose of the code.\\n\"\n",
" \"2. Explain the code line by line, describing what each line does and why it's written that way.\\n\"\n",
" \"3. Provide reasoning behind the code structure and logic to help novice Python developers understand the concepts better.\\n\\n\"\n",
" \"Use Markdown format in your response. Make the explanation beginner-friendly, using code blocks, bullet points, and headings where helpful.\"\n",
" ) \n",
"# In a plot twist worthy of sci-fi, this prompt was written by ChatGPT...\n",
"# to tell ChatGPT how to respond. We’ve officially entered the Matrix. 🤖🌀"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed7d2447-32a9-4761-8b0a-b31814bee7e5",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Guess where I got this code from :)\n",
"code_line = \"\"\"yeild from set(book.get(\"author)) for book in books if book.get(\"author\"))\"\"\"\n",
"is_code, raw_code = read_code(code=code_line)\n",
"\n",
"if is_code:\n",
" user_prompt = raw_code\n",
"else:\n",
" print(\"Invalid Arguments\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d74a1a39-1c24-4d4b-bd49-0ca416377a93",
"metadata": {},
"outputs": [],
"source": [
"def messages_for():\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df6c2726-d0fb-4ab6-b13b-d047e8807558",
"metadata": {},
"outputs": [],
"source": [
"def summarize():\n",
" \n",
" response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages_for()\n",
" )\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8425144c-595e-4ad6-9801-3e8778d285c4",
"metadata": {},
"outputs": [],
"source": [
"def display_summary():\n",
" summary = summarize()\n",
" display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "744bffdd-ec3c-4b27-b126-81bf3e8c8295",
"metadata": {},
"outputs": [],
"source": [
"display_summary()"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|