Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import json
|
| 5 |
import gradio as gr
|
| 6 |
+
from pypdf import PdfReader
|
| 7 |
|
| 8 |
+
# ---------------------------
|
| 9 |
+
# Secrets from Hugging Face
|
| 10 |
+
# ---------------------------
|
| 11 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
PUSHOVER_USER = os.getenv("PUSHOVER_USER")
|
| 13 |
+
PUSHOVER_TOKEN = os.getenv("PUSHOVER_TOKEN")
|
| 14 |
+
PUSHOVER_URL = "https://api.pushover.net/1/messages.json"
|
| 15 |
|
| 16 |
+
# Initialize OpenAI client
|
| 17 |
+
openai = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# ---------------------------
|
| 20 |
+
# Push notification helper
|
| 21 |
+
# ---------------------------
|
| 22 |
+
def push(message):
|
| 23 |
+
print(f"Push: {message}")
|
| 24 |
+
payload = {"user": PUSHOVER_USER, "token": PUSHOVER_TOKEN, "message": message}
|
| 25 |
+
requests.post(PUSHOVER_URL, data=payload)
|
| 26 |
|
| 27 |
+
# ---------------------------
|
| 28 |
+
# Tool functions
|
| 29 |
+
# ---------------------------
|
| 30 |
+
def connect(email, name="no name", notes="no notes"):
|
| 31 |
+
push(f'Someone wants to connect with email: {email}, name: {name}, notes: {notes}')
|
| 32 |
+
return {"recorded": "ok"}
|
| 33 |
|
| 34 |
+
def unknown(message):
|
| 35 |
+
push(f'The question I could not answer was: {message}')
|
| 36 |
+
return {"recorded": "ok"}
|
| 37 |
|
| 38 |
+
# JSON schema for tools
|
| 39 |
+
connect_json = {
|
| 40 |
+
"name": "connect",
|
| 41 |
+
"description": "Use this tool when someone wants to connect",
|
| 42 |
+
"parameters": {
|
| 43 |
+
"type": "object",
|
| 44 |
+
"properties": {
|
| 45 |
+
"email": {"type": "string", "description": "Email of person wanting to connect"},
|
| 46 |
+
"name": {"type": "string", "description": "Name of person wanting to connect"},
|
| 47 |
+
"notes": {"type": "string", "description": "Additional notes"}
|
| 48 |
+
},
|
| 49 |
+
"required": ["email"],
|
| 50 |
+
"additionalProperties": False
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
|
| 54 |
+
unknown_json = {
|
| 55 |
+
"name": "unknown",
|
| 56 |
+
"description": "Use this tool when you don't know the answer to a question",
|
| 57 |
+
"parameters": {
|
| 58 |
+
"type": "object",
|
| 59 |
+
"properties": {
|
| 60 |
+
"message": {"type": "string", "description": "The question asked by the user"}
|
| 61 |
+
},
|
| 62 |
+
"required": ["message"],
|
| 63 |
+
"additionalProperties": False
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
|
| 67 |
+
tools = [{"type": "function", "function": connect_json}, {"type": "function", "function": unknown_json}]
|
|
|
|
| 68 |
|
| 69 |
+
def handle_tool_calls(tool_calls):
|
| 70 |
+
results = []
|
| 71 |
+
for tool_call in tool_calls:
|
| 72 |
+
name = tool_call.function.name
|
| 73 |
+
print(f'Tool called: {name}')
|
| 74 |
+
arguments = json.loads(tool_call.function.arguments)
|
| 75 |
|
| 76 |
+
if name == "unknown":
|
| 77 |
+
result = unknown(**arguments)
|
| 78 |
+
elif name == "connect":
|
| 79 |
+
result = connect(**arguments)
|
| 80 |
+
|
| 81 |
+
results.append({"role": "tool", "content": json.dumps(result), "tool_call_id": tool_call.id})
|
| 82 |
+
return results
|
| 83 |
+
|
| 84 |
+
# ---------------------------
|
| 85 |
+
# Load PDF & summary
|
| 86 |
+
# ---------------------------
|
| 87 |
+
reader = PdfReader("me/Profile.pdf")
|
| 88 |
+
linkedin = "".join([page.extract_text() or "" for page in reader.pages])
|
| 89 |
+
|
| 90 |
+
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
| 91 |
+
summary = f.read()
|
| 92 |
+
|
| 93 |
+
# ---------------------------
|
| 94 |
+
# System prompt
|
| 95 |
+
# ---------------------------
|
| 96 |
+
name = "Zalaid Butt"
|
| 97 |
+
system_prompt = f"""
|
| 98 |
+
If you don't know the answer, just say you don't know.
|
| 99 |
+
You are acting as {name}. You are answering questions on {name}'s website,
|
| 100 |
+
particularly related to {name}'s career, background, skills and experience.
|
| 101 |
+
Be professional and engaging, as if talking to a potential client or future employer.
|
| 102 |
+
If you don't know the answer, use the record_unknown_question tool.
|
| 103 |
+
If the user wants to connect, use the record_user_details tool.
|
| 104 |
"""
|
| 105 |
+
|
| 106 |
+
system_prompt += f"\n\n## Summary:\n{summary}\n\n## LinkedIn Profile:\n{linkedin}\n\n"
|
| 107 |
+
system_prompt += f"With this context, please chat with the user, always staying in character as {name}. "
|
| 108 |
+
system_prompt += "Always be concise; do not give any additional information if not asked. " \
|
| 109 |
+
"If you don't know the answer, use the tool but don't give wrong/fake answers."
|
| 110 |
+
|
| 111 |
+
# ---------------------------
|
| 112 |
+
# Chat function
|
| 113 |
+
# ---------------------------
|
| 114 |
+
def chat(message, history):
|
| 115 |
+
messages = [{"role": "system", "content": system_prompt}] + history + [{"role": "user", "content": message}]
|
| 116 |
+
done = False
|
| 117 |
+
while not done:
|
| 118 |
+
response = openai.chat.completions.create(
|
| 119 |
+
model="gpt-3.5-turbo",
|
| 120 |
+
messages=messages,
|
| 121 |
+
tools=tools
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
finish_reason = response.choices[0].finish_reason
|
| 125 |
+
|
| 126 |
+
if finish_reason == "tool_calls":
|
| 127 |
+
message_obj = response.choices[0].message
|
| 128 |
+
tool_calls = message_obj.tool_calls
|
| 129 |
+
result = handle_tool_calls(tool_calls)
|
| 130 |
+
|
| 131 |
+
messages.append(message_obj)
|
| 132 |
+
messages.extend(result)
|
| 133 |
+
else:
|
| 134 |
+
done = True
|
| 135 |
+
|
| 136 |
+
return response.choices[0].message.content
|
| 137 |
+
|
| 138 |
+
# ---------------------------
|
| 139 |
+
# Launch Gradio
|
| 140 |
+
# ---------------------------
|
| 141 |
+
gr.ChatInterface(chat, type="messages").launch()
|