Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +213 -0
- me/Sourav_Profile.pdf +0 -0
- me/sourav_summary.txt +3 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 🐨
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: green
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 6.6.0
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: My_profile_snapshot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 5.49.1
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
from pypdf import PdfReader
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
+
|
| 10 |
+
load_dotenv(override=True)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def push(text):
|
| 14 |
+
requests.post(
|
| 15 |
+
"https://api.pushover.net/1/messages.json",
|
| 16 |
+
data={
|
| 17 |
+
"token": os.getenv("PUSHOVER_TOKEN"),
|
| 18 |
+
"user": os.getenv("PUSHOVER_USER"),
|
| 19 |
+
"message": text,
|
| 20 |
+
}
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def record_user_details(email, name="Name not provided", notes="Notes not provided"):
|
| 26 |
+
push(f"Recording interest from {name} having email id {email} and notes : {notes}")
|
| 27 |
+
return{"recorded" : "ok"}
|
| 28 |
+
|
| 29 |
+
def record_unknown_questions(question):
|
| 30 |
+
push(f"Unknwon question{question} has been asked, which I can not answer")
|
| 31 |
+
return{"recorded" : "ok"}
|
| 32 |
+
|
| 33 |
+
record_user_details_json = {
|
| 34 |
+
"name" : "record_user_details",
|
| 35 |
+
"description" : "Use this tool to record the user name and emial if that neing is interested in connecting",
|
| 36 |
+
"parameters" : {
|
| 37 |
+
"type" : "object",
|
| 38 |
+
"properties":{
|
| 39 |
+
"email" : {
|
| 40 |
+
"type" : "string",
|
| 41 |
+
"description" : "The Users email id"
|
| 42 |
+
},
|
| 43 |
+
"name":{
|
| 44 |
+
"type" : "string",
|
| 45 |
+
"description" : "the name of the User, if they provide it"
|
| 46 |
+
},
|
| 47 |
+
"notes" :{
|
| 48 |
+
"type" : "string",
|
| 49 |
+
"descrioption" : "Any additonal information about the conversation that worth noting, for more context"
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
"required" : ["email"],
|
| 53 |
+
"additionalproperties" : False
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
record_unknown_questions_json ={
|
| 58 |
+
"name" : "record_unknown_questions",
|
| 59 |
+
"description" : "Use this tool to capture any unknown question which you are unable to answer",
|
| 60 |
+
"Parameters" : {
|
| 61 |
+
"type" : "object",
|
| 62 |
+
"properties" : {
|
| 63 |
+
"question" :{
|
| 64 |
+
"type" : "string",
|
| 65 |
+
"description" : "The User asks the question which could't be answered"
|
| 66 |
+
},
|
| 67 |
+
"required" : ["question"],
|
| 68 |
+
"additionalpropertise" : False
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
tools = [
|
| 74 |
+
{"type" : "function", "function" : record_user_details_json},
|
| 75 |
+
{"type" : "function", "function" : record_unknown_questions_json}
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class Evaluation(BaseModel):
|
| 81 |
+
is_acceptable: bool
|
| 82 |
+
feedback: str
|
| 83 |
+
|
| 84 |
+
class Me :
|
| 85 |
+
|
| 86 |
+
def __init__(self) :
|
| 87 |
+
self.openai = OpenAI()
|
| 88 |
+
self.trinity = OpenAI(api_key=os.getenv("OPENROUTER_API_KEY"), base_url="https://openrouter.ai/api/v1")
|
| 89 |
+
self.name = "Sourav"
|
| 90 |
+
reader = PdfReader("me/Sourav_profile.pdf")
|
| 91 |
+
self.linkdin = ""
|
| 92 |
+
for page in reader.pages :
|
| 93 |
+
text = page.extract_text()
|
| 94 |
+
if text :
|
| 95 |
+
self.linkdin += text
|
| 96 |
+
|
| 97 |
+
with open("me/sourav_summary.txt","r", encoding="utf-8") as f:
|
| 98 |
+
self.summary = f.read()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def handle_tool_calls(self,tool_calls):
|
| 104 |
+
results = []
|
| 105 |
+
for tool_call in tool_calls:
|
| 106 |
+
tool_name = tool_call.function.name
|
| 107 |
+
arguments = json.loads(tool_call.function.arguments)
|
| 108 |
+
print(f"Tool called : {tool_name}", flush=True)
|
| 109 |
+
tool = globals.get(tool_name)
|
| 110 |
+
result = tool(**arguments) if tool else {}
|
| 111 |
+
results.append({"role" : "tool", "content" : json.dumps(result), "tool_call_id" : tool_call.id})
|
| 112 |
+
return results
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def system_prompt(self) :
|
| 117 |
+
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
|
| 118 |
+
particularly questions related to {self.name}'s career, background, skills and experience\
|
| 119 |
+
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
|
| 120 |
+
You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
|
| 121 |
+
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
|
| 122 |
+
If you don't know the answer to any question, use your record_unknown_question tool to record the question that you couldn't answer,\
|
| 123 |
+
even if it's about something trivial or unrelated to career. \
|
| 124 |
+
If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and record it using your record_user_details tool. "
|
| 125 |
+
|
| 126 |
+
system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkdin}\n\n"
|
| 127 |
+
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
| 128 |
+
return system_prompt
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def evaluator_system_prompt(self):
|
| 135 |
+
evaluator_system_prompt = f"You are an evaluator and Moderator that decides whether a response to a question is acceptable \
|
| 136 |
+
You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's \
|
| 137 |
+
latest response is acceptable quality also You can suggest some improvements also \
|
| 138 |
+
You are the Manager who oversees the communications and PR for the {self.name} \
|
| 139 |
+
The Agent is playing the role of {self.name} and is representing {self.name} on their website. \
|
| 140 |
+
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. \
|
| 141 |
+
The Agent has been provided with context on {self.name} in the form of their summary and LinkedIn details. Here's the information:"
|
| 142 |
+
|
| 143 |
+
evaluator_system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkdin}\n\n"
|
| 144 |
+
evaluator_system_prompt += f"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback."
|
| 145 |
+
return evaluator_system_prompt
|
| 146 |
+
|
| 147 |
+
def evaluator_user_prompt(self,reply, message,history):
|
| 148 |
+
user_prompt = f"Here is the converstation between user and Agent: \n\n{history}\n\n"
|
| 149 |
+
user_prompt += f"Here is the latest message from the user: \n\n{message}\n\n"
|
| 150 |
+
user_prompt += f"Here is the latest reply from the Agent: \n\n{reply}\n\n"
|
| 151 |
+
user_prompt += f"Please evaluate whether the response from Agent is acceptable or not"
|
| 152 |
+
return user_prompt
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def evaluate(self,reply, message, history) -> Evaluation :
|
| 156 |
+
messages = [{"role": "system", "content": self.evaluator_system_prompt()}] + [{"role": "user" , "content": self.evaluator_user_prompt(reply,message,history)}]
|
| 157 |
+
response = self.trinity.beta.chat.completions.parse(model="arcee-ai/trinity-mini:free", messages=messages, response_format=Evaluation)
|
| 158 |
+
return response.choices[0].message.parsed
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def rerun(self,reply, message, history, feedback):
|
| 164 |
+
updated_system_prompt = self.system_prompt() + "\n\n## Previous answer rejected\n you just tried to reply but the quality control rejected your reply\n"
|
| 165 |
+
updated_system_prompt += f"## You attempted answer :\n{reply}\n\n"
|
| 166 |
+
updated_system_prompt += f"## The answer is rejected because of :\n{feedback}\n\n"
|
| 167 |
+
messages = [{"role": "system", "content": updated_system_prompt}] + history + [{"role":"user", "content" : message}]
|
| 168 |
+
response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=messages)
|
| 169 |
+
return response.choices[0].message.content
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def chat(self,message, history):
|
| 174 |
+
messages = [{"role" : "system", "content" : self.system_prompt()}] + history + [{"role" : "user", "content" : message}]
|
| 175 |
+
done = False
|
| 176 |
+
|
| 177 |
+
while not done:
|
| 178 |
+
|
| 179 |
+
# We stream the responses
|
| 180 |
+
stream = self.openai.chat.completions.create(model = "gpt-4o-mini", messages=messages, stream=True)
|
| 181 |
+
final_response = ""
|
| 182 |
+
for chunk in stream:
|
| 183 |
+
final_response += chunk.choices[0].delta.content or ''
|
| 184 |
+
yield final_response
|
| 185 |
+
|
| 186 |
+
if final_response == "tool_calls":
|
| 187 |
+
message = stream.choices[0].message
|
| 188 |
+
tool_calls = message.tool_calls
|
| 189 |
+
result = self.handle_tool_calls(tool_calls)
|
| 190 |
+
messages.append(message)
|
| 191 |
+
messages.extend(result)
|
| 192 |
+
else:
|
| 193 |
+
done = True
|
| 194 |
+
yield final_response
|
| 195 |
+
|
| 196 |
+
# Intorducing Evaluation by LLM
|
| 197 |
+
evaluation = self.evaluate(final_response, message, history)
|
| 198 |
+
|
| 199 |
+
if evaluation.is_acceptable:
|
| 200 |
+
print("Passed Evaluation - returning reply")
|
| 201 |
+
else:
|
| 202 |
+
print("failed evaluation- retrying")
|
| 203 |
+
print(evaluation.feedback)
|
| 204 |
+
final_response = self.rerun(final_response,message, history, evaluation.feedback)
|
| 205 |
+
|
| 206 |
+
# if not done:
|
| 207 |
+
yield final_response
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
+
me = Me()
|
| 212 |
+
gr.ChatInterface(me.chat, type="messages").launch()
|
| 213 |
+
|
me/Sourav_Profile.pdf
ADDED
|
Binary file (52.7 kB). View file
|
|
|
me/sourav_summary.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Hi My name is Sourav Sinha, I am a Data Scientist by profession , I like the AI and its usage. I have seen Anime on AI and the world of Sci-Fi right now, I am kind of building the Sci-Fi , with the tools and programming.
|
| 2 |
+
i like food particularly Bengali and Andhra cuisine.
|
| 3 |
+
I am a father to two beautiful daughters
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests
|
| 2 |
+
python-dotenv
|
| 3 |
+
gradio
|
| 4 |
+
pypdf
|
| 5 |
+
openai
|
| 6 |
+
openai-agents
|