Merge pull request #769 from bharat109puri/main

Week2 Assignment submission by Bharat Puri
This commit is contained in:
Ed Donner
2025-10-21 21:16:18 -04:00
committed by GitHub
2 changed files with 388 additions and 0 deletions

View File

@@ -0,0 +1,388 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
"metadata": {},
"source": [
"# Project - New Employee Onboarding Assistant\n",
"\n",
"A friendly HR assistant that helps new employees get started — explains policies, checks training schedules, finds contacts, and shows office images — while speaking replies and displaying visuals."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os, json, sqlite3, base64\n",
"import json\n",
"from dotenv import load_dotenv\n",
"import gradio as gr\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"import sys\n",
"sys.path.append(os.path.abspath(os.path.join(\"..\", \"..\"))) \n",
"from openai import OpenAI\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
"metadata": {},
"outputs": [],
"source": [
"# Initialization\n",
"\n",
"conn = sqlite3.connect(\"onboarding.db\")\n",
"cursor = conn.cursor()\n",
"\n",
"cursor.execute(\"\"\"\n",
"CREATE TABLE IF NOT EXISTS employees (\n",
" name TEXT,\n",
" role TEXT,\n",
" start_date TEXT,\n",
" manager TEXT,\n",
" location TEXT\n",
")\n",
"\"\"\")\n",
"\n",
"cursor.execute(\"\"\"\n",
"CREATE TABLE IF NOT EXISTS training (\n",
" role TEXT,\n",
" course TEXT,\n",
" duration TEXT\n",
")\n",
"\"\"\")\n",
"\n",
"cursor.executemany(\"INSERT INTO employees VALUES (?, ?, ?, ?, ?)\", [\n",
" (\"Alice\", \"DevOps Engineer\", \"2025-10-15\", \"Bharat Puri\", \"Pune HQ\"),\n",
" (\"Ravi\", \"Data Analyst\", \"2025-10-20\", \"Neha Kapoor\", \"Bangalore\"),\n",
"])\n",
"\n",
"cursor.executemany(\"INSERT INTO training VALUES (?, ?, ?)\", [\n",
" (\"DevOps Engineer\", \"Cloud Infrastructure Basics\", \"2 weeks\"),\n",
" (\"DevOps Engineer\", \"Security and Compliance\", \"1 week\"),\n",
" (\"Data Analyst\", \"Python for Data Analysis\", \"3 weeks\")\n",
"])\n",
"\n",
"conn.commit()\n",
"conn.close()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c3e8173c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✅ API Key loaded: sk-proj-****\n"
]
}
],
"source": [
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"if openai_api_key:\n",
" print(f\"✅ API Key loaded: {openai_api_key[:8]}****\")\n",
"else:\n",
" print(\"❌ OPENAI_API_KEY not set\")\n",
"\n",
"MODEL = \"gpt-4.1-mini\"\n",
"openai = OpenAI()\n",
"DB = \"onboarding.db\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0a521d84-d07c-49ab-a0df-d6451499ed97",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"\"\"\n",
"You are WelcomeAI, an onboarding assistant for new employees.\n",
"Be friendly and concise (12 sentences). \n",
"Always be accurate and supportive. If unsure, say so politely.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2f6396f8-247e-4289-9bca-590cfc94a377",
"metadata": {},
"outputs": [],
"source": [
"# -------------------- TOOLS --------------------\n",
"\n",
"def get_employee_info(name):\n",
" with sqlite3.connect(DB) as conn:\n",
" cursor = conn.cursor()\n",
" cursor.execute(\"SELECT * FROM employees WHERE lower(name)=?\", (name.lower(),))\n",
" result = cursor.fetchone()\n",
" if result:\n",
" name, role, start_date, manager, location = result\n",
" return f\"{name} is joining as a {role} on {start_date}. Manager: {manager}. Location: {location}.\"\n",
" else:\n",
" return \"I couldnt find that employee in the database.\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "03f19289",
"metadata": {},
"outputs": [],
"source": [
"def get_training_schedule(role):\n",
" with sqlite3.connect(DB) as conn:\n",
" cursor = conn.cursor()\n",
" cursor.execute(\"SELECT course, duration FROM training WHERE role=?\", (role,))\n",
" results = cursor.fetchall()\n",
" if results:\n",
" schedule = \"; \".join([f\"{course} ({duration})\" for course, duration in results])\n",
" return f\"Training schedule for {role}: {schedule}\"\n",
" else:\n",
" return \"No training schedule found for that role.\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "bcfb6523",
"metadata": {},
"outputs": [],
"source": [
"# Tool schema definitions\n",
"employee_tool = {\n",
" \"name\": \"get_employee_info\",\n",
" \"description\": \"Retrieve onboarding information about a new employee.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"employee_name\": {\"type\": \"string\", \"description\": \"The full name of the employee.\"}\n",
" },\n",
" \"required\": [\"employee_name\"],\n",
" },\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
"metadata": {},
"outputs": [],
"source": [
"training_tool = {\n",
" \"name\": \"get_training_schedule\",\n",
" \"description\": \"Get the training schedule for a given role.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"role\": {\"type\": \"string\", \"description\": \"The job role of the employee.\"}\n",
" },\n",
" \"required\": [\"role\"],\n",
" },\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c91d012e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"tools = [{\"type\": \"function\", \"function\": employee_tool},\n",
" {\"type\": \"function\", \"function\": training_tool}]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "956c3b61",
"metadata": {},
"outputs": [],
"source": [
"# -------------------- MULTI-MODAL --------------------\n",
"def artist(topic):\n",
" prompt = f\"A friendly HR welcome image showing {topic}, office vibes, smiling team, pop-art style\"\n",
" image_response = openai.images.generate(\n",
" model=\"dall-e-3\",\n",
" prompt=prompt,\n",
" size=\"1024x1024\",\n",
" response_format=\"b64_json\"\n",
" )\n",
" img_base64 = image_response.data[0].b64_json\n",
" img_data = base64.b64decode(img_base64)\n",
" return Image.open(BytesIO(img_data))\n",
"\n",
"def talker(message):\n",
" response = openai.audio.speech.create(\n",
" model=\"gpt-4o-mini-tts\",\n",
" voice=\"alloy\",\n",
" input=message\n",
" )\n",
" return response.content"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "8eca803e",
"metadata": {},
"outputs": [],
"source": [
"# -------------------- AGENT LOGIC --------------------\n",
"\n",
"def handle_tool_calls(message):\n",
" responses, topics = [], []\n",
" for call in message.tool_calls:\n",
" if call.function.name == \"get_employee_info\":\n",
" args = json.loads(call.function.arguments)\n",
" name = args.get(\"employee_name\")\n",
" topics.append(name)\n",
" info = get_employee_info(name)\n",
" responses.append({\"role\": \"tool\", \"content\": info, \"tool_call_id\": call.id})\n",
" elif call.function.name == \"get_training_schedule\":\n",
" args = json.loads(call.function.arguments)\n",
" role = args.get(\"role\")\n",
" topics.append(role)\n",
" info = get_training_schedule(role)\n",
" responses.append({\"role\": \"tool\", \"content\": info, \"tool_call_id\": call.id})\n",
" return responses, topics\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "2c27c4ba-8ed5-492f-add1-02ce9c81d34c",
"metadata": {},
"outputs": [],
"source": [
"def chat(history):\n",
" history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
" topics, image = [], None\n",
"\n",
" while response.choices[0].finish_reason == \"tool_calls\":\n",
" msg = response.choices[0].message\n",
" responses, topics = handle_tool_calls(msg)\n",
" messages.append(msg)\n",
" messages.extend(responses)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
"\n",
" reply = response.choices[0].message.content\n",
" voice = talker(reply)\n",
"\n",
" if topics:\n",
" image = artist(topics[0])\n",
"\n",
" return history + [{\"role\": \"assistant\", \"content\": reply}], voice, image"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "773a9f11-557e-43c9-ad50-56cbec3a0f8f",
"metadata": {},
"outputs": [],
"source": [
"# -------------------- GRADIO UI --------------------\n",
"\n",
"def put_message_in_chatbot(message, history):\n",
" return \"\", history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
"with gr.Blocks() as ui:\n",
" gr.Markdown(\"## 🧑‍💼 WelcomeAI — Your HR Onboarding Companion\")\n",
" with gr.Row():\n",
" chatbot = gr.Chatbot(height=500, type=\"messages\")\n",
" image_output = gr.Image(height=500, interactive=False)\n",
" with gr.Row():\n",
" audio_output = gr.Audio(autoplay=True)\n",
" with gr.Row():\n",
" message = gr.Textbox(label=\"Ask me about onboarding, training, or company info:\")\n",
"\n",
" message.submit(put_message_in_chatbot, [message, chatbot], [message, chatbot]).then(\n",
" chat, chatbot, [chatbot, audio_output, image_output]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "728a12c5-adc3-415d-bb05-82beb73b079b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Rerunning server... use `close()` to stop if you need to change `launch()` parameters.\n",
"----\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": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ui.launch(inbrowser=True, auth=(\"hradmin\", \"welcome123\"))"
]
}
],
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}