o
    Ii;                     @   s  U d dl mZmZmZ d dlmZmZ d dlmZ d dl	Z	d dl
Z
d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZmZ d d	lmZ d
Ze Zee ddddZ!ee!dZ"dd Z#e Z$ee%j&Z'e'd Z(ee)e(dZ*dddddddddddddd d!d"d#dd$d%d&dddd'd(d)dd*dd+d,d-d.ddd/d0d1dd2dd3d4d5dddgZ+ee e,d6< G d7d8 d8eZ-d9e)d:ee) fd;d<Z.d=e)d:e)fd>d?Z/d9e)d=e)d@e)d:e)fdAdBZ0e$1dCdDdE Z2e$j1dFedGdHefdIdJZ3e$j1dKedGdHefdLdMZ4e$5dKedNedNedNfdHed9e)d=e)d@e)fdOdPZ6e$5dQedNedNfdRe7dSe)fdTdUZ8ej9j:e!d dS )V    )FastAPIRequestForm)HTMLResponseRedirectResponse)Jinja2TemplatesN)ListDict)Path)ColumnIntegerStringTextBooleanFloatDateTimecreate_engine)declarative_basesessionmaker)funczjpostgresql+asyncpg://postgres:p4python@postgres.cqf3qroocfwm.us-east-1.rds.amazonaws.com:5432/email_filterz+asyncpg T)echo)bindc                  C   s6   t  } z| ttj  W |   S |   w )N)SessionLocalquery	EmailSpamorder_byreceived_atdescallclose)db r"   2/home/esfera/Desktop/Rishu/Manjeet/manjeet/main.pyget_all_emails_from_db   s   r$   	templates)	directoryu7   🎉 WIN $10,000 NOW!!! Click Here to Claim Your Prize!zwinner@prize-giveaway.comzCongratulations! You have won $10,000 in our special lottery. Click the link below immediately to claim your prize. Limited time offer!Spamu>  Subject Analysis: Subject contains suspicious pattern: '\b(win|winner|won|prize|free|urgent|limited time|act now|click here)\b'; Subject contains suspicious pattern: '\$[\d,]+' | Sender Analysis: ℹ️ New sender. No previous history found. | Content Analysis: Contains suspicious keywords: win, prize, click, limited)subjectsendersummarylabelanalysisz3Meeting Reminder: Project Review - Tomorrow at 2 PMzjohn.smith@company.comzHi team, just a reminder about our project review meeting scheduled for tomorrow at 2 PM in Conference Room B. Please prepare your status updates.Not Spamu>   Sender Analysis: ℹ️ New sender. No previous history found.z9Your Account Verification Required - Urgent Action Neededznoreply@secure-bank-verify.netzWe need to verify your account information. Please click the link below to verify your account immediately. Failure to do so may result in account suspension.	Uncertainu
  Subject Analysis: Subject contains suspicious pattern: '\b(click|download|claim|verify)\b.*\b(now|immediately|today)\b' | Sender Analysis: ⚠️ Unfamiliar sender domain: secure-bank-verify.net | Content Analysis: Contains suspicious keywords: verify, click, urgentz$Invoice #INV-2024-0012 - Payment Duezbilling@techservices.comzDear customer, your invoice #INV-2024-0012 for $450.00 is due on March 15, 2024. Please make payment through the link in your account portal.z)FREE Viagra Pills - Guaranteed Results!!!zpharmacy@cheap-meds-online.bizzzGet free viagra pills delivered to your door. No prescription needed. Risk-free guarantee. Click now for special discount!u  Subject Analysis: Subject contains suspicious pattern: '\b(viagra|cialis|pharmacy|pills)\b'; Subject contains suspicious pattern: '!!!+' | Sender Analysis: ⚠️ Unfamiliar sender domain: cheap-meds-online.biz | Content Analysis: Contains suspicious keywords: free, guaranteed, clickz,Newsletter: Weekly Tech Updates - March 2024znewsletter@techblog.comznThis week's top stories: AI developments, new software releases, and industry insights. Read more on our blog.Not Classifiedz0Act Now! Limited Time Offer - 50% Off Everythingzdeals@shopping-mall-super.comzzDon't miss out! Our biggest sale ever - 50% off all items. Limited time only. Click here to shop now before it's too late!u  Subject Analysis: Subject contains suspicious pattern: '\b(win|winner|won|prize|free|urgent|limited time|act now|click here)\b' | Sender Analysis: ⚠️ Unfamiliar sender domain: shopping-mall-super.com | Content Analysis: Contains suspicious keywords: limited, clickz'Password Reset Request for Your Accountzsecurity@github.comzWe received a request to reset your password. If you made this request, click the link below. If not, please ignore this email.spam_emailsc                   @   s   e Zd ZdZeedddZeeddZeeddZ	ee
ddZeeddZeeddZeeddZeeddZee
ddZeeddZeedde dZd	S )
r   email_filterT)primary_keyindexF)nullable)default)timezone)server_defaultN)__name__
__module____qualname____tablename__r   r   idr   sender_emailr(   r   bodyr   spam_probabilitypredicted_labelfeedback_labelr   is_reviewedemail_summarymodel_accuracyr   r   nowr   r"   r"   r"   r#   r   f   s    r   r(   returnc                 C   sD   g }|   }g d}|D ]}t||tjr|d| d q|S )z*Check email subject for spam-like patterns)zF\b(win|winner|won|prize|free|urgent|limited time|act now|click here)\bz\$[\d,]+z!!!+z"\b(viagra|cialis|pharmacy|pills)\bz(\b(guaranteed|risk-free|no obligation)\bz>\b(click|download|claim|verify)\b.*\b(now|immediately|today)\bz&Subject contains suspicious pattern: '')lowerresearch
IGNORECASEappend)r(   spam_patternssubject_lowerspam_keywordspatternr"   r"   r#   analyze_email_subjectz   s   	rQ   r)   c                    s   |    d v r dd nd} fddtD }|rRtdd |D }tdd |D }t|}||kr?d	| d
| dS ||krLd| d
| dS d	| dS g d}|rj||vrjt|ddkrjd| S dS )z'Compare sender history with past emails@r   c                    s$   g | ]}| d d  kr|qS )r)   r   )getrH   .0esender_lowerr"   r#   
<listcomp>   s   $ z*analyze_sender_history.<locals>.<listcomp>c                 s   "    | ]}| d dkrdV  qdS )r+   r'      NrT   rU   r"   r"   r#   	<genexpr>        z)analyze_sender_history.<locals>.<genexpr>c                 s   r[   )r+   r-   r\   Nr]   rU   r"   r"   r#   r^      r_   u   ⚠️ Sender has /z- emails previously marked as Spam. High risk.u   ✓ Sender has z0 emails previously marked as Not Spam. Low risk.z. previous email(s) with mixed classifications.)z	gmail.comz	yahoo.comzhotmail.com.   u!   ⚠️ Unfamiliar sender domain: u-   ℹ️ New sender. No previous history found.)rH   splitr0   sumlen)r)   sender_domainprevious_emails
spam_countnot_spam_counttotalsuspicious_domainsr"   rX   r#   analyze_sender_history   s"   
rl   r*   c                    s   g }t | }|r|dd|dd   t|}|d|  |  g d} fdd|D }|rC|d	d
|dd   |sGdS d|S )z#Generate automated analysis summaryzSubject Analysis: z; Nrb   zSender Analysis: )freewinprizeurgentlimited
guaranteedclickverifyc                    s   g | ]}| v r|qS r"   r"   )rV   wordsummary_lowerr"   r#   rZ      s    z/generate_automated_analysis.<locals>.<listcomp>z0Content Analysis: Contains suspicious keywords: z,    u<   ✓ No obvious spam indicators detected. Review recommended.z | )rQ   rL   joinrl   rH   )r(   r)   r*   analysis_partssubject_patternssender_analysissuspicious_wordsfound_wordsr"   rv   r#   generate_automated_analysis   s   
r   r`   c                      s   t ddS )N/dashboard/spam)url)r   r"   r"   r"   r#   root   s   
r   r   )response_classrequestc                    s6  t  }t|}dd fdd|D }tdd |D }tdd |D }tdd |D }td	d |D }|| }g }	|D ]É  j}
|
d
krXdt jpRdd  }n+|
dkrhdt jpbdd  }n|
dkrxdt jprdd  }ndt jp~dd  }tdd  jpdD }d jpdv }d jpdv pd jpdv }g d}t fdd|D } j j j j|
pdd|t	|t
t jpdd d d|||t jd dkrdnddd d |
d u|
d ud!d"d |
d
k|
dk|
dkp|
d u d#d$
}|	| qA||||||||d!d%	}td&| |	|d'S )(Nc                 S   s<   | sd S |    } | dv rdS | dv rdS | dkrdS d S )N)hamznot spamr-   )	undecided	uncertainr.   spamr'   )striprH   )r+   r"   r"   r#   normalize_label   s   z'spam_dashboard.<locals>.normalize_labelc                    s   g | ]} |j qS r"   )r@   rU   )r   r"   r#   rZ      s    z"spam_dashboard.<locals>.<listcomp>c                 s       | ]	}|d krdV  qdS )r   r\   Nr"   rV   lr"   r"   r#   r^          z!spam_dashboard.<locals>.<genexpr>c                 s   r   )r   r\   Nr"   r   r"   r"   r#   r^      r   c                 s   r   )r   r\   Nr"   r   r"   r"   r#   r^      r   c                 s   s    | ]	}|d u rdV  qd S )Nr\   r"   r   r"   r"   r#   r^      r   r   K   r      r   
      r   -   2   c                 s   s    | ]	}|  rd V  qdS )r\   N)isupper)rV   cr"   r"   r#   r^      r   !$)rm   rn   ro   rp   rs   rt   c                 3   s&    | ]}| j p	d  v rdV  qdS )r   r\   N)r>   rH   )rV   kw)emailr"   r#   r^      s   $ r\   d   rb   r   NewKnown)uppercase_ratiohas_exclamationhas_dollar_signsuspicious_keywordssender_reputationTF)fetchedpreprocessed
classifiedfeedback_storedmodel_retrained)inboxr   r   r   )
email_idr)   r(   r*   r+   r,   ml_scorefeaturesworkflow_statusfolder_location)	rj   r   r   r   not_classifiedr   inbox_countfeedback_countr   zspam_dashboard.html)r   emailsstats)r$   re   rd   r@   hashr(   r>   r<   r=   roundmaxrL   r%   TemplateResponse)r   	db_emailstotal_emailsnormalized_labelsrh   	ham_countundecided_countnot_classified_countclassified_countenhanced_emailsdisplay_labelr   subject_upperr   
has_dollarr   keyword_countenhanced_emailr   r"   )r   r   r#   spam_dashboard   s   
 r   z/dashboard/spam/addc                    s   t dd| iS )Nzspam_add.htmlr   )r%   r   )r   r"   r"   r#   spam_add_page8  s
   r   .c              	      s`   t |||}t }zt|||d dd dd}|| |  W |  n|  w tdddS )Nr/   F)r=   r(   r>   r?   r@   rA   rB   r   i.  )r   status_code)r   r   r   addcommitr    r   )r   r(   r)   r*   r,   r!   	new_emailr"   r"   r#   spam_add_submitC  s"   
	
r   z/dashboard/spam/classifyr   classificationc                    sz   |  }|dvrtdddS t }z|ttj| k }|r,||_d|_	|
  W |  n|  w tdddS )N)r   r   r   r   i/  )r   T)rH   r   r   r   r   filterr<   firstr@   rB   r   r    )r   r   r!   	email_objr"   r"   r#   classify_emaild  s   r   );fastapir   r   r   fastapi.responsesr   r   fastapi.templatingr   rI   ostypingr   r	   pathlibr
   
sqlalchemyr   r   r   r   r   r   r   r   sqlalchemy.ormr   r   sqlalchemy.sqlr   DATABASE_URLBasereplaceenginer   r$   app__file__parentBASE_DIRTEMPLATES_DIRstrr%   r0   __annotations__r   rQ   rl   r   rT   r   r   r   postr   intr   metadata
create_allr"   r"   r"   r#   <module>   s    (


;
h
 