Spaces:
Sleeping
Sleeping
Update app.py
Browse filesreverted to the working version
app.py
CHANGED
|
@@ -38,8 +38,6 @@ def record_unknown_question(question):
|
|
| 38 |
writer.writerow([question])
|
| 39 |
return {"recorded": "ok"}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
# JSON definitions for tools
|
| 44 |
record_user_details_json = {
|
| 45 |
"name": "record_user_details",
|
|
@@ -75,6 +73,21 @@ tools = [
|
|
| 75 |
]
|
| 76 |
|
| 77 |
class Me:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Handle tool calls
|
| 79 |
def handle_tool_call(self, tool_calls):
|
| 80 |
results = []
|
|
@@ -84,10 +97,22 @@ class Me:
|
|
| 84 |
tool = globals().get(tool_name)
|
| 85 |
result = tool(**arguments) if tool else {}
|
| 86 |
results.append({"role": "tool", "content": json.dumps(result), "tool_call_id": tool_call.id})
|
| 87 |
-
|
| 88 |
return results
|
| 89 |
|
| 90 |
# System prompt for LLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
expertise_text = ", ".join(self.expertise)
|
| 92 |
|
| 93 |
education_text = ""
|
|
@@ -113,9 +138,35 @@ class Me:
|
|
| 113 |
f"## Interests:\n{', '.join(self.personal_info.get('personal_interests', []))}\n\n"
|
| 114 |
f"## Travel History:\n{', '.join(self.personal_info.get('travel_history', []))}\n\n"
|
| 115 |
f"## Education:\n{education_text}\n\n"
|
|
|
|
| 116 |
f"## Experience:\n{experience_text}\n\n"
|
| 117 |
f"## Friends (for fun):\n{friends_text}\n\n"
|
| 118 |
f"## LinkedIn Profile:\nhttps://www.linkedin.com/in/sneha-leela-0a450349/\n\n"
|
| 119 |
f"Chat with the user staying in character as {self.personal_info['name']}."
|
| 120 |
)
|
| 121 |
-
return system_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
writer.writerow([question])
|
| 39 |
return {"recorded": "ok"}
|
| 40 |
|
|
|
|
|
|
|
| 41 |
# JSON definitions for tools
|
| 42 |
record_user_details_json = {
|
| 43 |
"name": "record_user_details",
|
|
|
|
| 73 |
]
|
| 74 |
|
| 75 |
class Me:
|
| 76 |
+
def __init__(self):
|
| 77 |
+
self.openai = gemini
|
| 78 |
+
self.name = "SnehaLeela"
|
| 79 |
+
|
| 80 |
+
# Load profile JSON
|
| 81 |
+
with open("profile.json", "r", encoding="utf-8") as f:
|
| 82 |
+
self.profile = json.load(f)
|
| 83 |
+
|
| 84 |
+
# Set attributes for easier access
|
| 85 |
+
self.personal_info = self.profile.get("personal_info", {})
|
| 86 |
+
self.expertise = self.profile.get("expertise", [])
|
| 87 |
+
self.experience = self.profile.get("experience", [])
|
| 88 |
+
self.education = self.profile.get("education", [])
|
| 89 |
+
self.friends = self.profile.get("friends", [])
|
| 90 |
+
|
| 91 |
# Handle tool calls
|
| 92 |
def handle_tool_call(self, tool_calls):
|
| 93 |
results = []
|
|
|
|
| 97 |
tool = globals().get(tool_name)
|
| 98 |
result = tool(**arguments) if tool else {}
|
| 99 |
results.append({"role": "tool", "content": json.dumps(result), "tool_call_id": tool_call.id})
|
|
|
|
| 100 |
return results
|
| 101 |
|
| 102 |
# System prompt for LLM
|
| 103 |
+
def system_prompt(self):
|
| 104 |
+
# Combine experience into text
|
| 105 |
+
experience_text = ""
|
| 106 |
+
for company in self.experience:
|
| 107 |
+
experience_text += f"{company['company']}"
|
| 108 |
+
if 'location' in company:
|
| 109 |
+
experience_text += f" ({company['location']})"
|
| 110 |
+
for role in company.get('roles', []):
|
| 111 |
+
experience_text += f"\n- {role['title']} ({role.get('years', '')})"
|
| 112 |
+
for hl in role.get('highlights', []):
|
| 113 |
+
experience_text += f"\n • {hl}"
|
| 114 |
+
experience_text += "\n"
|
| 115 |
+
|
| 116 |
expertise_text = ", ".join(self.expertise)
|
| 117 |
|
| 118 |
education_text = ""
|
|
|
|
| 138 |
f"## Interests:\n{', '.join(self.personal_info.get('personal_interests', []))}\n\n"
|
| 139 |
f"## Travel History:\n{', '.join(self.personal_info.get('travel_history', []))}\n\n"
|
| 140 |
f"## Education:\n{education_text}\n\n"
|
| 141 |
+
f"## Expertise:\n{expertise_text}\n\n"
|
| 142 |
f"## Experience:\n{experience_text}\n\n"
|
| 143 |
f"## Friends (for fun):\n{friends_text}\n\n"
|
| 144 |
f"## LinkedIn Profile:\nhttps://www.linkedin.com/in/sneha-leela-0a450349/\n\n"
|
| 145 |
f"Chat with the user staying in character as {self.personal_info['name']}."
|
| 146 |
)
|
| 147 |
+
return system_prompt
|
| 148 |
+
|
| 149 |
+
# Main chat function
|
| 150 |
+
def chat(self, message, history):
|
| 151 |
+
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
| 152 |
+
done = False
|
| 153 |
+
while not done:
|
| 154 |
+
response = self.openai.chat.completions.create(
|
| 155 |
+
model="gemini-2.5-flash-preview-05-20",
|
| 156 |
+
messages=messages,
|
| 157 |
+
tools=tools
|
| 158 |
+
)
|
| 159 |
+
if response.choices[0].finish_reason == "tool_calls":
|
| 160 |
+
message = response.choices[0].message
|
| 161 |
+
tool_calls = message.tool_calls
|
| 162 |
+
results = self.handle_tool_call(tool_calls)
|
| 163 |
+
messages.append(message)
|
| 164 |
+
messages.extend(results)
|
| 165 |
+
else:
|
| 166 |
+
done = True
|
| 167 |
+
return response.choices[0].message.content
|
| 168 |
+
|
| 169 |
+
# Launch Gradio interface
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
me = Me()
|
| 172 |
+
gr.ChatInterface(me.chat, type="messages").launch(share=True)
|