Ayush239 commited on
Commit
130c5cc
·
verified ·
1 Parent(s): 82b3117

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
app.py CHANGED
@@ -17,12 +17,11 @@ client = OpenAI(api_key=OPENAI_API_KEY, base_url=BASE_URL)
17
  def push(text):
18
  try:
19
  if not PUSHOVER_TOKEN or not PUSHOVER_USER:
20
- print("Pushover not configured:", text)
21
  return
22
  requests.post(
23
  "https://api.pushover.net/1/messages.json",
24
  data={"token": PUSHOVER_TOKEN, "user": PUSHOVER_USER, "message": text},
25
- timeout=10
26
  )
27
  except:
28
  pass
@@ -49,7 +48,7 @@ tools = [
49
  "properties": {
50
  "email": {"type": "string"},
51
  "name": {"type": "string"},
52
- "notes": {"type": "string"},
53
  },
54
  "required": ["email"]
55
  }
@@ -59,7 +58,7 @@ tools = [
59
  "type": "function",
60
  "function": {
61
  "name": "record_unknown_question",
62
- "description": "Record an unknown question.",
63
  "parameters": {
64
  "type": "object",
65
  "properties": {"question": {"type": "string"}},
@@ -78,21 +77,21 @@ class Me:
78
  if os.path.exists("me/summary.txt"):
79
  self.summary = open("me/summary.txt", "r", encoding="utf-8").read()
80
 
81
- pdf = "me/Ayush_linkdin.pdf"
82
- if os.path.exists(pdf):
83
  text = []
84
- reader = PdfReader(pdf)
85
- for p in reader.pages:
86
- t = p.extract_text()
87
  if t:
88
  text.append(t)
89
  self.linkedin = "\n\n".join(text)
90
 
91
  def system_prompt(self):
92
  return f"""
93
- You are acting as {self.name}. Answer questions about his background, skills and experience.
94
- If unknown, use record_unknown_question.
95
- If user is interested, ask for email and call record_user_details.
96
 
97
  Summary:
98
  {self.summary}
@@ -104,9 +103,8 @@ LinkedIn:
104
  def chat(self, message, history):
105
  messages = [{"role": "system", "content": self.system_prompt()}]
106
 
107
- # Add history
108
- for role, content in history:
109
- messages.append({"role": role, "content": content})
110
 
111
  messages.append({"role": "user", "content": message})
112
 
@@ -116,16 +114,18 @@ LinkedIn:
116
  messages=messages,
117
  tools=tools,
118
  tool_choice="auto",
119
- max_tokens=600
120
  )
121
 
122
- msg = response.choices[0].message
123
- finish = response.choices[0].finish_reason
 
124
 
125
  if finish == "tool_calls":
126
- for tool_call in msg.tool_calls:
127
- func = tool_call.function
128
- result = globals()[func.name](**json.loads(func.arguments))
 
129
  messages.append({"role": "tool", "content": json.dumps(result)})
130
  continue
131
 
@@ -134,11 +134,11 @@ LinkedIn:
134
 
135
  me = Me()
136
 
137
- def respond(user_message, chat_history):
138
- output = me.chat(user_message, chat_history)
139
- chat_history.append(("user", user_message))
140
- chat_history.append(("assistant", output))
141
- return "", chat_history
142
 
143
 
144
  with gr.Blocks(css="""
@@ -155,7 +155,7 @@ with gr.Blocks(css="""
155
  btn_projects = gr.Button("Latest Projects")
156
 
157
  chatbot = gr.Chatbot(height=450)
158
- user_input = gr.Textbox(placeholder="Type here...")
159
 
160
  user_input.submit(respond, [user_input, chatbot], [user_input, chatbot])
161
 
@@ -168,6 +168,5 @@ with gr.Blocks(css="""
168
  btn_projects.click(lambda: "Show Ayush Tyagi’s latest projects.", None, user_input)
169
  btn_projects.click(respond, [user_input, chatbot], [user_input, chatbot])
170
 
171
-
172
  if __name__ == "__main__":
173
- ui.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
 
17
  def push(text):
18
  try:
19
  if not PUSHOVER_TOKEN or not PUSHOVER_USER:
 
20
  return
21
  requests.post(
22
  "https://api.pushover.net/1/messages.json",
23
  data={"token": PUSHOVER_TOKEN, "user": PUSHOVER_USER, "message": text},
24
+ timeout=5
25
  )
26
  except:
27
  pass
 
48
  "properties": {
49
  "email": {"type": "string"},
50
  "name": {"type": "string"},
51
+ "notes": {"type": "string"}
52
  },
53
  "required": ["email"]
54
  }
 
58
  "type": "function",
59
  "function": {
60
  "name": "record_unknown_question",
61
+ "description": "Record unknown question.",
62
  "parameters": {
63
  "type": "object",
64
  "properties": {"question": {"type": "string"}},
 
77
  if os.path.exists("me/summary.txt"):
78
  self.summary = open("me/summary.txt", "r", encoding="utf-8").read()
79
 
80
+ pdf_path = "me/Ayush_linkdin.pdf"
81
+ if os.path.exists(pdf_path):
82
  text = []
83
+ reader = PdfReader(pdf_path)
84
+ for page in reader.pages:
85
+ t = page.extract_text()
86
  if t:
87
  text.append(t)
88
  self.linkedin = "\n\n".join(text)
89
 
90
  def system_prompt(self):
91
  return f"""
92
+ You are acting as {self.name}. You answer questions about his background, skills and experience.
93
+ If unsure, use record_unknown_question.
94
+ If user is interested, ask for an email and call record_user_details.
95
 
96
  Summary:
97
  {self.summary}
 
103
  def chat(self, message, history):
104
  messages = [{"role": "system", "content": self.system_prompt()}]
105
 
106
+ for role, text in history:
107
+ messages.append({"role": role, "content": text})
 
108
 
109
  messages.append({"role": "user", "content": message})
110
 
 
114
  messages=messages,
115
  tools=tools,
116
  tool_choice="auto",
117
+ max_tokens=400
118
  )
119
 
120
+ choice = response.choices[0]
121
+ msg = choice.message
122
+ finish = choice.finish_reason
123
 
124
  if finish == "tool_calls":
125
+ for call in msg.tool_calls:
126
+ fname = call.function.name
127
+ args = json.loads(call.function.arguments)
128
+ result = globals()[fname](**args)
129
  messages.append({"role": "tool", "content": json.dumps(result)})
130
  continue
131
 
 
134
 
135
  me = Me()
136
 
137
+ def respond(user_message, history):
138
+ bot_reply = me.chat(user_message, history)
139
+ history.append(("user", user_message))
140
+ history.append(("assistant", bot_reply))
141
+ return "", history
142
 
143
 
144
  with gr.Blocks(css="""
 
155
  btn_projects = gr.Button("Latest Projects")
156
 
157
  chatbot = gr.Chatbot(height=450)
158
+ user_input = gr.Textbox(placeholder="Type your message...")
159
 
160
  user_input.submit(respond, [user_input, chatbot], [user_input, chatbot])
161
 
 
168
  btn_projects.click(lambda: "Show Ayush Tyagi’s latest projects.", None, user_input)
169
  btn_projects.click(respond, [user_input, chatbot], [user_input, chatbot])
170
 
 
171
  if __name__ == "__main__":
172
+ ui.launch()