siddharth24m commited on
Commit
b1d039c
·
verified ·
1 Parent(s): 1336d53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py CHANGED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+
10
+
11
+
12
+ load_dotenv(override=True)
13
+ load_dotenv("./key.env")
14
+
15
+ def push(text):
16
+ requests.post(
17
+ "https://api.pushover.net/1/messages.json",
18
+ data={
19
+ "token": os.getenv("PUSHOVER_TOKEN"),
20
+ "user": os.getenv("PUSHOVER_USER"),
21
+ "message": text,
22
+ }
23
+ )
24
+
25
+
26
+ def record_user_details(email, name="Name not provided", notes="not provided"):
27
+ push(f"Recording {name} with email {email} and notes {notes}")
28
+ return {"recorded": "ok"}
29
+
30
+ def record_unknown_question(question):
31
+ push(f"Recording {question}")
32
+ return {"recorded": "ok"}
33
+
34
+ record_user_details_json = {
35
+ "name": "record_user_details",
36
+ "description": "Use this tool to record that a user is interested in being in touch and provided an email address",
37
+ "parameters": {
38
+ "type": "object",
39
+ "properties": {
40
+ "email": {
41
+ "type": "string",
42
+ "description": "The email address of this user"
43
+ },
44
+ "name": {
45
+ "type": "string",
46
+ "description": "The user's name, if they provided it"
47
+ }
48
+ ,
49
+ "notes": {
50
+ "type": "string",
51
+ "description": "Any additional information about the conversation that's worth recording to give context"
52
+ }
53
+ },
54
+ "required": ["email"],
55
+ "additionalProperties": False
56
+ }
57
+ }
58
+
59
+ record_unknown_question_json = {
60
+ "name": "record_unknown_question",
61
+ "description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer",
62
+ "parameters": {
63
+ "type": "object",
64
+ "properties": {
65
+ "question": {
66
+ "type": "string",
67
+ "description": "The question that couldn't be answered"
68
+ },
69
+ },
70
+ "required": ["question"],
71
+ "additionalProperties": False
72
+ }
73
+ }
74
+
75
+ tools = [{"type": "function", "function": record_user_details_json},
76
+ {"type": "function", "function": record_unknown_question_json}]
77
+
78
+
79
+ class Me:
80
+
81
+ def __init__(self):
82
+ self.openai = OpenAI()
83
+ self.name = "siddharth machhoya"
84
+ reader = PdfReader("./1_foundations/me/linkedin.pdf")
85
+ reader2 = PdfReader("./1_foundations/me/resume.pdf")
86
+ self.linkedin = ""
87
+ for page in reader.pages:
88
+ text = page.extract_text()
89
+ if text:
90
+ self.linkedin += text
91
+ self.resume = ""
92
+ for page in reader2.pages:
93
+ text = page.extract_text()
94
+ if text:
95
+ self.resume += text
96
+ with open("./1_foundations/me/summary.txt", "r", encoding="utf-8") as f:
97
+ self.summary = f.read()
98
+
99
+
100
+ def handle_tool_call(self, tool_calls):
101
+ results = []
102
+ for tool_call in tool_calls:
103
+ tool_name = tool_call.function.name
104
+ arguments = json.loads(tool_call.function.arguments)
105
+ print(f"Tool called: {tool_name}", flush=True)
106
+ tool = globals().get(tool_name)
107
+ result = tool(**arguments) if tool else {}
108
+ results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
109
+ return results
110
+
111
+ def system_prompt(self):
112
+ system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
113
+ particularly questions related to {self.name}'s career, background, skills and experience. \
114
+ Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
115
+ You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
116
+ Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
117
+ 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, even if it's about something trivial or unrelated to career. \
118
+ 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. "
119
+
120
+ system_prompt += f"\n\n## Summary:\n{self.summary}{self.resume}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
121
+ system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
122
+ return system_prompt
123
+
124
+ def chat(self, message, history):
125
+
126
+ google_api_key = os.getenv('GOOGLE_API_KEY')
127
+
128
+ self.gemini = OpenAI(api_key= google_api_key, base_url="https://generativelanguage.googleapis.com/v1beta/openai/")
129
+
130
+
131
+
132
+ messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
133
+ done = False
134
+
135
+
136
+ while not done:
137
+ response = self.gemini .chat.completions.create(model="gemini-2.5-flash", messages=messages, tools=tools)
138
+ if response.choices[0].finish_reason=="tool_calls":
139
+ message = response.choices[0].message
140
+ tool_calls = message.tool_calls
141
+ results = self.handle_tool_call(tool_calls)
142
+ messages.append(message)
143
+ messages.extend(results)
144
+ else:
145
+ done = True
146
+ return response.choices[0].message.content
147
+
148
+
149
+ if __name__ == "__main__":
150
+ me = Me()
151
+ gr.ChatInterface(me.chat).launch()
152
+