Spaces:
Sleeping
Sleeping
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") | |
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 | |
]) | |
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 | |
]) | |
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 | |
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 | |