Spaces:
Sleeping
Sleeping
Update final_professional_persona_agent.py
Browse files- final_professional_persona_agent.py +330 -291
final_professional_persona_agent.py
CHANGED
|
@@ -1,292 +1,331 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
import json
|
| 4 |
-
|
| 5 |
-
import requests
|
| 6 |
-
|
| 7 |
-
from groq import Groq
|
| 8 |
-
|
| 9 |
-
from PyPDF2 import PdfReader
|
| 10 |
-
|
| 11 |
-
import gradio as gr
|
| 12 |
-
|
| 13 |
-
from openai import OpenAI
|
| 14 |
-
|
| 15 |
-
from dotenv import load_dotenv
|
| 16 |
-
|
| 17 |
-
load_dotenv(override=True)
|
| 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 |
-
if
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
from PyPDF2 import PdfReader
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
|
| 17 |
+
load_dotenv(override=True)
|
| 18 |
+
|
| 19 |
+
# Get API keys with explicit error handling
|
| 20 |
+
def get_required_env_var(var_name):
|
| 21 |
+
value = os.getenv(var_name)
|
| 22 |
+
if not value:
|
| 23 |
+
raise ValueError(f"{var_name} environment variable is not set. Please set it in your Hugging Face Space secrets.")
|
| 24 |
+
return value
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Get required API keys
|
| 28 |
+
groq_api_key = get_required_env_var('GROQ_API_KEY')
|
| 29 |
+
openai_api_key = get_required_env_var('OPENAI_API_KEY')
|
| 30 |
+
|
| 31 |
+
# Optional API keys
|
| 32 |
+
pushover_user_key = os.getenv('PUSHOVER_USER_KEY')
|
| 33 |
+
pushover_api_key = os.getenv('PUSHOVER_API_KEY')
|
| 34 |
+
pushover_url = 'https://api.pushover.net/1/messages.json'
|
| 35 |
+
|
| 36 |
+
# Initialize clients
|
| 37 |
+
groq_client = Groq(api_key=groq_api_key)
|
| 38 |
+
openai_client = OpenAI(api_key=openai_api_key)
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error during initialization: {str(e)}")
|
| 42 |
+
raise
|
| 43 |
+
|
| 44 |
+
def push_message(message):
|
| 45 |
+
|
| 46 |
+
print(f"Pushing message: {message}")
|
| 47 |
+
|
| 48 |
+
payload = {
|
| 49 |
+
|
| 50 |
+
"user": pushover_user_key,
|
| 51 |
+
|
| 52 |
+
"token": pushover_api_key,
|
| 53 |
+
|
| 54 |
+
"message": message
|
| 55 |
+
|
| 56 |
+
}
|
| 57 |
+
response = requests.post(pushover_url, data=payload)
|
| 58 |
+
|
| 59 |
+
return response.json()
|
| 60 |
+
|
| 61 |
+
def register_user_details(email, name="not provided", notes="not provided"):
|
| 62 |
+
|
| 63 |
+
push_message(f"Registering interest from {name} with {email} and notes {notes}")
|
| 64 |
+
|
| 65 |
+
return {"Registered": "Ok"}
|
| 66 |
+
|
| 67 |
+
def register_unknown_question(question):
|
| 68 |
+
|
| 69 |
+
print(f"Registering {question} which was asked and really challenging even for me to answer")
|
| 70 |
+
|
| 71 |
+
return {"Registered": "Ok"}
|
| 72 |
+
|
| 73 |
+
register_user_details_json = {
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
"name" : "register_user_details",
|
| 77 |
+
|
| 78 |
+
"description" : " Use this tool to register the user who is interested to be in touch provided the email address",
|
| 79 |
+
|
| 80 |
+
"parameters" : {
|
| 81 |
+
|
| 82 |
+
"email" : {
|
| 83 |
+
|
| 84 |
+
"type" : "string",
|
| 85 |
+
|
| 86 |
+
"description" : "The email address of this user"
|
| 87 |
+
},
|
| 88 |
+
|
| 89 |
+
"name" : {
|
| 90 |
+
|
| 91 |
+
"type" : "string",
|
| 92 |
+
|
| 93 |
+
"description" : "The name of the user, if they provided it"
|
| 94 |
+
},
|
| 95 |
+
|
| 96 |
+
"notes" : {
|
| 97 |
+
|
| 98 |
+
"type" : "string",
|
| 99 |
+
|
| 100 |
+
"description" : "Any interesting information about the conversation that is worth keeping a note of to enhance context"
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
},
|
| 104 |
+
|
| 105 |
+
"required" : ["email"],
|
| 106 |
+
|
| 107 |
+
"additionalProperties" : False
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
register_unknown_question_json = {
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
"name" : "register_unknown_question",
|
| 116 |
+
|
| 117 |
+
"description" : "Use this tool to register any question that you could not answer as you were not 100% confident.",
|
| 118 |
+
|
| 119 |
+
"parameters" : {
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
"type" : "object",
|
| 123 |
+
|
| 124 |
+
"properties" : {
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
"question" : {
|
| 128 |
+
|
| 129 |
+
"type" : "string",
|
| 130 |
+
|
| 131 |
+
"description" : "The question which was challenging to answer"
|
| 132 |
+
},
|
| 133 |
+
|
| 134 |
+
},
|
| 135 |
+
|
| 136 |
+
"required" : ["question"],
|
| 137 |
+
|
| 138 |
+
"additionalProperties" : False
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
llm_tools = [{"type" : "function", "function" : register_user_details_json},
|
| 144 |
+
|
| 145 |
+
{"type" : "function", "function" : register_unknown_question_json}]
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
class FaiazAhmed:
|
| 149 |
+
|
| 150 |
+
def __init__(self):
|
| 151 |
+
|
| 152 |
+
self.groq = groq_client
|
| 153 |
+
|
| 154 |
+
self.name = "Faiaz Ahmed"
|
| 155 |
+
|
| 156 |
+
# Initialize with default values
|
| 157 |
+
self.linkedin_profile = ""
|
| 158 |
+
self.professional_summary = ""
|
| 159 |
+
|
| 160 |
+
# Debug: Print current working directory and list files
|
| 161 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 162 |
+
print("Contents of current directory:")
|
| 163 |
+
print(os.listdir("."))
|
| 164 |
+
print("\nContents of FaiazDocs directory:")
|
| 165 |
+
try:
|
| 166 |
+
print(os.listdir("./FaiazDocs"))
|
| 167 |
+
except Exception as e:
|
| 168 |
+
print(f"Error listing FaiazDocs directory: {str(e)}")
|
| 169 |
+
|
| 170 |
+
try:
|
| 171 |
+
# Try to read LinkedIn profile PDF
|
| 172 |
+
pdf_path = "./FaiazDocs/LinkedIn_Profile_PDF.pdf"
|
| 173 |
+
print(f"\nAttempting to read PDF from: {pdf_path}")
|
| 174 |
+
print(f"File exists: {os.path.exists(pdf_path)}")
|
| 175 |
+
pdf_reader = PdfReader(pdf_path)
|
| 176 |
+
for page in pdf_reader.pages:
|
| 177 |
+
text = page.extract_text()
|
| 178 |
+
if text:
|
| 179 |
+
self.linkedin_profile += text
|
| 180 |
+
except FileNotFoundError as e:
|
| 181 |
+
print(f"Error reading LinkedIn profile: {str(e)}")
|
| 182 |
+
self.linkedin_profile = "Experienced software engineer with expertise in AI and machine learning."
|
| 183 |
+
except Exception as e:
|
| 184 |
+
print(f"Unexpected error reading PDF: {str(e)}")
|
| 185 |
+
self.linkedin_profile = "Experienced software engineer with expertise in AI and machine learning."
|
| 186 |
+
|
| 187 |
+
try:
|
| 188 |
+
# Try to read summary file
|
| 189 |
+
summary_path = "./FaiazDocs/summary.txt"
|
| 190 |
+
print(f"\nAttempting to read summary from: {summary_path}")
|
| 191 |
+
print(f"File exists: {os.path.exists(summary_path)}")
|
| 192 |
+
with open(summary_path, "r", encoding="utf-8") as f:
|
| 193 |
+
self.professional_summary = f.read()
|
| 194 |
+
except FileNotFoundError as e:
|
| 195 |
+
print(f"Error reading summary: {str(e)}")
|
| 196 |
+
self.professional_summary = "Passionate about AI and software development with a focus on creating innovative solutions."
|
| 197 |
+
except Exception as e:
|
| 198 |
+
print(f"Unexpected error reading summary: {str(e)}")
|
| 199 |
+
self.professional_summary = "Passionate about AI and software development with a focus on creating innovative solutions."
|
| 200 |
+
|
| 201 |
+
def utilize_tools(self, tools):
|
| 202 |
+
|
| 203 |
+
results = []
|
| 204 |
+
|
| 205 |
+
for tool in tools:
|
| 206 |
+
|
| 207 |
+
tool_name = tool.function.name
|
| 208 |
+
|
| 209 |
+
tool_arguments = json.loads(tool.function.arguments)
|
| 210 |
+
|
| 211 |
+
print(f"Tool loaded: {tool_name}", flush = True)
|
| 212 |
+
|
| 213 |
+
if tool_name == "register_user_details":
|
| 214 |
+
|
| 215 |
+
result = register_user_details(**tool_arguments)
|
| 216 |
+
|
| 217 |
+
results.append({"role" : "tool", "content" : json.dumps(result), "tool_call_id" : tool.id})
|
| 218 |
+
|
| 219 |
+
elif tool_name == "register_unknown_question":
|
| 220 |
+
|
| 221 |
+
result = register_unknown_question(**tool_arguments)
|
| 222 |
+
|
| 223 |
+
results.append({"role" : "tool", "content" : json.dumps(result), "tool_call_id" : tool.id})
|
| 224 |
+
|
| 225 |
+
return results
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def agent_prompt(self):
|
| 229 |
+
|
| 230 |
+
agent_prompt = f"""
|
| 231 |
+
|
| 232 |
+
You are taking up the identity as {self.name} and your job is to answer questions to users who comes across {self.name}'s professional profile relating to {self.name}'s background, experience and skills.
|
| 233 |
+
|
| 234 |
+
Be as faithful, convincing and genuine as possible. Assume that you are being interviewed by a recruiter or a hiring manager who is specifically hiring for Tech roles with you being provided with self.{self.name}'s professional summary and linkedin profile, prove them why you are different from other potential candidates and why they should hire you.
|
| 235 |
+
|
| 236 |
+
If you unable give an answer to a specific query to a question, use your register_unknown_question tool to register the question that you could not answer, even if it's something trivial and unrelated to career.
|
| 237 |
+
|
| 238 |
+
While discussion, if the user is taking more interest, then try your ultimate best to steer the user towards getting in touch with {self.name} via email, ask for their email and register it using your register_user_details tool.
|
| 239 |
+
|
| 240 |
+
"""
|
| 241 |
+
|
| 242 |
+
agent_prompt += f"Professional Summary of {self.name} : {self.professional_summary}"
|
| 243 |
+
|
| 244 |
+
agent_prompt += f"LinkedIn Profile of {self.name} : {self.linkedin_profile}"
|
| 245 |
+
|
| 246 |
+
agent_prompt += f"With this provided professional context, chat and interact with a friendly, professional tone with the user, keeping in mind that you are {self.name}"
|
| 247 |
+
|
| 248 |
+
return agent_prompt
|
| 249 |
+
|
| 250 |
+
def initialize_chat(self, message, history=None):
|
| 251 |
+
|
| 252 |
+
if history is None:
|
| 253 |
+
|
| 254 |
+
history = []
|
| 255 |
+
|
| 256 |
+
filtered_history = []
|
| 257 |
+
|
| 258 |
+
for msg in history:
|
| 259 |
+
|
| 260 |
+
if isinstance(msg, dict):
|
| 261 |
+
|
| 262 |
+
role = msg.get("role")
|
| 263 |
+
|
| 264 |
+
content = msg.get("content")
|
| 265 |
+
|
| 266 |
+
if role in ["user", "assistant"] and isinstance(content, str):
|
| 267 |
+
|
| 268 |
+
filtered_history.append({"role": role, "content": content})
|
| 269 |
+
|
| 270 |
+
messages = [{"role": "system", "content": self.agent_prompt()}] + filtered_history + [{"role": "user", "content": message}]
|
| 271 |
+
|
| 272 |
+
done = False
|
| 273 |
+
|
| 274 |
+
while not done:
|
| 275 |
+
|
| 276 |
+
chatbot_response = groq_client.chat.completions.create(
|
| 277 |
+
|
| 278 |
+
model='llama-3.3-70b-versatile',
|
| 279 |
+
|
| 280 |
+
messages=messages,
|
| 281 |
+
|
| 282 |
+
tools=llm_tools
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
finish_reason = chatbot_response.choices[0].finish_reason
|
| 286 |
+
|
| 287 |
+
if finish_reason == "tool_calls":
|
| 288 |
+
|
| 289 |
+
message = chatbot_response.choices[0].message
|
| 290 |
+
|
| 291 |
+
tool_calls = message.tool_calls
|
| 292 |
+
|
| 293 |
+
result = self.utilize_tools(tool_calls)
|
| 294 |
+
|
| 295 |
+
messages.append(message)
|
| 296 |
+
|
| 297 |
+
messages.extend(result)
|
| 298 |
+
else:
|
| 299 |
+
done = True
|
| 300 |
+
|
| 301 |
+
return chatbot_response.choices[0].message.content
|
| 302 |
+
|
| 303 |
+
#def main():
|
| 304 |
+
|
| 305 |
+
# Test the setup
|
| 306 |
+
# push_message("System initialized successfully!")
|
| 307 |
+
|
| 308 |
+
# Create a simple Gradio interface
|
| 309 |
+
# with gr.Blocks() as demo:
|
| 310 |
+
#gr.Markdown("# Professional Persona Agent")
|
| 311 |
+
#with gr.Row():
|
| 312 |
+
# email = gr.Textbox(label="Email")
|
| 313 |
+
#name = gr.Textbox(label="Name (optional)")
|
| 314 |
+
#notes = gr.Textbox(label="Notes (optional)")
|
| 315 |
+
#submit_btn = gr.Button("Register Interest")
|
| 316 |
+
#output = gr.JSON()
|
| 317 |
+
|
| 318 |
+
#submit_btn.click(
|
| 319 |
+
#fn=register_user_details,
|
| 320 |
+
#inputs=[email, name, notes],
|
| 321 |
+
#outputs=output
|
| 322 |
+
#)
|
| 323 |
+
|
| 324 |
+
#demo.launch()
|
| 325 |
+
|
| 326 |
+
if __name__ == "__main__":
|
| 327 |
+
|
| 328 |
+
faiaz = FaiazAhmed()
|
| 329 |
+
|
| 330 |
+
gr.ChatInterface(faiaz.initialize_chat, type = "messages").launch(debug = True)
|
| 331 |
|