Dhanush7080 commited on
Commit
d9046ea
·
verified ·
1 Parent(s): c3289f2

Upload 3 files

Browse files
Files changed (3) hide show
  1. Profile-1.docx +0 -0
  2. Summary.txt +4 -0
  3. 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
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
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
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
66
-
67
-
68
- if __name__ == "__main__":
69
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)