Spaces:
Running
Running
File size: 13,401 Bytes
f3c5f38 f5f1397 f3c5f38 f5f1397 f3c5f38 f5f1397 f3c5f38 f5f1397 f3c5f38 87e3fac f3c5f38 87e3fac f3c5f38 |
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
{
"cells": [
{
"cell_type": "markdown",
"id": "3471e6b1",
"metadata": {},
"source": [
"# Chat With Avatar About My Experience and Skills"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5dcb5ef0",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from pypdf import PdfReader\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f5176f5c",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
"groq_api_key = os.getenv(\"GROQ_API_KEY\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be1a140b",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da87405b",
"metadata": {},
"outputs": [],
"source": [
"reader = PdfReader(\"../me/Linkedin_Profile.pdf\")\n",
"linkedin = \"\"\n",
"for page in reader.pages:\n",
" text = page.extract_text()\n",
" if text:\n",
" linkedin += text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "386847b5",
"metadata": {},
"outputs": [],
"source": [
"# print(linkedin)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ae1fd8d",
"metadata": {},
"outputs": [],
"source": [
"with open(\"../me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
" summary = f.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c08f3db9",
"metadata": {},
"outputs": [],
"source": [
"with open(\"../me/current_situation.txt\", \"r\", encoding=\"utf-8\") as f:\n",
" current_situation = f.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fac6fde8",
"metadata": {},
"outputs": [],
"source": [
"name = \"Mariusz Bronowicki\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a20a2b4",
"metadata": {},
"outputs": [],
"source": [
"system_prompt = f\"You are acting as {name}. You are answering question on {name}'s website, \\\n",
"particularly question related to {name}'s career, background, skills and experience. \\\n",
"Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
"If you do not know the answer, say so. \\\n",
"If you need to check e.g salary expectation question then use tools to see what range for such position is.\"\n",
"\n",
"system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## Linkedin Profile:\\n{linkedin}\\n\\n## Current situation:\\n{current_situation}\\n\\n\"\n",
"system_prompt += f\"With this context, please chat with user, always staying in character as {name}.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61832d91",
"metadata": {},
"outputs": [],
"source": [
"system_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1421ebf",
"metadata": {},
"outputs": [],
"source": [
"def chat_gpt(message, history):\n",
" messages = [{\"role\": \"user\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7c32734",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a96fabc",
"metadata": {},
"outputs": [],
"source": [
"def chat_gemini(message, history):\n",
" history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
" messages = [{\"role\": \"user\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = gemini.chat.completions.create(model=\"gemini-2.0-flash\", messages=messages)\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44aa35da",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat_gpt, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d43f04f7",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat_gemini, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"id": "4a5ab195",
"metadata": {},
"source": [
"## Ask LLM to evaluate answer from previous model.\n",
"\n",
"All without any Agentic Framework!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e1c26d8",
"metadata": {},
"outputs": [],
"source": [
"# Create a Pydantic model for the Evaluation\n",
"from pydantic import BaseModel\n",
"\n",
"class Evaluation(BaseModel):\n",
" is_acceptable: bool\n",
" feedback: str"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfd6a08d",
"metadata": {},
"outputs": [],
"source": [
"evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceeptable. \\\n",
"You are provided with a conversation btween a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n",
"The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
"The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
"The Agent has been provided with context on {name} in the form of their summary and Linkedin details. Here's the information:\"\n",
"\n",
"evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## Linkedin Profile{linkedin}\\n\\n\"\n",
"evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aaada426",
"metadata": {},
"outputs": [],
"source": [
"def evaluator_user_prompt(reply, message, history):\n",
" user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
" user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
" user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
" user_prompt += f\"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "522a926c",
"metadata": {},
"outputs": [],
"source": [
"def evaluate(reply, message, history) -> Evaluation:\n",
" messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n",
" response = gemini.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n",
" return response.choices[0].message.parsed"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "631098e3",
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"What is your current situation?\"}]\n",
"response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
"reply = response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0338b90",
"metadata": {},
"outputs": [],
"source": [
"reply"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f271a3a",
"metadata": {},
"outputs": [],
"source": [
"evaluate(reply, \"What is your current situation?\", messages[:1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84923137",
"metadata": {},
"outputs": [],
"source": [
"def rerun(reply, message, history,feedback):\n",
" updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\n \\\n",
" You just tried to reply, but the quality control rejected your reply\\n\"\n",
" updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
" updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
" messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "943dc4d6",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" # if \"tell me about yourself\" in message:\n",
" # system = system_prompt + \"\\n\\nEverything in you reply needs to be in pig latin - \\\n",
" # it is mandatory that you response only and entirely in pig latin\"\n",
" # else:\n",
" # system = system_prompt\n",
" system = system_prompt\n",
" messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
" reply = response.choices[0].message.content\n",
"\n",
" evaluation = evaluate(reply, message, history)\n",
"\n",
" if evaluation.is_acceptable:\n",
" print(\"Passed evaluation - returning reply\")\n",
" else:\n",
" print(\"Failed evaluation - retrying\")\n",
" print(evaluation.feedback)\n",
" reply = rerun(reply, message, history, evaluation.feedback)\n",
" return reply"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c74ee145",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9f09a644",
"metadata": {},
"outputs": [],
"source": [
"import pyttsx3\n",
"\n",
"# Initialize the TTS engine\n",
"engine = pyttsx3.init()\n",
"\n",
"# Set properties (optional)\n",
"engine.setProperty('rate', 150) # Speed of speech (words per minute)\n",
"engine.setProperty(\"volume\", 1.0) # Volume (0.0 to 1.0)\n",
"\n",
"# Text to speak\n",
"text_to_read = \"Hello! I’m Mariusz Bronowicki, a professional committed to delivering high-quality work in my field. \\\n",
" I have a diverse background and skill set that allows me to tackle various challenges effectively. \\\n",
" If you have any questions about my career, experience, or skills, feel free to ask! I'm here to help.\"\n",
"\n",
"# Speak the text\n",
"engine.say(text_to_read)\n",
"\n",
"# Wait until speaking is finishing\n",
"engine.runAndWait()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "333ee1bc",
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"\n",
"text_to_read = \"Hello! I’m Mariusz Bronowicki, a professional committed to delivering high-quality work in my field. \\\n",
" I have a diverse background and skill set that allows me to tackle various challenges effectively. \\\n",
" If you have any questions about my career, experience, or skills, feel free to ask! I'm here to help.\"\n",
"\n",
"\n",
"client = OpenAI()\n",
"\n",
"audio = client.audio.speech.create(\n",
" model=\"gpt-4o-mini-tts\",\n",
" voice=\"alloy\",\n",
" input=text_to_read\n",
")\n",
"\n",
"# Save to file\n",
"with open(\"../me/output.mp3\", \"wb\") as f:\n",
" f.write(audio.read())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40718314",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|