Spaces:
Sleeping
Sleeping
File size: 2,409 Bytes
8419546 |
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 |
from fastapi import APIRouter, Depends, HTTPException, Body
from sqlalchemy.orm import Session
from server.utils.database import get_db
from fastapi.encoders import jsonable_encoder
from server.controllers.mail import MailContactFilter, MailSummary,MailDate
from server.utils.summarize import summarize,summarize_week
import json
from server.crud.mail import (
get_all_mails,
get_top_n_mails,
get_contact_id_by_email,
get_mails_by_contact_id,
get_mails_by_date_range,
)
router = APIRouter(prefix="/mails")
@router.get("/all")
def read_all_mails(db: Session = Depends(get_db)):
mails = get_all_mails(db)
return jsonable_encoder([
{
"id": str(mail.id),
"bodyPlainRedacted": mail.bodyPlainRedacted
}
for mail in mails
])
@router.get("/topk")
def read_all_mails(db: Session = Depends(get_db),topk: int=10):
mails = get_top_n_mails(db,topk=topk)
return jsonable_encoder([
{
"id": str(mail.id),
"bodyPlainRedacted": mail.bodyPlainRedacted
}
for mail in mails
])
@router.post("/by-contact")
def get_mails_for_contact(payload: MailContactFilter, db: Session = Depends(get_db))-> MailSummary :
contact_id = payload.contact_id
user_email = payload.user_email
if not contact_id and not user_email:
raise HTTPException(status_code=400, detail="Either contact_id or user_email must be provided")
if user_email:
contact_id = get_contact_id_by_email(db, user_email)
if contact_id is None:
raise HTTPException(status_code=404, detail="User email not found")
mails = get_mails_by_contact_id(db, contact_id)
full_text=""
for i in mails:
full_text+=i.bodyPlainRedacted
obj=MailSummary()
obj.full_mail=full_text
obj.result=summarize(full_text)
return obj
@router.post("/weekly")
def get_summary_weekly(payload:MailDate, db: Session=Depends(get_db))->MailSummary:
star_date=payload.start_date
end_date=payload.end_date
mails=get_mails_by_date_range(db,start_date=star_date, end_date=end_date)
full_text=""
for i in mails:
full_text+=i.bodyPlainRedacted
obj=MailSummary()
res=summarize_week(full_text)
if res:
obj.result={"summary":res}
return obj
else:
obj.result={"summary":"No mail found"}
return obj
|