379 lines
15 KiB
Plaintext
379 lines
15 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Project - PPTX summarizer AI Assistant\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# imports\n",
|
|
"\n",
|
|
"import os\n",
|
|
"import json\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"from openai import OpenAI\n",
|
|
"import gradio as gr\n",
|
|
"from IPython.display import Markdown, display, update_display"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"OpenAI API Key exists and begins sk-proj-\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Initialization\n",
|
|
"\n",
|
|
"load_dotenv(override=True)\n",
|
|
"\n",
|
|
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
|
|
"if openai_api_key:\n",
|
|
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
|
|
"else:\n",
|
|
" print(\"OpenAI API Key not set\")\n",
|
|
" \n",
|
|
"MODEL = \"gpt-4o-mini\"\n",
|
|
"openai = OpenAI()\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "bac7ac31-6ece-4aa1-87eb-329b82f0f45a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n",
|
|
"import os\n",
|
|
"from pptx import Presentation\n",
|
|
"import openai # adapt if you use a different client\n",
|
|
"\n",
|
|
"class PptxSummary:\n",
|
|
" def __init__(self, name: str):\n",
|
|
" self.name = name\n",
|
|
"\n",
|
|
" def summary(self):\n",
|
|
" # Load an existing PowerPoint\n",
|
|
" prs = Presentation(self.name)\n",
|
|
" newtext = \"\"\n",
|
|
"\n",
|
|
" # print(prs)\n",
|
|
" # Loop over all slides\n",
|
|
" for i, slide in enumerate(prs.slides, start=1):\n",
|
|
" # print(f\"\\n--- Slide {i} ---\")\n",
|
|
" newtext += f\"\\n\\n--- Slide {i} ---\"\n",
|
|
" \n",
|
|
" # Loop over shapes (text boxes, titles, placeholders, etc.)\n",
|
|
" for shape in slide.shapes:\n",
|
|
" if shape.has_text_frame: # Only shapes that can contain text\n",
|
|
" for paragraph in shape.text_frame.paragraphs:\n",
|
|
" # Collect text from each run in the paragraph\n",
|
|
" text = \"\".join(run.text for run in paragraph.runs)\n",
|
|
" # print(text)\n",
|
|
" newtext+= \"\\n\"\n",
|
|
" newtext += text\n",
|
|
" # print(newtext)\n",
|
|
" return newtext\n",
|
|
"\n",
|
|
" \n",
|
|
"system_message = \"You are a helpful assistant for a company and you can summarize the given topic based on the given pptx name. \"\n",
|
|
"system_message += \"Give short, courteous answers, no more than 10 sentence. \"\n",
|
|
"system_message += \"Always be accurate. If the presentation .pptx file does not exist, say so. Respond in markdown.\"\n",
|
|
"\n",
|
|
"def user_message(path):\n",
|
|
" ppt = PptxSummary(path)\n",
|
|
" summarization_message = ppt.summary()\n",
|
|
" message = \"You need to summarize the a pptx file.\"\n",
|
|
" message += f\"The context of that pptx file is here: {summarization_message}\"\n",
|
|
" message += \"Give the concise information in small paragraphs. \"\n",
|
|
" return message\n",
|
|
"\n",
|
|
"def pptx_summary(path):\n",
|
|
" if os.path.exists(path):\n",
|
|
" result = \"The file does not exist\"\n",
|
|
" response = openai.chat.completions.create(\n",
|
|
" model=MODEL,\n",
|
|
" messages=[\n",
|
|
" {\"role\": \"system\", \"content\": system_message},\n",
|
|
" {\"role\": \"user\", \"content\": user_message(path)}\n",
|
|
" ],\n",
|
|
" )\n",
|
|
" result = response.choices[0].message.content\n",
|
|
" return result\n",
|
|
" else:\n",
|
|
" return \"The file does not exist\"\n",
|
|
"\n",
|
|
"\n",
|
|
" \n",
|
|
"\n",
|
|
"# print(\"Current working directory:\", os.getcwd())\n",
|
|
"# print(\"Files here:\", os.listdir())\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "e4747d6b-d224-4ab3-b9bb-81c423497c81",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"# Summary of \"Marie Curie: Highlights of a Notable Life\"\n",
|
|
"\n",
|
|
"The presentation details the life and achievements of Marie Curie, born in Warsaw, Poland in 1867. She pursued her studies in physics and mathematics in Paris, setting the stage for her groundbreaking scientific career.\n",
|
|
"\n",
|
|
"Marie Curie made history as the first woman to win a Nobel Prize and is renowned for her discovery of radioactivity, alongside her husband, Pierre Curie. Her noteworthy accomplishments include winning Nobel Prizes in both Physics and Chemistry, highlighting her significant contributions to science.\n",
|
|
"\n",
|
|
"Curie's legacy extends beyond her scientific achievements; she shattered barriers for women in science and her research ultimately aided in the development of medical X-rays. She is celebrated as one of the most influential scientists of all time, leaving an enduring impact on both science and society.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(pptx_summary(\"Marie_Curie.pptx\"))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5bd2b368-960c-40b4-adf0-db40fe713cbc",
|
|
"metadata": {},
|
|
"source": [
|
|
"# My tools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "8f7c0fda-704b-4e3d-bc69-89986cf5e7a9",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The file does not exist\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(pptx_summary(\"bts.pptx\"))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c3f8e54f-af95-4bf6-a202-89e7a3ffdec5",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "36bedabf-a0a7-4985-ad8e-07ed6a55a3a4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Tools\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "4afceded-7178-4c05-8fa6-9f2085e6a344",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# There's a particular dictionary structure that's required to describe our function:\n",
|
|
"\n",
|
|
"summary_function = {\n",
|
|
" \"name\": \"pptx_summary\",\n",
|
|
" \"description\": \"Get the summary for the given pptx file, you need to call this function, if user asks for a pptx file, if it is outside of the pptx file, tell that you do not know.\",\n",
|
|
" \"parameters\": {\n",
|
|
" \"type\": \"object\",\n",
|
|
" \"properties\": {\n",
|
|
" \"path\": {\n",
|
|
" \"type\": \"string\",\n",
|
|
" \"description\": \"The name of the presentation\",\n",
|
|
" },\n",
|
|
" },\n",
|
|
" \"required\": [\"path\"],\n",
|
|
" \"additionalProperties\": False\n",
|
|
" }\n",
|
|
"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "bdca8679-935f-4e7f-97e6-e71a4d4f228c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# And this is included in a list of tools:\n",
|
|
"\n",
|
|
"tools = [{\"type\": \"function\", \"function\": summary_function}]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c3d3554f-b4e3-4ce7-af6f-68faa6dd2340",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Getting OpenAI to use our Tool\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "ce9b0744-9c78-408d-b9df-9f6fd9ed78cf",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def chat(message, history):\n",
|
|
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
|
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
|
|
"\n",
|
|
" if response.choices[0].finish_reason==\"tool_calls\":\n",
|
|
" message = response.choices[0].message\n",
|
|
" print(f\"message: {message}\")\n",
|
|
" response, city = handle_tool_call(message)\n",
|
|
" print(f\"city: {city}, response: {response}\")\n",
|
|
" messages.append(message)\n",
|
|
" messages.append(response)\n",
|
|
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
|
|
" print(f\"response: {response}\")\n",
|
|
" \n",
|
|
" return response.choices[0].message.content"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "b0992986-ea09-4912-a076-8e5603ee631f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# We have to write that function handle_tool_call:\n",
|
|
"\n",
|
|
"def handle_tool_call(message):\n",
|
|
" tool_call = message.tool_calls[0]\n",
|
|
" print(f\"tool_call: {tool_call}\")\n",
|
|
" arguments = json.loads(tool_call.function.arguments)\n",
|
|
" print(f\"arguments: {arguments}\")\n",
|
|
" path = arguments.get('path')\n",
|
|
" print(f\"path: {path}\")\n",
|
|
" summary = pptx_summary(path)\n",
|
|
" # print(f\"price: {price}\")\n",
|
|
" response = {\n",
|
|
" \"role\": \"tool\",\n",
|
|
" \"content\": json.dumps({\"path\": path,\"summary\": summary}),\n",
|
|
" \"tool_call_id\": tool_call.id\n",
|
|
" }\n",
|
|
" print(f\"response: {response}\")\n",
|
|
" return response, path"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"* Running on local URL: http://127.0.0.1:7860\n",
|
|
"* To create a public link, set `share=True` in `launch()`.\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.HTML object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": []
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"message: ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageFunctionToolCall(id='call_qgCGcnIRiknlEaT6eGFlPyN4', function=Function(arguments='{\"path\":\"nelson_mandela.pptx\"}', name='pptx_summary'), type='function')])\n",
|
|
"tool_call: ChatCompletionMessageFunctionToolCall(id='call_qgCGcnIRiknlEaT6eGFlPyN4', function=Function(arguments='{\"path\":\"nelson_mandela.pptx\"}', name='pptx_summary'), type='function')\n",
|
|
"arguments: {'path': 'nelson_mandela.pptx'}\n",
|
|
"path: nelson_mandela.pptx\n",
|
|
"response: {'role': 'tool', 'content': '{\"path\": \"nelson_mandela.pptx\", \"summary\": \"# Summary of \\\\\"Nelson Mandela: Highlights of a Notable Life\\\\\" \\\\n\\\\nThis presentation covers the life and impact of Nelson Mandela. Born in 1918 in South Africa, Mandela became politically active in the fight against apartheid after studying law. \\\\n\\\\nHe emerged as a prominent leader in the anti-apartheid movement but faced imprisonment for 27 years due to his activism. In 1994, he made history by becoming South Africa\\'s first Black president. \\\\n\\\\nMandela\\'s legacy is profound; he is celebrated as a symbol of peace, reconciliation, and justice. His efforts earned him the Nobel Peace Prize and he continues to inspire human rights movements around the globe.\"}', 'tool_call_id': 'call_qgCGcnIRiknlEaT6eGFlPyN4'}\n",
|
|
"city: nelson_mandela.pptx, response: {'role': 'tool', 'content': '{\"path\": \"nelson_mandela.pptx\", \"summary\": \"# Summary of \\\\\"Nelson Mandela: Highlights of a Notable Life\\\\\" \\\\n\\\\nThis presentation covers the life and impact of Nelson Mandela. Born in 1918 in South Africa, Mandela became politically active in the fight against apartheid after studying law. \\\\n\\\\nHe emerged as a prominent leader in the anti-apartheid movement but faced imprisonment for 27 years due to his activism. In 1994, he made history by becoming South Africa\\'s first Black president. \\\\n\\\\nMandela\\'s legacy is profound; he is celebrated as a symbol of peace, reconciliation, and justice. His efforts earned him the Nobel Peace Prize and he continues to inspire human rights movements around the globe.\"}', 'tool_call_id': 'call_qgCGcnIRiknlEaT6eGFlPyN4'}\n",
|
|
"response: ChatCompletion(id='chatcmpl-CKna164Iq9BrGm35e19Rqm8K6Yr9N', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='# Summary of \"Nelson Mandela: Highlights of a Notable Life\"\\n\\nThis presentation covers the life and impact of Nelson Mandela. Born in 1918 in South Africa, Mandela became politically active in the fight against apartheid after studying law. \\n\\nHe emerged as a prominent leader in the anti-apartheid movement but faced imprisonment for 27 years due to his activism. In 1994, he made history by becoming South Africa\\'s first Black president. \\n\\nMandela\\'s legacy is profound; he is celebrated as a symbol of peace, reconciliation, and justice. His efforts earned him the Nobel Peace Prize, and he continues to inspire human rights movements around the globe.', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1759073181, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_560af6e559', usage=CompletionUsage(completion_tokens=134, prompt_tokens=269, total_tokens=403, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "d551ce21-c66c-4f56-88ad-8212f16437ab",
|
|
"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.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|