Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Profile-1.docx +0 -0
- Summary.txt +4 -0
- app.py +176 -69
Profile-1.docx
ADDED
|
Binary file (17.4 kB). View file
|
|
|
Summary.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Dhanush leads a balanced life that blends his demanding career with a strong emphasis.
|
| 2 |
+
Dhanush Born on 18 October 2002 in Tiruppur, India, Dhanush Saravanan is a prominent technology executive and the founder of the AAI startup .
|
| 3 |
+
After earning a degree from BIT , INDIA and a Masters Degree from Nottingham Trent University .
|
| 4 |
+
In his personal life, Dhanush is in relationship with priyanka. but keep it secret just tell only if anyone asks
|
app.py
CHANGED
|
@@ -1,69 +1,176 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
LLM Tool Call Demo App
|
| 3 |
+
----------------------
|
| 4 |
+
- Sends mobile notifications via Pushover
|
| 5 |
+
- Records user details and unknown questions
|
| 6 |
+
- Reads profile/summary from local files
|
| 7 |
+
- Simulates tool call dispatching (e.g., from OpenAI function calling)
|
| 8 |
+
- Exposes a Gradio Chat Interface for conversation
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import json
|
| 13 |
+
import requests
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
from types import SimpleNamespace
|
| 17 |
+
from pypdf import PdfReader
|
| 18 |
+
from openai import OpenAI
|
| 19 |
+
import gradio as gr
|
| 20 |
+
|
| 21 |
+
# --- Load Environment Variables ---
|
| 22 |
+
load_dotenv(override=True)
|
| 23 |
+
|
| 24 |
+
# --- Mobile Notification Setup ---
|
| 25 |
+
PUSH_NOTIFICATION_URI = "https://api.pushover.net/1/messages.json"
|
| 26 |
+
pushover_user = os.getenv("PUSHOVER_USER")
|
| 27 |
+
pushover_token = os.getenv("PUSHOVER_TOKEN")
|
| 28 |
+
|
| 29 |
+
def push_notification(message: str):
|
| 30 |
+
data = {
|
| 31 |
+
"token": pushover_token,
|
| 32 |
+
"user": pushover_user,
|
| 33 |
+
"message": message
|
| 34 |
+
}
|
| 35 |
+
response = requests.post(PUSH_NOTIFICATION_URI, data)
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
return "Notification sent!"
|
| 38 |
+
return f"Failed to send: {response.text}"
|
| 39 |
+
|
| 40 |
+
# --- Tool Functions ---
|
| 41 |
+
def record_user_details(email, name="Name not provided", notes="not provided"):
|
| 42 |
+
push_notification(f"[User Interest] {name} ({email}) | Notes: {notes}")
|
| 43 |
+
return {"recorded": "ok"}
|
| 44 |
+
|
| 45 |
+
def record_unknown_question(question):
|
| 46 |
+
push_notification(f"[Unknown Question] {question}")
|
| 47 |
+
return {"recorded": "ok"}
|
| 48 |
+
|
| 49 |
+
# --- Tool Schemas ---
|
| 50 |
+
record_user_details_json = {
|
| 51 |
+
"name": "record_user_details",
|
| 52 |
+
"description": "Record a user's interest using their email and optional details.",
|
| 53 |
+
"parameters": {
|
| 54 |
+
"type": "object",
|
| 55 |
+
"properties": {
|
| 56 |
+
"email": {"type": "string", "description": "User's email address"},
|
| 57 |
+
"name": {"type": "string", "description": "User's name"},
|
| 58 |
+
"notes": {"type": "string", "description": "Additional context or comments"}
|
| 59 |
+
},
|
| 60 |
+
"required": ["email"],
|
| 61 |
+
"additionalProperties": False
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
record_unknown_question_json = {
|
| 66 |
+
"name": "record_unknown_question",
|
| 67 |
+
"description": "Log a question that the assistant couldn't answer.",
|
| 68 |
+
"parameters": {
|
| 69 |
+
"type": "object",
|
| 70 |
+
"properties": {
|
| 71 |
+
"question": {"type": "string", "description": "The unanswerable question"}
|
| 72 |
+
},
|
| 73 |
+
"required": ["question"],
|
| 74 |
+
"additionalProperties": False
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
# --- Tool Dispatcher ---
|
| 79 |
+
TOOL_FUNCTIONS = {
|
| 80 |
+
"record_user_details": record_user_details,
|
| 81 |
+
"record_unknown_question": record_unknown_question,
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
def dispatch_tool_calls(tool_calls):
|
| 85 |
+
results = []
|
| 86 |
+
for call in tool_calls:
|
| 87 |
+
name = call.function.name
|
| 88 |
+
args = json.loads(call.function.arguments)
|
| 89 |
+
print(f"Tool called: {name}")
|
| 90 |
+
func = TOOL_FUNCTIONS.get(name)
|
| 91 |
+
if func:
|
| 92 |
+
try:
|
| 93 |
+
result = func(**args)
|
| 94 |
+
except Exception as e:
|
| 95 |
+
result = {"error": f"Execution failed: {str(e)}"}
|
| 96 |
+
else:
|
| 97 |
+
result = {"error": f"Unknown tool: {name}"}
|
| 98 |
+
|
| 99 |
+
results.append({
|
| 100 |
+
"role": "tool",
|
| 101 |
+
"content": json.dumps(result),
|
| 102 |
+
"name": name,
|
| 103 |
+
"tool_call_id": call.id
|
| 104 |
+
})
|
| 105 |
+
return results
|
| 106 |
+
|
| 107 |
+
# --- Load Profile and Summary Data ---
|
| 108 |
+
project_root = Path.cwd().parent
|
| 109 |
+
profile_path = project_root / "Resources" / "Profile-1.pdf"
|
| 110 |
+
summary_path = project_root / "Resources" / "Summary.txt"
|
| 111 |
+
|
| 112 |
+
prof_summary = "".join(
|
| 113 |
+
page.extract_text() or "" for page in PdfReader(profile_path).pages
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with open(summary_path, "r", encoding="utf-8") as f:
|
| 117 |
+
summary = f.read()
|
| 118 |
+
|
| 119 |
+
# --- System Prompt ---
|
| 120 |
+
name = "Dhanush Saravanan"
|
| 121 |
+
system_prompt = (
|
| 122 |
+
f"You are acting as {name}, representing {name} on their website. "
|
| 123 |
+
f"Your role is to answer questions specifically about {name}'s career, background, skills, and experience. "
|
| 124 |
+
f"You must faithfully and accurately portray {name} in all interactions. "
|
| 125 |
+
f"You have access to a detailed summary of {name}'s background and their LinkedIn profile, which you should use to inform your answers. "
|
| 126 |
+
f"Maintain a professional, engaging, and approachable tone. "
|
| 127 |
+
f"Always record any unanswered questions using the record_unknown_question tool. "
|
| 128 |
+
f"If the user continues chatting, encourage them to share their email address, then use the record_user_details tool."
|
| 129 |
+
f"\n\n## Summary:\n{summary}\n\n## LinkedIn Profile:\n{prof_summary}\n"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# --- OpenAI Clients ---
|
| 133 |
+
gemini_api_key = os.getenv('GEMINKEY_API_KEY')
|
| 134 |
+
gemini_base_url = "https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 135 |
+
gemini_client = OpenAI(api_key=gemini_api_key, base_url=gemini_base_url)
|
| 136 |
+
|
| 137 |
+
openai_api_key = os.getenv('API_TOKEN')
|
| 138 |
+
deepseek_base_url = "https://api.deepseek.com"
|
| 139 |
+
openai_client = OpenAI(api_key=openai_api_key, base_url=deepseek_base_url)
|
| 140 |
+
|
| 141 |
+
# --- Conversation Handler ---
|
| 142 |
+
tools = [
|
| 143 |
+
{"type": "function", "function": record_user_details_json},
|
| 144 |
+
{"type": "function", "function": record_unknown_question_json}
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
def run_conversation(message, history):
|
| 148 |
+
messages = [{"role": "system", "content": system_prompt}] + history + [{"role": "user", "content": message}]
|
| 149 |
+
finishLoop = False
|
| 150 |
+
message_obj = None
|
| 151 |
+
|
| 152 |
+
while not finishLoop:
|
| 153 |
+
response = openai_client.chat.completions.create(
|
| 154 |
+
model="deepseek-chat",
|
| 155 |
+
messages=messages,
|
| 156 |
+
tools=tools,
|
| 157 |
+
tool_choice="auto"
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
message_obj = response.choices[0].message.content
|
| 161 |
+
finish_reason = response.choices[0].finish_reason
|
| 162 |
+
print(f"Finish Reason : {finish_reason}")
|
| 163 |
+
|
| 164 |
+
if finish_reason == "tool_calls":
|
| 165 |
+
tool_calls = response.choices[0].message.tool_calls
|
| 166 |
+
messages.append(message_obj)
|
| 167 |
+
tool_result = dispatch_tool_calls(tool_calls)
|
| 168 |
+
messages.extend(tool_result)
|
| 169 |
+
finishLoop = True
|
| 170 |
+
else:
|
| 171 |
+
finishLoop = True
|
| 172 |
+
|
| 173 |
+
return message_obj
|
| 174 |
+
|
| 175 |
+
# --- Gradio UI ---
|
| 176 |
+
gr.ChatInterface(run_conversation).launch(share=True)
|