File size: 5,682 Bytes
7e820ed |
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
{
"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
}
|