Ayush239 commited on
Commit
f1dfb77
·
verified ·
1 Parent(s): f0a0781

Update app.py

Browse files

live an dworking

Files changed (1) hide show
  1. app.py +18 -69
app.py CHANGED
@@ -1,7 +1,3 @@
1
- # app.py — NVIDIA NIM + Tool Calling + Gradio Chatbot
2
- # Model: meta/llama3-8b-instruct (supports OpenAI-style tools)
3
- # Works with your nvapi-xxxx key on HuggingFace Spaces.
4
-
5
  import os
6
  import json
7
  import requests
@@ -9,10 +5,7 @@ from pypdf import PdfReader
9
  from openai import OpenAI
10
  import gradio as gr
11
 
12
- # ===============================
13
- # ENVIRONMENT CONFIG
14
- # ===============================
15
- OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") # your nvapi-xxxx key
16
  BASE_URL = "https://integrate.api.nvidia.com/v1"
17
  MODEL = "meta/llama3-8b-instruct"
18
 
@@ -21,10 +14,6 @@ PUSHOVER_USER = os.environ.get("PUSHOVER_USER")
21
 
22
  client = OpenAI(api_key=OPENAI_API_KEY, base_url=BASE_URL)
23
 
24
-
25
- # ===============================
26
- # OPTIONAL: Pushover notification
27
- # ===============================
28
  def push(text):
29
  try:
30
  if not PUSHOVER_TOKEN or not PUSHOVER_USER:
@@ -42,27 +31,17 @@ def push(text):
42
  except Exception as e:
43
  print("Pushover failed:", e)
44
 
45
-
46
- # ===============================
47
- # TOOL IMPLEMENTATIONS
48
- # ===============================
49
  def record_user_details(email, name="Name not provided", notes="not provided"):
50
- push(f"Lead captured → {name} | {email} | Notes: {notes}")
51
  return {"status": "ok", "email": email, "name": name, "notes": notes}
52
 
53
  def record_unknown_question(question):
54
- push(f"Unknown question: {question}")
55
  return {"status": "ok", "question": question}
56
 
57
-
58
- # Register tools globally
59
  globals()["record_user_details"] = record_user_details
60
  globals()["record_unknown_question"] = record_unknown_question
61
 
62
-
63
- # ===============================
64
- # TOOL JSON DEFINITIONS
65
- # ===============================
66
  tools = [
67
  {
68
  "type": "function",
@@ -84,7 +63,7 @@ tools = [
84
  "type": "function",
85
  "function": {
86
  "name": "record_unknown_question",
87
- "description": "Record any question the assistant cannot answer.",
88
  "parameters": {
89
  "type": "object",
90
  "properties": {
@@ -96,66 +75,48 @@ tools = [
96
  }
97
  ]
98
 
99
-
100
- # ===============================
101
- # MAIN ASSISTANT CLASS
102
- # ===============================
103
  class Me:
104
  def __init__(self):
105
  self.name = "Ayush Tyagi"
106
  self.summary = ""
107
  self.linkedin_text = ""
108
 
109
- # Load summary file
110
  if os.path.exists("me/summary.txt"):
111
  self.summary = open("me/summary.txt", "r", encoding="utf-8").read()
112
 
113
- # Load PDF if exists
114
  pdf_path = "me/Ayush_linkdin.pdf"
115
  if os.path.exists(pdf_path):
116
  text = []
117
  reader = PdfReader(pdf_path)
118
  for page in reader.pages:
119
- page_text = page.extract_text()
120
- if page_text:
121
- text.append(page_text)
122
  self.linkedin_text = "\n\n".join(text)
123
 
124
  def system_prompt(self):
125
  return f"""
126
- You are acting as {self.name}. You answer questions about his background,
127
- skills, experience, portfolio, and career.
128
-
129
- If you DO NOT know something:
130
- ➡ Use the tool: record_unknown_question
131
-
132
- If the user is interested in working with Ayush:
133
- ➡ Ask for their email and call: record_user_details
134
 
135
- Be friendly, confident, and professional.
136
-
137
- ### Summary:
138
  {self.summary}
139
 
140
- ### LinkedIn Extract:
141
  {self.linkedin_text}
142
  """
143
 
144
- # ===============================
145
- # CHAT LOOP WITH TOOL CALLING
146
- # ===============================
147
  def chat(self, message, history):
148
  messages = [{"role": "system", "content": self.system_prompt()}]
149
 
150
- # FIXED: Handle both tuple-history & dict-history
151
  for item in history:
152
  if isinstance(item, (list, tuple)) and len(item) == 2:
153
- user_msg, bot_msg = item
154
- if user_msg:
155
- messages.append({"role": "user", "content": user_msg})
156
- if bot_msg:
157
- messages.append({"role": "assistant", "content": bot_msg})
158
-
159
  elif isinstance(item, dict) and "role" in item and "content" in item:
160
  messages.append({"role": item["role"], "content": item["content"]})
161
 
@@ -174,34 +135,22 @@ Be friendly, confident, and professional.
174
  finish = choice.finish_reason
175
  msg = choice.message
176
 
177
- # ---- TOOL CALL ----
178
  if finish == "tool_calls":
179
  messages.append(msg.dict())
180
-
181
  for tool_call in msg.tool_calls:
182
  func = tool_call.function
183
  name = func.name
184
  args = json.loads(func.arguments)
185
-
186
- tool_fn = globals().get(name)
187
- result = tool_fn(**args)
188
-
189
  messages.append({
190
  "role": "tool",
191
  "tool_call_id": tool_call.id,
192
  "content": json.dumps(result)
193
  })
194
-
195
- # Let model continue after tool execution
196
  continue
197
 
198
- # ---- NORMAL RESPONSE ----
199
  return msg.content
200
 
201
-
202
- # ===============================
203
- # GRADIO APP
204
- # ===============================
205
  me = Me()
206
 
207
  ui = gr.ChatInterface(
 
 
 
 
 
1
  import os
2
  import json
3
  import requests
 
5
  from openai import OpenAI
6
  import gradio as gr
7
 
8
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
 
 
 
9
  BASE_URL = "https://integrate.api.nvidia.com/v1"
10
  MODEL = "meta/llama3-8b-instruct"
11
 
 
14
 
15
  client = OpenAI(api_key=OPENAI_API_KEY, base_url=BASE_URL)
16
 
 
 
 
 
17
  def push(text):
18
  try:
19
  if not PUSHOVER_TOKEN or not PUSHOVER_USER:
 
31
  except Exception as e:
32
  print("Pushover failed:", e)
33
 
 
 
 
 
34
  def record_user_details(email, name="Name not provided", notes="not provided"):
35
+ push(f"Lead → {name} | {email} | {notes}")
36
  return {"status": "ok", "email": email, "name": name, "notes": notes}
37
 
38
  def record_unknown_question(question):
39
+ push(f"Unknown {question}")
40
  return {"status": "ok", "question": question}
41
 
 
 
42
  globals()["record_user_details"] = record_user_details
43
  globals()["record_unknown_question"] = record_unknown_question
44
 
 
 
 
 
45
  tools = [
46
  {
47
  "type": "function",
 
63
  "type": "function",
64
  "function": {
65
  "name": "record_unknown_question",
66
+ "description": "Record an unknown question.",
67
  "parameters": {
68
  "type": "object",
69
  "properties": {
 
75
  }
76
  ]
77
 
 
 
 
 
78
  class Me:
79
  def __init__(self):
80
  self.name = "Ayush Tyagi"
81
  self.summary = ""
82
  self.linkedin_text = ""
83
 
 
84
  if os.path.exists("me/summary.txt"):
85
  self.summary = open("me/summary.txt", "r", encoding="utf-8").read()
86
 
 
87
  pdf_path = "me/Ayush_linkdin.pdf"
88
  if os.path.exists(pdf_path):
89
  text = []
90
  reader = PdfReader(pdf_path)
91
  for page in reader.pages:
92
+ t = page.extract_text()
93
+ if t:
94
+ text.append(t)
95
  self.linkedin_text = "\n\n".join(text)
96
 
97
  def system_prompt(self):
98
  return f"""
99
+ You are acting as {self.name}. You answer questions about his background, skills and experience.
100
+ If you don't know something, use record_unknown_question.
101
+ If user is interested, ask for email and use record_user_details.
 
 
 
 
 
102
 
103
+ Summary:
 
 
104
  {self.summary}
105
 
106
+ LinkedIn:
107
  {self.linkedin_text}
108
  """
109
 
 
 
 
110
  def chat(self, message, history):
111
  messages = [{"role": "system", "content": self.system_prompt()}]
112
 
 
113
  for item in history:
114
  if isinstance(item, (list, tuple)) and len(item) == 2:
115
+ u, a = item
116
+ if u:
117
+ messages.append({"role": "user", "content": u})
118
+ if a:
119
+ messages.append({"role": "assistant", "content": a})
 
120
  elif isinstance(item, dict) and "role" in item and "content" in item:
121
  messages.append({"role": item["role"], "content": item["content"]})
122
 
 
135
  finish = choice.finish_reason
136
  msg = choice.message
137
 
 
138
  if finish == "tool_calls":
139
  messages.append(msg.dict())
 
140
  for tool_call in msg.tool_calls:
141
  func = tool_call.function
142
  name = func.name
143
  args = json.loads(func.arguments)
144
+ result = globals()[name](**args)
 
 
 
145
  messages.append({
146
  "role": "tool",
147
  "tool_call_id": tool_call.id,
148
  "content": json.dumps(result)
149
  })
 
 
150
  continue
151
 
 
152
  return msg.content
153
 
 
 
 
 
154
  me = Me()
155
 
156
  ui = gr.ChatInterface(