-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.py
1257 lines (1017 loc) · 47.5 KB
/
index.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from robyn.robyn import Request
from sklearn.metrics.pairwise import cosine_similarity
from supabase import create_client, Client
import supabase
from openai import OpenAI
import requests
import json
import os
import time
import numpy as np
from hashlib import sha256
from typing import List, Optional
from groq import Groq
import uuid
import fitz
from dotenv import load_dotenv
from mixedbread_ai.client import MixedbreadAI
from io import BytesIO
from PyPDF2 import PdfReader
from robyn import Robyn, ALLOW_CORS, WebSocket, Response, Request
from robyn.types import Body
import logging
from database.db_manager import DatabaseManager
from functools import lru_cache
# Configure logging at the start of the file
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
load_dotenv()
# Initialize database manager
db = DatabaseManager()
class AIClientAdapter:
def __init__(self, client_mode, ollama_url):
self.client_mode = client_mode
self.ollama_url = f"{ollama_url}/api/chat"
self.openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def chat_completions_create(self, model, messages, temperature=0.2, response_format=None):
# expect llama3.2 as the model name
local = {
"llama3.2": "llama3.2",
"gpt-4o": "llama3.2"
}
groq = {
"llama-3.3": "llama-3.3-70b-versatile",
"llama-3.2": "llama3-70b-8192"
}
if self.client_mode == "LOCAL":
# Use Ollama client
data = {
"messages": messages,
"model": local[model],
"stream": False,
}
response = requests.post(self.ollama_url, json=data)
return json.loads(response.text)["message"]["content"]
elif self.client_mode == "ONLINE":
# Use OpenAI or Groq client based on the model
if "gpt" in model:
return self.openai_client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
response_format=response_format
).choices[0].message.content
else:
return self.groq_client.chat.completions.create(
model=groq[model],
messages=messages,
temperature=temperature,
response_format=response_format
).choices[0].message.content
class EmbeddingAdapter:
def __init__(self, client_mode):
self.client_mode = client_mode
if self.client_mode == "LOCAL":
from fastembed import TextEmbedding # Import fastembed only when running project locally
self.fastembed_model = TextEmbedding(model_name="BAAI/bge-base-en")
elif self.client_mode == "ONLINE":
self.mxbai_client = MixedbreadAI(api_key=os.getenv("MXBAI_API_KEY"))
def embeddings(self, text):
if self.client_mode == "LOCAL":
# Use the fastembed model to generate embeddings
result = np.array(list(self.fastembed_model.embed([text])))[-1].tolist()
return result
elif self.client_mode == "ONLINE":
# Use the MixedbreadAI client to generate embeddings
result = self.mxbai_client.embeddings(
model='mixedbread-ai/mxbai-embed-large-v1',
input=[text],
normalized=True,
encoding_format='float',
truncation_strategy='end'
)
return result.data[0].embedding
client_mode = os.getenv("CLIENT_MODE")
ollama_url = os.getenv("OLLAMA_ENDPOINT", "http://localhost:11434")
ai_client = AIClientAdapter(client_mode, ollama_url)
embedding_client = EmbeddingAdapter(client_mode)
app = Robyn(__file__)
websocket = WebSocket(app, "/ws")
ALLOW_CORS(app, origins = ["*"])
url = os.getenv("SUPABASE_URL")
key = os.getenv("SUPABASE_ANON_KEY")
supabase: Client = create_client(url, key)
def parse_array_string(s):
# Remove brackets and split in one operation
return np.fromstring(s[1:-1], sep=',', dtype=float)
@app.exception
def handle_exception(error):
logger.error(f"Application error: {str(error)}", exc_info=True)
return Response(status_code=500, description=f"error msg: {error}", headers={})
def get_cache_key(transcript: str) -> str:
"""Generate a deterministic cache key from the transcript"""
return f"transcript:{sha256(transcript.encode()).hexdigest()}"
@lru_cache
def extract_action_items(transcript):
# Sample prompt to instruct the model on extracting action items per person
messages = [
{
"role": "user",
"content": """You are an executive assistant tasked with extracting action items from a meeting transcript.
For each person involved in the transcript, list their name with their respective action items, or state "No action items"
if there are none for that person.
Write it as an html list in a json body. For example:
{"html":"
<h3>Arsen</h3>
<ul>
<li>action 1 bla bla</li>
<li>action 2 bla</li>
</ul>
<h3>Sanskar</h3>
<ul>
<li>action 1 bla bla</li>
<li>action 2 bla</li>
</ul>"
}
Transcript: """ + transcript
}
]
# Sending the prompt to the AI model using chat completions
response = ai_client.chat_completions_create(
model="gpt-4o",
messages=messages,
temperature=0.2,
response_format={"type": "json_object"}
)
if response is None:
logger.error("Error extracting action items")
return "No action items found."
action_items = json.loads(response)["html"]
return action_items
@lru_cache
def generate_notes(transcript):
messages = [
{
"role": "user",
"content": f"""You are an executive assistant tasked with taking notes from an online meeting transcript. You must produce the notes in Markdown format
Full transcript: {transcript}. Follow the JSON structure:""" + "{notes: meeting notes}" +
"""Here's an example: ### Meeting Notes
**Date:** January 15, 2025
**Participants:**
- You
- Sanskar Jethi
**Summary:**
- Discussion about an option being fully received.
- Confirmation that the system is running properly now.
- Network issues have been resolved and are working perfectly.
**Key Points:**
- Option was fully received and confirmed.
- System is confirmed to be running properly.
- Network is functioning correctly."""
}
]
response = ai_client.chat_completions_create(
model="gpt-4o",
messages=messages,
temperature=0.2,
response_format={"type": "json_object"}
)
notes = json.loads(response)["notes"]
return notes
@lru_cache
def generate_title(summary):
messages = [
{
"role": "system",
"content": f"""You are an executive assistant tasked with generating titles for meetings based on the meeting summaries."""
},
{
"role": "user",
"content": "Generate a title for the following meeting summary. You must follow the JSON schema: {title: generated title}" + f"Full summary: {summary}"
}
]
response = ai_client.chat_completions_create(
model="gpt-4o",
messages=messages,
temperature=0.2,
response_format={"type": "json_object"}
)
title = json.loads(response)["title"]
return title
def send_email_summary(list_emails, actions, meeting_summary = None):
url = "https://api.resend.com/emails"
successful_emails = []
resend_key = os.getenv("RESEND_API_KEY")
resend_email = os.getenv("RESEND_NOREPLY")
if not meeting_summary:
html = f"""
<h1>Action Items</h1>
{actions}
"""
else:
html = f"""
<h1>Meeting Summary</h1>
<p>{meeting_summary}</p>
<h1>Action Items</h1>
{actions}
"""
if list_emails:
current_time = time.localtime()
formatted_time = time.strftime("%d %b %Y %I:%M%p", current_time)
for email in list_emails:
payload = {
"from": resend_email,
"to": email,
"subject": f"Summary | Meeting on {formatted_time} | Amurex",
"html": html
}
headers = {
"Authorization": f"Bearer {resend_key}",
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
if response.status_code != 200:
return {"type": "error", "error": f"Error sending email to {email}: {response.text}", "emails": None}
else:
successful_emails.append(email)
return {"type": "success", "error": None, "emails": successful_emails}
def send_email(email, email_type, **kwargs):
url = "https://api.resend.com/emails"
resend_key = os.getenv("RESEND_API_KEY")
resend_email = os.getenv("RESEND_FOUNDERS_EMAIL")
if not email:
return {"error": "no email provided"}
if email_type == "signup":
html = """
<div>
<div>
<p><b>Hello there 👋</b></p>
</div>
<div>
<p>First off, a big thank you for signing up for Amurex! We're excited to have you join our mission to create the world's first AI meeting copilot.</p>
<p>Amurex is on a mission to become the world's first AI meeting copilot and ultimately your complete executive assistant. We're thrilled to have you join us on this journey.</p>
<p>As a quick heads-up, here's what's coming next:</p>
<ul>
<li>Sneak peeks into new features</li>
<li>Early access opportunities</li>
<li>Ways to share your feedback and shape the future of Amurex</li>
</ul>
<p>Want to learn more about how Amurex can help you? <a href="https://cal.com/founders-the-personal-ai-company/15min" >Just Book a Demo →</a></p>
<p>If you have any questions or just want to say hi, hit reply – we're all ears! We'd love to talk to you. Or better yet, join our conversation on <a href="https://discord.gg/ftUdQsHWbY">Discord</a>.</p>
<p>Thanks for being part of our growing community.</p>
<p>Cheers,<br>Sanskar 🦖</p>
</div>
</div>
"""
subject = "Welcome to Amurex – We're Glad You're Here!"
elif email_type == "meeting_share":
share_url = kwargs['share_url']
owner_email = kwargs['owner_email']
meeting_obj_id = kwargs['meeting_obj_id']
resend_email = os.getenv("RESEND_NOREPLY")
subject = f"{owner_email} shared their notes with you | Amurex"
html = f"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en">
<div
style="display:none;overflow:hidden;line-height:1px;opacity:0;max-height:0;max-width:0"
>
{owner_email} shared their notes with you
<div>
</div>
</div>
<body
style='background-color:rgb(255,255,255);margin-top:auto;margin-bottom:auto;margin-left:auto;margin-right:auto;font-family:ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";padding-left:0.5rem;padding-right:0.5rem'
>
<table
align="center"
width="100%"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="border-width:1px;border-style:solid;border-color:rgb(234,234,234);border-radius:0.25rem;margin-top:40px;margin-bottom:40px;margin-left:auto;margin-right:auto;padding:20px;max-width:465px"
>
<tbody>
<tr style="width:100%">
<td>
<table
align="center"
width="100%"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="margin-top:32px"
>
<tbody>
<tr>
<td>
<div
style="text-align:center;margin-top:0px;margin-bottom:0px;margin-left:auto;margin-right:auto;display:block;outline:none;border:none;text-decoration:none"
>
<a href="https://amurex.ai" style="text-decoration: none; color: inherit;" target="_blank">
<p style="font-size: 50px; display: inline-block; margin-right: 10px;">🦖</p>
<span style="font-size: 50px; display: inline-block;">Amurex</span>
</a>
</div>
</td>
</tr>
</tbody>
</table>
<h1
style="color:rgb(0,0,0);font-size:24px;font-weight:400;text-align:center;padding:0px;margin-top:30px;margin-bottom:30px;margin-left:0px;margin-right:0px"
>
<strong>{owner_email}</strong> shared their meeting notes with you
</h1>
<p
style="color:rgb(0,0,0);font-size:14px;line-height:24px;margin:16px 0"
>
Hey,
</p>
<p
style="color:rgb(0,0,0);font-size:14px;line-height:24px;margin:16px 0"
>
<a
href="mailto:{owner_email}"
style="color:rgb(37,99,235);text-decoration-line:none"
target="_blank"
>{owner_email}</a
> has granted you access to their meeting notes.<!-- -->
</p>
<table
align="center"
width="100%"
border="0"
cellpadding="0"
cellspacing="0"
role="presentation"
style="text-align:center;margin-top:32px;margin-bottom:32px"
>
<tbody>
<tr>
<td>
<a
href="{share_url}"
style="background-color:rgb(0,0,0);border-radius:0.25rem;color:rgb(255,255,255);font-size:12px;font-weight:600;text-decoration-line:none;text-align:center;padding-left:1.25rem;padding-right:1.25rem;padding-top:0.75rem;padding-bottom:0.75rem;line-height:100%;text-decoration:none;display:inline-block;max-width:100%;mso-padding-alt:0px;padding:12px 20px 12px 20px"
target="_blank"
><span
><!--[if mso
]><i
style="mso-font-width:500%;mso-text-raise:18"
hidden
>  </i
><!
[endif]--></span
><span
style="max-width:100%;display:inline-block;line-height:120%;mso-padding-alt:0px;mso-text-raise:9px"
>Open the document</span
><span
><!--[if mso
]><i style="mso-font-width:500%" hidden
>  ​</i
><!
[endif]--></span
></a
>
</td>
</tr>
</tbody>
</table>
<p
style="color:rgb(0,0,0);font-size:14px;line-height:24px;margin:16px 0"
>
or copy and paste this URL into your browser:<!-- -->
<a
href="{share_url}"
style="color:rgb(37,99,235);text-decoration-line:none"
target="_blank"
>{share_url}</a
>
</p>
<hr
style="border-width:1px;border-style:solid;border-color:rgb(234,234,234);margin-top:26px;margin-bottom:26px;margin-left:0px;margin-right:0px;width:100%;border:none;border-top:1px solid #eaeaea"
/>
<p
style="color:rgb(102,102,102);font-size:12px;line-height:24px;margin:16px 0"
>
This invitation was intended for<!-- -->
<span style="color:rgb(0,0,0)">{email}</span>. If you
were not expecting this invitation, you can ignore this email. If
you are concerned about your account's safety, please reply
to this email to get in touch with us.
</p>
</td>
</tr>
</tbody>
</table>
<!--/$-->
</body>
</html>"""
shared_emails = supabase.table("late_meeting")\
.select("shared_with")\
.eq("id", meeting_obj_id)\
.execute().data[0]["shared_with"]
if shared_emails:
if email not in shared_emails:
result = supabase.table("late_meeting")\
.update({"shared_with": shared_emails + [email]})\
.eq("id", meeting_obj_id)\
.execute()
else:
pass
else:
result = supabase.table("late_meeting")\
.update({"shared_with": [email]})\
.eq("id", meeting_obj_id)\
.execute()
payload = {
"from": resend_email,
"to": email,
"subject": subject,
"html": html
}
headers = {
"Authorization": f"Bearer {resend_key}",
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
if response.status_code != 200:
return {"type": "error", "error": f"Error sending email to {email}: {response.text}"}
return {"type": "success", "error": None}
def extract_text(file_path):
with fitz.open(file_path) as pdf_document:
text = ""
for page_num in range(pdf_document.page_count):
page = pdf_document[page_num]
text += page.get_text()
return text
def get_chunks(text):
max_chars = 200
overlap = 50
chunks = []
start = 0
while start < len(text):
chunk = text[start:start + max_chars]
chunks.append(chunk)
start += max_chars - overlap
if start < len(text):
chunks.append(text[start:])
return chunks
def embed_text(text):
embeddings = embedding_client.embeddings(text)
return embeddings
def calc_centroid(embeddings):
return np.mean(embeddings, axis=0)
@app.post("/upload_meeting_file/:meeting_id/:user_id/")
async def upload_meeting_file(request):
meeting_id = request.path_params.get("meeting_id")
user_id = request.path_params.get("user_id")
logger.info(f"Processing file upload for meeting_id: {meeting_id}, user_id: {user_id}")
files = request.files
file_name = list(files.keys())[0] if len(files) > 0 else None
if not file_name:
logger.warning("No file provided in request")
return Response(status_code=400, description="No file provided", headers={})
# Check file size limit (20MB)
file_contents = files[file_name]
file_limit = 20 * 1024 * 1024
if len(file_contents) > file_limit:
logger.warning(f"File size {len(file_contents)} exceeds limit of {file_limit}")
return Response(status_code=413, description="File size exceeds 20MB limit", headers={})
logger.info(f"Processing file: {file_name}")
# Generate unique filename
file_extension = file_name.split(".")[-1]
unique_filename = f"{uuid.uuid4()}.{file_extension}"
# Read file contents
file_contents = files[file_name]
# Upload to Supabase Storage
storage_response = supabase.storage.from_("meeting_context_files").upload(
unique_filename,
file_contents
)
# Get public URL for the uploaded file
file_url = supabase.storage.from_("meeting_context_files").get_public_url(unique_filename)
new_entry = supabase.table("meetings").upsert(
{
"meeting_id": meeting_id,
"user_id": user_id,
"context_files": [file_url]
},
on_conflict="meeting_id, user_id"
).execute()
pdf_stream = BytesIO(file_contents)
reader = PdfReader(pdf_stream)
file_text = ''
for page in reader.pages:
file_text += page.extract_text()
file_chunks = get_chunks(file_text)
embedded_chunks = [str(embed_text(chunk)) for chunk in file_chunks]
result = supabase.table("meetings")\
.update({"embeddings": embedded_chunks, "chunks": file_chunks})\
.eq("meeting_id", meeting_id)\
.eq("user_id", user_id)\
.execute()
return {
"status": "success",
"file_url": file_url,
"updated_meeting": result.data[0]
}
class TranscriptRequest(Body):
transcript: str
meeting_id: str
user_id: str
class ActionRequest(Body):
transcript: str
class EndMeetingRequest(Body):
transcript: str
user_id: str
meeting_id: str
class ActionItemsRequest(Body):
action_items: str
emails: List[str]
meeting_summary: Optional[str] = None
def create_memory_object(transcript):
# Try to get from cache
logger.info("Cache miss - generating new results")
# Generate new results if not in cache
action_items = extract_action_items(transcript)
notes_content = generate_notes(transcript)
title = generate_title(notes_content)
result = {
"action_items": action_items,
"notes_content": notes_content,
"title": title
}
return result
@app.post("/end_meeting")
async def end_meeting(request, body: EndMeetingRequest):
# the logic here could be simplified as well
# TODO: simplify the logic
data = json.loads(body)
transcript = data["transcript"]
user_id = data.get("user_id", None)
meeting_id = data.get("meeting_id", None)
if not user_id:
# this is a temporary fix for the issue
# we need to fix this in the future
# TODO: figure out why tf are we not sending user_id from the chrome extension
return {
"notes_content": generate_notes(transcript),
"action_items": extract_action_items(transcript)
}
if not meeting_id:
action_items = extract_action_items(transcript)
notes_content = generate_notes(transcript)
return {
"notes_content": notes_content,
"action_items": action_items
}
is_memory_enabled = supabase.table("users").select("memory_enabled").eq("id", user_id).execute().data[0]["memory_enabled"]
if is_memory_enabled is True:
meeting_obj = supabase.table("late_meeting").select("id, transcript").eq("meeting_id", meeting_id).execute().data
if not meeting_obj or len(meeting_obj) == 0 or meeting_obj[0]["transcript"] is None:
result = supabase.table("late_meeting").upsert({
"meeting_id": meeting_id,
"user_ids": [user_id],
"meeting_start_time": time.time()
}, on_conflict="meeting_id").execute()
meeting_obj_transcript_exists = None
meeting_obj_id = result.data[0]["id"]
meeting_obj_transcript_exists = meeting_obj[0]["transcript"] # None or str url
meeting_obj_id = meeting_obj[0]["id"]
if not meeting_obj_transcript_exists:
unique_filename = f"{uuid.uuid4()}.txt"
file_contents = transcript
file_bytes = file_contents.encode('utf-8')
storage_response = supabase.storage.from_("transcripts").upload(
path=unique_filename,
file=file_bytes,
)
file_url = supabase.storage.from_("transcripts").get_public_url(unique_filename)
result = supabase.table("late_meeting")\
.update({"transcript": file_url})\
.eq("id", meeting_obj_id)\
.execute()
memory = supabase.table("memories").select("*").eq("meeting_id", meeting_obj_id).execute().data
if memory:
if memory[0]["content"] and "ACTION_ITEMS" in memory[0]["content"]:
summary = memory[0]["content"].split("DIVIDER")[0]
action_items = memory[0]["content"].split("DIVIDER")[1]
result = {
"action_items": action_items,
"notes_content": summary
}
return result
else:
memory_obj = create_memory_object(transcript=transcript)
content = memory_obj["notes_content"] + memory_obj["action_items"]
content_chunks = get_chunks(content)
embeddings = [embed_text(chunk) for chunk in content_chunks]
centroid = str(calc_centroid(np.array(embeddings)).tolist())
embeddings = list(map(str, embeddings))
content = memory_obj["notes_content"] + f"\nDIVIDER\n" + memory_obj["action_items"]
title = memory_obj["title"]
supabase.table("memories").insert({
"user_id": user_id,
"meeting_id": meeting_obj_id,
"content": content,
"chunks": content_chunks,
"embeddings": embeddings,
"centroid": centroid,
}).execute()
supabase.table("late_meeting")\
.update({"summary": memory_obj["notes_content"], "action_items": memory_obj["action_items"], "meeting_title": title})\
.eq("id", meeting_obj_id)\
.execute()
return {
"action_items": memory_obj["action_items"],
"notes_content": memory_obj["notes_content"]
}
else:
action_items = extract_action_items(transcript)
notes_content = generate_notes(transcript)
return {
"notes_content": notes_content,
"action_items": action_items
}
@app.post("/generate_actions")
async def generate_actions(request, body: ActionRequest):
data = json.loads(body)
transcript = data["transcript"]
cache_key = get_cache_key(transcript)
logger.info(f"Generating actions for transcript with cache key: {cache_key}")
# Try to get from cache
cached_result = redis_client.get(cache_key)
if cached_result:
logger.info("Retrieved result from cache")
return json.loads(cached_result)
logger.info("Cache miss - generating new results")
# Generate new results if not in cache
action_items = extract_action_items(transcript)
notes_content = generate_notes(transcript)
result = {
"action_items": action_items,
"notes_content": notes_content
}
# Cache the result
redis_client.setex(
cache_key,
CACHE_EXPIRATION,
json.dumps(result)
)
return result
@app.post("/submit")
async def submit(request: Request, body: ActionItemsRequest):
data = json.loads(body)
action_items = data["action_items"]
meeting_summary = data["meeting_summary"]
# notion_url = create_note(notes_content)
emails = data["emails"]
successful_emails = send_email_summary(emails, action_items, meeting_summary)
if successful_emails["type"] == "error":
return {
"successful_emails": None,
"error": successful_emails["error"]
}
return {"successful_emails": successful_emails["emails"]}
class TrackingRequest(Body):
uuid: str
event_type: str
meeting_id: Optional[str] = None
@app.post("/track")
async def track(request: Request, body: TrackingRequest):
try:
data = json.loads(body)
uuid = data["uuid"]
event_type = data["event_type"]
meeting_id = data.get("meeting_id")
result = supabase.table("analytics").insert({
"uuid": uuid,
"event_type": event_type,
"meeting_id": meeting_id
}).execute()
return {"result": "success"}
except Exception as e:
return Response(
status_code=500,
description=f"Error tracking event: {str(e)}",
headers={}
)
@app.get("/")
def home():
return "Welcome to the Amurex backend!"
def find_closest_chunk(query_embedding, chunks_embeddings, chunks):
query_embedding = np.array(query_embedding)
chunks_embeddings = np.array(chunks_embeddings)
similarities = cosine_similarity([query_embedding], chunks_embeddings)
closest_indices = np.argsort(similarities, axis=1)[0, -5:][::-1] # Five the closest indices of embeddings
closest_chunks = [chunks[i] for i in closest_indices]
return closest_chunks
def generate_realtime_suggestion(context, transcript):
messages = [
{
"role": "system",
"content": """
You are a personal online meeting assistant, and your task is to give instant help for a user during a call.
Possible cases when user needs help or a suggestion:
- They are struggling to answer a question
- They were asked a question that requires recalling something
- They need to recall something from their memory (e.g. 'what was the company you told us about 3 weeks ago?')
You have to generate the most important suggestion or help for a user based on the information retrieved from user's memory and latest transcript chunk.
"""
},
{
"role": "user",
"content": f"""
Information retrieved from user's memory: {context},
Latest chunk of the transcript: {transcript},
Be super short. Just give some short facts or key words that could help your user to answer the question.
Do not use any intro words, like 'Here's the suggestion' etc.
"""
}
]
response = ai_client.chat_completions_create(
model="llama-3.2",
messages=messages,
temperature=0
)
return response
def check_suggestion(request_dict):
try:
transcript = request_dict["transcript"]
meeting_id = request_dict["meeting_id"]
user_id = request_dict["user_id"]
is_file_uploaded = request_dict.get("isFileUploaded", None)
if is_file_uploaded:
sb_response = supabase.table("meetings").select("context_files, embeddings, chunks, suggestion_count").eq("meeting_id", meeting_id).eq("user_id", user_id).execute().data
if not sb_response:
return {
"files_found": False,
"generated_suggestion": None,
"last_question": None,
"type": "no_record_found"
}
sb_response = sb_response[0]
if not sb_response["context_files"] or not sb_response["chunks"]:
return {
"files_found": False,
"generated_suggestion": None,
"last_question": None,
"type": "no_file_found"
}
logger.info("This is the suggestion count: %s ", sb_response["suggestion_count"])
if int(sb_response["suggestion_count"]) == 10:
return {
"files_found": True,
"generated_suggestion": None,
"last_question": None,
"type": "exceeded_response"
}
file_chunks = sb_response["chunks"]
embedded_chunks = sb_response["embeddings"]
embedded_chunks = [parse_array_string(item) for item in embedded_chunks]
messages_list = [
{
"role": "system",
"content": """You are a personal online meeting copilot, and your task is to detect if a speaker needs help during a call.
Possible cases when user needs help in real time:
- They need to recall something from their memory (e.g. 'what was the company you told us about 3 weeks ago?')
- They need to recall something from files or context they have prepared for the meeting (we are able handle the RAG across their documents)
If the user was not asked a question or is not trying to recall something, then they don't need any help or suggestions.
You have to identify if they need help based on the call transcript,
If your user has already answered the question, there is no need to help.
If the last sentence in the transcript was a question, then your user probably needs help. If it's not a question, then don't.
You are strictly required to follow this JSON structure:
{"needs_help":true/false, "last_question": json null or the last question}
"""
},
{
"role": "user",
"content": f"""
Latest chunk from the transcript: {transcript}.
"""
}
]
response = ai_client.chat_completions_create(
model="llama-3.2",
messages=messages_list,
temperature=0,
response_format={"type": "json_object"}
)
response_content = json.loads(response)
last_question = response_content["last_question"]
if 'needs_help' in response_content and response_content["needs_help"]:
embedded_query = embed_text(last_question)
closest_chunks = find_closest_chunk(query_embedding=embedded_query, chunks_embeddings=embedded_chunks, chunks=file_chunks)
suggestion = generate_realtime_suggestion(context=closest_chunks, transcript=transcript)
result = supabase.table("meetings")\
.update({"suggestion_count": int(sb_response["suggestion_count"]) + 1})\
.eq("meeting_id", meeting_id)\
.eq("user_id", user_id)\
.execute()
return {
"files_found": True,
"generated_suggestion": suggestion,