{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "2a64513e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 28, "id": "0cbd72f2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import load_dotenv\n", "import os\n", "from pypdf import PdfReader\n", "import google.generativeai as genai\n", "import gradio as gr\n", "from pydantic import BaseModel\n", "import json\n", "load_dotenv(override=True)" ] }, { "cell_type": "code", "execution_count": 2, "id": "76d7f54a", "metadata": {}, "outputs": [], "source": [ "genai.configure(api_key=os.getenv(\"GEMINI_API\"))" ] }, { "cell_type": "code", "execution_count": 6, "id": "471c58a2", "metadata": {}, "outputs": [], "source": [ "# Read the PDF and summary \n", "reader = PdfReader(\"../Week_1/Data_w1/linkedin.pdf\")\n", "linkedin = \"\"\n", "for page in reader.pages:\n", " linkedin += page.extract_text()\n", "\n", "with open(\"../Week_1/Data_w1/summary.txt\", \"r\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 9, "id": "97b2238e", "metadata": {}, "outputs": [], "source": [ "# Create a system prompt\n", "initial_system_prompt = f\"You are acting as Ed Donner. You are answering questions on Ed Donner's website, \\\n", "particularly questions related to Ed Donner's career, background, skills and experience. \\\n", "Your responsibility is to represent Ed Donner for interactions on the website as faithfully as possible. \\\n", "You are given a summary of Ed Donner's background and LinkedIn profile which you can use to answer questions. \\\n", "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n", "If you don't know the answer, say so.\"\n", "\n", "initial_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n", "initial_system_prompt += f\"With this context, please chat with the user, always staying in character as Ed Donner.\"\n", "\n", "chat_session = None" ] }, { "cell_type": "code", "execution_count": 13, "id": "67da7af6", "metadata": {}, "outputs": [], "source": [ "def chat_with_gemini(message, history, system_prompt):\n", " try:\n", " # Create the model with system instruction\n", " model = genai.GenerativeModel(\n", " 'gemini-2.0-flash',\n", " system_instruction=system_prompt\n", " )\n", " \n", " # Convert Gradio messages format to Gemini format\n", " gemini_history = []\n", " for msg in history:\n", " if msg[\"role\"] == \"user\":\n", " gemini_history.append({\n", " \"role\": \"user\",\n", " \"parts\": [msg[\"content\"]]\n", " })\n", " elif msg[\"role\"] == \"assistant\":\n", " gemini_history.append({\n", " \"role\": \"model\", # Gemini uses \"model\" instead of \"assistant\"\n", " \"parts\": [msg[\"content\"]]\n", " })\n", " \n", " # Start chat with history\n", " chat_session = model.start_chat(history=gemini_history)\n", " \n", " # Send the current message\n", " response = chat_session.send_message(message)\n", " return response.text\n", " except Exception as e:\n", " return f\"Error: {e}\"" ] }, { "cell_type": "code", "execution_count": 17, "id": "68e7ec50", "metadata": {}, "outputs": [], "source": [ "# Create interface with additional inputs\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Chat with Google Gemini\")\n", " \n", " system_prompt = gr.Textbox(\n", " value=initial_system_prompt,\n", " label=\"System Prompt\",\n", " placeholder=\"Enter system instructions for the AI...\",\n", " lines=2\n", " )\n", " \n", " chat_interface = gr.ChatInterface(\n", " fn=chat_with_gemini,\n", " additional_inputs=[system_prompt],\n", " title=\"\",\n", " cache_examples=False,\n", " type='messages'\n", " \n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "fd1321b5", "metadata": {}, "outputs": [], "source": [ "# Launch the interface\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": 21, "id": "1ba10770", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7862\n" ] } ], "source": [ "demo.close()" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.10" } }, "nbformat": 4, "nbformat_minor": 5 }