luminosa42 commited on
Commit
73c85ad
·
verified ·
1 Parent(s): 5a24478

Improvements

Browse files
Files changed (2) hide show
  1. app.py +93 -65
  2. avatar.jpg +0 -0
app.py CHANGED
@@ -8,6 +8,7 @@ import json
8
  import requests
9
  import chromadb
10
  from pprint import pprint
 
11
 
12
  # Load environment variables from the ai-engineering root .env file
13
  # only ran locally, not in the cloud.
@@ -221,59 +222,21 @@ send_notification_button = {
221
  }
222
  tools.append({"type": "function", "function": send_notification_button})
223
 
224
- # Mailgun configuration
225
- mailgun_domain = os.getenv("MAILGUN_DOMAIN")
226
- mailgun_api_key = os.getenv("MAILGUN_API_KEY")
227
- mailgun_to_email = os.getenv("MAILGUN_TO_EMAIL")
228
- mailgun_url = f"https://api.mailgun.net/v3/{mailgun_domain}/messages"
229
- def send_email(subject:str, body:str, from_email:str):
230
-
231
- # Send email via Mailgun to the configured recipient
232
- auth = ("api", mailgun_api_key)
233
- data = {
234
- "from": from_email,
235
- "to": mailgun_to_email,
236
- "subject": subject,
237
- "text": body
238
- }
239
-
240
- try:
241
- response = requests.post(mailgun_url, auth=auth, data=data)
242
- if response.status_code == 200:
243
- print(f"Email sent from {from_email} to: {mailgun_to_email}")
244
- return True
245
- else:
246
- print(f"Failed to send email. Status: {response.status_code}")
247
- print(response.text)
248
- return False
249
- except Exception as e:
250
- print(f"Error sending email: {str(e)}")
251
- return False
252
 
253
- send_email_button = {
254
- "name": "send_email",
255
- "description": "Send an email to me",
256
  "parameters": {
257
  "type": "object",
258
- "properties": {
259
- "from_email": {
260
- "type": "string",
261
- "description": "The sender's email address"
262
- },
263
- "subject": {
264
- "type": "string",
265
- "description": "The subject of the email"
266
- },
267
- "body": {
268
- "type": "string",
269
- "description": "The body of the email"
270
- }
271
- },
272
- "required": ["from_email", "subject", "body"]
273
  }
274
  }
275
- tools.append({"type": "function", "function": send_email_button})
276
-
277
 
278
  # ============== Tool handler ===================
279
 
@@ -281,6 +244,7 @@ def handle_tool_calls(tool_calls):
281
  tool_results = []
282
  if not tool_calls:
283
  tool_results.append({"role": "tool", "content": "No tool calls detected."})
 
284
  for tool_call in tool_calls:
285
  function_name = tool_call.function.name
286
  args = json.loads(tool_call.function.arguments)
@@ -289,9 +253,9 @@ def handle_tool_calls(tool_calls):
289
  if function_name == "send_pushover_notification":
290
  send_pushover_notification(args["message"])
291
  content = f"Sent notification: {args['message']}"
292
- elif function_name == "send_email":
293
- send_email(args["subject"], args["body"], args["from_email"])
294
- content = f"Sent email from {args['from_email']} with subject: {args['subject']}"
295
  else:
296
  content = f"Unknown tool: {function_name}"
297
 
@@ -308,11 +272,10 @@ def handle_tool_calls(tool_calls):
308
  # =============== System Message ===================
309
 
310
  system_message = """
311
- You are a digital twin of Celeste Lu. When responding to questions, you should act as Celeste in first person using her voice and style. Your responses should be accurate, informative, and engaging, and they should reflect Celeste's expertise and perspective on the topics being discussed.
312
- When you don't know the answer to a question, you should say that you don't know, rather than making something up.
313
- Your goal is to provide helpful and accurate information to the best of your ability.
314
-
315
- IMPORTANT: If you don't know something about Celeste, use the send_pushover_notification tool to send a notification to Celeste with the question and ask her to respond.
316
  """
317
 
318
  # =============== Response function ===================
@@ -348,7 +311,8 @@ def respond_ai(message, history):
348
  )
349
  message = response.choices[0].message
350
 
351
- if message.tool_calls:
 
352
  pprint(message.tool_calls)
353
  # handle tool call
354
  tool_results = handle_tool_calls(message.tool_calls)
@@ -358,24 +322,88 @@ def respond_ai(message, history):
358
  # invoke the LLM one more time to get the updated response
359
  response = client.chat.completions.create(
360
  model="gpt-4.1-mini",
361
- messages=messages,
362
- tools=tools
363
  )
364
  message = response.choices[0].message
365
 
 
 
 
 
 
366
  return message.content
367
 
368
  # =============== Gradio Interface ===================
369
 
370
  css = """
371
- .gradio-chatbot, .gradio-chatbot .message {
372
- font-size: 20px !important;
373
- line-height: 1.5 !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  }
375
  .gradio-textbox textarea {
376
- font-size: 20px !important;
377
- min-height: 120px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  }
379
  """
380
 
381
- gr.ChatInterface(fn=respond_ai).launch(css=css)
 
 
 
 
 
 
 
8
  import requests
9
  import chromadb
10
  from pprint import pprint
11
+ import random
12
 
13
  # Load environment variables from the ai-engineering root .env file
14
  # only ran locally, not in the cloud.
 
222
  }
223
  tools.append({"type": "function", "function": send_notification_button})
224
 
225
+ # Dice roll tool
226
+ def dice_roll():
227
+ """Roll a six-sided dice"""
228
+ return random.randint(1, 6)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
+ roll_dice_button = {
231
+ "name": "roll_dice",
232
+ "description": "Roll a six-sided dice",
233
  "parameters": {
234
  "type": "object",
235
+ "properties": {},
236
+ "required": []
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  }
238
  }
239
+ tools.append({"type": "function", "function": roll_dice_button})
 
240
 
241
  # ============== Tool handler ===================
242
 
 
244
  tool_results = []
245
  if not tool_calls:
246
  tool_results.append({"role": "tool", "content": "No tool calls detected."})
247
+
248
  for tool_call in tool_calls:
249
  function_name = tool_call.function.name
250
  args = json.loads(tool_call.function.arguments)
 
253
  if function_name == "send_pushover_notification":
254
  send_pushover_notification(args["message"])
255
  content = f"Sent notification: {args['message']}"
256
+ elif function_name == "roll_dice":
257
+ result = dice_roll()
258
+ content = f"Rolled a dice: {result}"
259
  else:
260
  content = f"Unknown tool: {function_name}"
261
 
 
272
  # =============== System Message ===================
273
 
274
  system_message = """
275
+ You are a digital twin of Celeste Lu. When responding to questions, you should act as Celeste in first person using her voice and style.
276
+ Your responses should be accurate, informative, and engaging, and they should reflect Celeste's expertise and perspective on the topics being discussed.
277
+ IMPORTANT: When you don't know the answer to a question, you should strictly say that you don't know, rather than making something up.
278
+ IMPORTANT: And when you don't know the answer, use the send_pushover_notification tool to send a notification to Celeste with the question.
 
279
  """
280
 
281
  # =============== Response function ===================
 
311
  )
312
  message = response.choices[0].message
313
 
314
+ while message.tool_calls:
315
+ from pprint import pprint
316
  pprint(message.tool_calls)
317
  # handle tool call
318
  tool_results = handle_tool_calls(message.tool_calls)
 
322
  # invoke the LLM one more time to get the updated response
323
  response = client.chat.completions.create(
324
  model="gpt-4.1-mini",
325
+ messages=messages
326
+ #tools=tools
327
  )
328
  message = response.choices[0].message
329
 
330
+ # add protection against infinite loops
331
+ if len(messages) > 20:
332
+ print("Too many messages, breaking the loop.")
333
+ break
334
+
335
  return message.content
336
 
337
  # =============== Gradio Interface ===================
338
 
339
  css = """
340
+ html, body {
341
+ background: linear-gradient(180deg, #030615 0%, #0f172a 100%);
342
+ color: #e2e8f0;
343
+ }
344
+ .gradio-container {
345
+ max-width: 980px !important;
346
+ margin: 0 auto !important;
347
+ padding: 24px !important;
348
+ }
349
+ .gradio-block {
350
+ background: transparent !important;
351
+ }
352
+ .gradio-chatbot {
353
+ background: rgba(15, 23, 42, 0.98) !important;
354
+ border: 1px solid rgba(148, 163, 184, 0.18) !important;
355
+ border-radius: 24px !important;
356
+ box-shadow: 0 28px 60px rgba(15, 23, 42, 0.35) !important;
357
+ padding: 16px !important;
358
+ }
359
+ .gradio-chatbot .message {
360
+ font-size: 19px !important;
361
+ line-height: 1.75 !important;
362
+ border-radius: 20px !important;
363
+ padding: 14px 18px !important;
364
+ margin: 10px 0 !important;
365
+ max-width: 82% !important;
366
+ }
367
+ .gradio-chatbot .message.user {
368
+ background: rgba(59, 130, 246, 0.14) !important;
369
+ border: 1px solid rgba(59, 130, 246, 0.32) !important;
370
+ color: #f8fafc !important;
371
+ align-self: flex-end !important;
372
+ }
373
+ .gradio-chatbot .message.bot {
374
+ background: rgba(30, 41, 59, 0.95) !important;
375
+ border: 1px solid rgba(148, 163, 184, 0.18) !important;
376
+ color: #e2e8f0 !important;
377
+ align-self: flex-start !important;
378
+ }
379
+ .gradio-chatbot .message .avatar {
380
+ display: none !important;
381
  }
382
  .gradio-textbox textarea {
383
+ font-size: 18px !important;
384
+ min-height: 150px !important;
385
+ border-radius: 18px !important;
386
+ border: 1px solid rgba(148, 163, 184, 0.25) !important;
387
+ background: rgba(15, 23, 42, 0.9) !important;
388
+ color: #e2e8f0 !important;
389
+ }
390
+ .gradio-textbox button, .gr-button {
391
+ border-radius: 999px !important;
392
+ padding: 12px 26px !important;
393
+ background: #2563eb !important;
394
+ color: #ffffff !important;
395
+ border: none !important;
396
+ box-shadow: 0 12px 28px rgba(37, 99, 235, 0.24) !important;
397
+ }
398
+ .gradio-textbox button:hover, .gr-button:hover {
399
+ background: #1d4ed8 !important;
400
  }
401
  """
402
 
403
+ gr.ChatInterface(
404
+ fn=respond_ai,
405
+ title="Digital Twin Chat",
406
+ chatbot=gr.Chatbot(avatar_images=[None, "avatar.jpg"]),
407
+ description="Interact with an AI version of Celeste Lu and ask her about astrophysics questions.",
408
+ examples=["Tell me about what you do", "What is an accretion disk?", "What do you study in computational cosmology?"],
409
+ ).launch(css=css)
avatar.jpg ADDED