
    khFy                        S SK r S SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKrS SKJr  S SKrS SK7  S SKrS SKJr  S SKJr  S S	KJr  S SKJ	r	  S S
KJr  S SKJr  S SKrS SKJrJr  S SKJr  S SKJ r   S SK!J"r"  SSK#J$r$  SSK%JrJ&r&J'r'  S SK(J)r)  \	" S/5      S 5       r*\	" S/5      S 5       r+S r,S r-\	" S/5      S 5       r.\	" S/5      S 5       r/\	" S/5      S 5       r0\	" S/5      S 5       r1\	" S/5      S 5       r2\" S/5      \" \/5      S 5       5       r3\" S/5      \" \/5      S  5       5       r4\" S/5      \" \/5      S! 5       5       r5\" S/5      \" \/5      S" 5       5       r6\" S/5      \" \/5      S# 5       5       r7\" S/5      \" \/5      S$ 5       5       r8\" S/5      \" \/5      S% 5       5       r9S& r:g)'    N)settings)JsonResponse)require_http_methods)DevelopmentApplication)EXTRACT_STRUCTURED_DATA_PROMPT)*render)csrf_exempt)method_decorator)View)api_viewpermission_classes)AllowAny)Responsestatus   )RAGChatbotServicer   PDFDocumentExtractedPDFData)QGETc                     [         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[
        R                  " U5      n[
        R                  " U5      nUR                  SS2UR                  R                  R                  SSS9) 4   nUR                  SS2UR                  R                  R                  SSS9) 4   nUR                  R                  UR                  5      nXW   nXg   n[
        R                  " XV/S	S
9nXR                  SS9   n	UR                  S	S
9n
U
R                  USS9  U	R                  USS9  [!        SU R#                  [        R$                  S-   5      U R#                  [        R$                  S-   5      ['        [)        U	5      5      ['        [)        U
5      5      S.5      $ ! [*         a"  n[!        SS[        U5       30SS9s SnA$ SnAff = f)z~
Merges two CSV files using only exactly matching column names.
Removes duplicates and prevents creation of unwanted columns.
z,Application Tables - 2025-07-20 08_21_55.csvzhistorical_scrape.csvmerged_output.csvzduplicate_entries.csvNz^UnnamedF)caseT)ignore_index)keepindexz7Merge completed successfully using exact column matches)messageoutput_fileduplicates_filenum_duplicatesnum_final_rowserrorMerge failed:   r   )ospathjoinr   
MEDIA_ROOTpdread_csvloccolumnsstrcontainsintersectionconcat
duplicateddrop_duplicatesto_csvr   build_absolute_uri	MEDIA_URLintlen	Exception)requestfile1file2merged_filedup_filedf1df2common_columnscombined_dfduplicate_rows	unique_dfes               2/home/developer/Desktop/shao/shao/shaoApp/views.pymergeCsvFilesViewrK      s   'NX002`aX002IJggll8#6#68KL77<< 3 35LM kk% kk%  gga#++//22:E2JJJKgga#++//22:E2JJJK 11#++>!! ii
>$%;%;%;%GH//T/B	 	E2he4P"55h6H6HK^6^_&99(:L:LOf:fg!#n"56!#i.1
  	  NWs1vh&?@MMNs   II
 

I6I1+I61I6c                     [         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[
        R                  " U5      n[
        R                  " U5      n/ SQnXV   n[
        R                  " XESSS9nUR                  USS	9  US
   R                  5       R                  5       n[        SU R                  [        R                  S-   5      [        U5      S.5      $ ! [         a"  n	[        SS[!        U	5       30SS9s Sn	A	$ Sn	A	ff = f)z=
Enriches merged CSV data using determined application data.
r   z%cumulative_applicationsdetermined.csvenriched_merged_output.csv)application_iddecisionlodgement_datedetermined_datecostcouncil_namerN   leftonhowFr    rO   z!Enrichment completed successfully)r"   enriched_filenum_records_enrichedr'   zEnrichment failed: r)   r   N)r*   r+   r,   r   r-   r.   r/   merger8   notnasumr   r9   r:   r;   r=   r2   )
r>   merged_pathdetermined_pathenriched_output_path	merged_dfdetermined_dfcolsenriched_dfenriched_countrI   s
             rJ   enrichMergedFileViewre   N   s3   
Sggll8#6#68KL'',,x':':<cd!ww||H,?,?A]^KK,	O4 k%+hhy<LRXY/u=$Z0668<<>:$778J8JMi8ij$'$7
  	  SW(;CF8&DEcRRSs   D<D? ?
E+	E& E+&E+c                      [         R                  " U SS9n[         R                  " U5      (       d  [        U[         R                  5      (       a  g UR                  5       $ ! [         a     g f = f)Ncoerce)errors)r.   to_datetimeisnull
isinstanceNaTdater=   )date_strrm   s     rJ   
parse_datero   n   sU    ~~hx899T??jrvv66yy{ s   AA" A" "
A/.A/c                 L    [         R                  " U 5      (       d  U S:X  a  S $ U $ )N )r.   isna)vals    rJ   safe_getrt   w   s    773<<3"945#5    c                     [         R                  R                  [        R                  S5      n[         R                  R                  U5      (       d  [        SS0SS9$ [        R                  " U5      nSu  p4nSn[        U5      n[        SXv5       GHW  nUR                  XU-    n	[        S	X-  S
-    SXv-   S
-
  U-   35        U	R                  5        GH  u  p [        UR                  SS5      5      nU(       a  [!        U5      nO0 n[        SU5        ["        R$                  R'                  [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [)        UR                  S5      5      [)        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      [        UR                  S5      5      S.S9u  pU(       a  US
-  nGM  US
-  nGM     X-   U:  d  GMA  [,        R.                  " S!5        GMZ     [        S"UUUX4-   U-   S#.5      $ ! [*         a  n[        SU 35        US
-  n S nAGMS  S nAff = f! [*         a"  n[        SS$[1        U5       30S%S9s S nA$ S nAff = f)&zK
Imports enriched CSV data into the database with improved error handling.
rM   r'   z%enriched_merged_output.csv not found.  r   )r   r   r      r   zProcessing batch r   /descriptionrq   	extractedrN   application_web_urlrO   rP   rQ   rR   council_name_xdevelopment_typeland_usenumber_of_dwellingsnumber_of_storeysnumber_of_placesnumber_of_unitsnumber_of_lots)application_urlrO   rP   rQ   rR   rS   r~   r   categoryr   r   r   number_of_apartmentsnumber_of_subdivisions)rN   defaultszError processing row: N   z2Data import completed with improved error handling)r"   createdupdatedfailedtotal_processedzData import failed: r)   )r*   r+   r,   r   r-   existsr   r.   r/   r<   rangeilocprintiterrowsrt   getextract_structured_datar   objectsupdate_or_createro   r=   timesleepr2   )r>   	file_pathdfr   r   r   
batch_size
total_rowsibatch_df_rowrz   
structuredobj
is_createdrI   s                    rJ   importEnrichedDataViewr   z   s   
FTGGLL!4!46RS	ww~~i((*Q R[^__[[##* & 
W
q*1AwwqZ0H%ama&7%8:;RUV;VYc:c9def"++-&"*377="+E"FK #%<[%I
%'
+j1&<&D&D&U&U'/8H0I'J/7@U8V/W(01D(E.8AQ9R.S/9#''BS:T/U$,SWWV_$=,4SWW=M5N,O08HZ9[0\'/
z0J'K'/
?Q0R'S3;JNNK`<a3b19*..I\:]1^08HZ9[0\4<Z^^L]=^4_6>z~~N^?_6`" 'V 'OC( "11E .T ~
*

1_ 2b K&069
  	 ! 21#67aKF"  TW(<SVH&EFsSSTsb   AL2  A=L2 GL=L2  LL2 0L2 
L/L*#L2 *L//L2 2
M<MMMc           	          [         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[         R                  R                  [        R                  S5      n[         R                  R                  U5      (       a$  [         R                  R                  U5      (       d  [        SS0SS9$ [        R                  " U5      n[        R                  " U5      nUR                  SS	0S
S9  UR                  S	/S9nXDS	   R                  [        5      R                  R                  5       S:g     n[        R                  " UU/ SQ   S	SS9nUS   R                  [        5      R                  S 5      US'   UR!                  USS9  [        SU R#                  [        R$                  S-   5      ['        [)        U5      5      ['        US   R+                  5       R-                  5       5      S.5      $ ! [.         a"  n[        SS[        U5       30SS9s SnA$ SnAff = f)z
Merges enriched data with Name field from the scraper document based on Application ID.
Filters out rows with missing Application ID before merging.
Replaces Windows-style file path with /media/PDFs/ format.
rM   z+Scraper Documents - 2025-07-20 08_21_54.csvenriched_with_name.csvr'   z%One or both source files are missing.rw   r   zApplication IDrN   T)r1   inplace)subsetrq   )rN   NamePathrT   rU   r   c           	      X   [        U [        5      (       a  U R                  5       (       az  SSR                  U R	                  SS5      R                  S5       Vs/ sH<  nU(       d  M  SU;   a  M  UR                  5       (       a  [        U5      S::  a  M:  UPM>     sn5      -   $ S$ s  snf )Nz/media/ry   \:   rq   )rk   r2   stripr,   replacesplitisupperr<   )pparts     rJ   <lambda>.mergeWithNameFromScraperView.<locals>.<lambda>   s     Q$$	  #((!"4!5!;!;C!@,!@ !$ 15CIQRN !@, #  ; 9;	; ,s   
B'
(B'
0"B'
B'
Fr    z&Merge with Name completed successfullyr   )r"   r#   num_recordsnum_with_namer(   r)   N)r*   r+   r,   r   r-   r   r   r.   r/   renamedropnaastyper2   r   rZ   applyr8   r9   r:   r;   r<   r[   r\   r=   )r>   enriched_pathscraper_docs_pathoutput_pathrc   
scraper_dfr`   rI   s           rJ   mergeWithNameFromScraperViewr      s   1NX%8%8:VWGGLL)<)<>klggll8#6#68PQ ww~~m,,BGGNNCT4U4U*Q R[^__ kk-0[[!23
 	#35E"FPTU "((1A0B(C!.>"?"F"Fs"K"O"O"U"U"W[]"]^ HH9:	
	 &f-44S9??;
	& 	E2?"55h6H6HKc6cds9~. 6!2!8!8!:!>!>!@A	
  	  NWs1vh&?@MMNs%   CH$  EH$ $
I.IIIc                     [         R                  R                  [        R                  S5      n[         R                  R                  U5      (       d  [        SS0SS9$ [        R                  " U5      nSUR                  ;  d  SUR                  ;  a  [        SS0S	S9$ / nUR                  5        H  u  pEUS   n[         R                  R                  [        R                  UR                  S
5      5      n[        U5        [         R                  R                  U5      (       d  Mx  UR                  US   5        M     [        S[        U5      US.5      $ ! [          a"  n[        SS[#        U5       30SS9s SnA$ SnAff = f)z
Checks the existence of PDF files specified in the 'Path' column
of enriched_with_name.csv and returns a list of application IDs
for which the file exists.
r   r'   zMerged file not found.rw   r   rN   r   z(Required columns are missing in the CSV.  ry   zFile existence check complete.)r"   num_files_foundapplication_idszFile check failed: r)   N)r*   r+   r,   r   r-   r   r   r.   r/   r1   r   BASE_DIRlstripr   appendr<   r=   r2   )	r>   
input_pathr   existing_idsr   r   relative_path	full_pathrI   s	            rJ   checkExistingPdfFilesViewr     sP   SWW\\("5"57OP
 ww~~j))*B CCPP [[$2::-rzz1I*T U^abb kkmFAKMX%6%68L8LS8QRI)ww~~i((##C(8$9: $ 7"<0+
  	  SW(;CF8&DEcRRSs2   AE  AE "BE ,/E 
F&F=FFc                     [         R                  R                  [        R                  S5      n[         R                  R                  U5      (       d  [        SS0SS9$ [        R                  " U5      nSUR                  ;  d  SUR                  ;  a  [        SS0S	S9$ / nS
nS
nS
n/ nUR                  5        H  u  pU	S   n
U	S   n[        R                  " U5      (       d  US:X  a  M2  [         R                  R                  [        R                  UR                  S5      5      n[         R                  R                  U5      (       a  UR                  X45        M  UR                  U
SUS.5        US-  nM     Sn[        U5      n[!        S
X5       GHs  nXX-    n[#        SX-  S-    SX-   S-
  U-   35        U GH#  u  p [%        X5      nUR'                  S5      (       a  UR                  U
UR'                  S5      UUR'                  S5      UR'                  S5      UR'                  SS
5      UR'                  SS
5      UR'                  S5      S.5        US-  nUR'                  S0 5      R'                  S5      (       a  US-  nM  M  UR                  U
UR'                  S5      =(       d    UR'                  S5      UUR'                  S5      S.5        US-  nGM&     X-   U:  d  GM]  [,        R.                  " S5        GMv     [         R                  R                  [        R                  S5      n[1        USS S!9 n[2        R4                  " UUS"S#S$9  SSS5        [        S%UUUUU[        U5      S&.5      $ ! [(         a/  nUR                  U
[+        U5      US.5        US-  n SnAGM  SnAff = f! , (       d  f       Nf= f! [(         a"  n[        SS'[+        U5       30S(S9s SnA$ SnAff = f))ze
Extract data from PDF files and save to database with improved error handling and batch processing.
r   r'   z!enriched_with_name.csv not found.rw   r   rN   r   zCSV missing required columnsr   r   rq   ry   zPDF file not found)rN   r'   r+   r      zProcessing PDF batch extraction_successextracted_datapdf_type
confidencetext_lengthpages_processeddatabase_save)rN   datar+   r   r   r   r   r   successreason)rN   r'   r+   r   Nzextracted_applications.jsonwzutf-8)encodingr   F)indentensure_asciiz*PDF extraction and database save completed)r"   r#   
total_pdfssuccessfully_processedsuccessfully_saved_to_dbr   total_recordszPDF extraction failed: r)   )r*   r+   r,   r   r-   r   r   r.   r/   r1   r   rr   r   r   r   r<   r   r   process_and_save_pdf_datar   r=   r2   r   r   openjsondump)r>   	input_csvr   result	processedr   saved_to_db
valid_rowsr   r   app_idrel_pathpdf_pathr   r   r   batchprocessing_resultrI   r   fs                        rJ   extractPdfDatar   ,  s   
eWGGLL!4!46NO	ww~~i((*M NWZ[[[[#2::-rzz1I*H IRUVV	 
kkmFA)*F6{Hwwx  HNww||H$5$5xs7KLHww~~h''!!6"45&,1$ 
 !! $& 
_
q*1A0E)!-!*;)<Az?VYZ?Z]g>g=hij$) # (A&(S%(,,-ABB.4$5$9$9:J$K$,(9(=(=j(I*;*?*?*M+<+@+@PQ+R/@/D/DEVXY/Z->-B-B?-S	' 	 "Q	 -00"EII)TT'1,K U .4%6%:%:8%D%fHYH]H]^eHf$,->-B-B?-S	'  !9 %*N ~
*

1Y 2^ ggll8#6#68UV+sW5IIfa> 6 C&$&/(3 [
  	# !  MM*0!$Q (# 
 aKF  65  WW(?Ax&HIRUVVWs   AO  AO "D(O CNO AN'O 7AO O##O 
O #N;4O ;O  O 
OO 
P O;5P ;P c                     SSK JnJnJn  U R                  R                  S5      nU(       Ga   UR                  R                  US9nUR                  R                  5       nUUR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                   UR"                  UR$                  UR&                  S./ S.nU GH  nUR(                  UR*                  UR,                  UR.                  UR0                  UR2                  UR4                  UR6                  UR8                  UR:                  UR<                  R?                  5       S.n	[A        US5      (       Ga(  URB                  n
0 S	U
RD                  _S
U
RF                  _SU
RH                  _SU
RJ                  _SU
RL                  _SU
RN                  _SU
RP                  _SU
RR                  _SU
RT                  _SU
RV                  _SU
RX                  _SU
RZ                  _SU
R\                  _SU
R^                  _SU
R`                  _SU
Rb                  _SU
Rd                  _U
Rf                  U
Rh                  U
Rj                  U
Rl                  U
Rn                  S.EU	S'   US   Rq                  U	5        GM     [s        U5      $ UR                  Rw                  S!S"9Ry                  5       n/ nU Hq  nUR                  R{                  5       nUR                  Rw                  S#S$9R{                  5       nURq                  UR|                  UR                  UUX-
  S%.5        Ms     [s        S&[        U5      US'.5      $ ! URt                   a    [s        SSU S30SS 9s $ f = f! [         a"  n[s        SS([        U5       30S)S 9s S*nA$ S*nAff = f)+zU
Retrieve PDF data from the database for a specific application or all applications.
r   r   rN   rN   )rS   rO   rP   rQ   rR   r~   r   r   r   r   r   )rN   application_datapdf_documents)id	file_namer   document_typer   r   r   r   extraction_statuserror_message
created_atr   land_descriptionregistered_proprietorencumbrancesactivity_last_125_daysadministrative_noticesproposed_userz   applicant_namecontact_namecontact_addresscontact_emailcontact_phoneapplicant_addressapplicant_emailapplicant_phonelot_sizesite_coverage)
total_areaground_floor_areafirst_floor_areapossposr   r'   zApplication z
 not foundrw   r   F)pdf_documents__isnullr   )r   )rN   rS   r   successful_extractionsfailed_extractionszPDF data summary)r"   total_applications_with_pdfsapplicationszDatabase query failed: r)   N)BshaoApp.modelsr   r   r   r   r   r   r   allrS   rO   rP   rQ   rR   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	isoformathasattrr   r   r   r   r   r  r  rz   r  r  r  r  r  r  r	  r
  r  r  r  r  r  r  r  r   r   DoesNotExistfilterdistinctcountrN   r<   r=   r2   )r>   r   r   r   rN   applicationr   r   pdf_docpdf_datar{   applications_with_pdfssummaryapp	pdf_countr  rI   s                    rJ   getPdfDataFromDatabaser&    s   
fWXX )9:Ff4<<@@P^@_ + 9 9 = = ? '5(3(@(@$/$8$8*5*D*D+6+F+F + 0 0,7,H,H/:/N/N-8-J-J,7,H,H0;0P0P2=2T2T) &($  -G%jj%,%6%6%,%6%6)0)>)>$+$4$4&-&8&8'.':':+2+B+B-4-F-F)0)>)>&-&8&8&B&B&D H w(899$+$:$:	6.	0J0J63Y5T5T6 +I,B,B6 5i6V6V	6
 5i6V6V6 +I,B,B6 *9+@+@6 -i.F.F6 +I,B,B6 .y/H/H6 ,Y-D-D6 ,Y-D-D6 01L1L6 .y/H/H6 .y/H/H6  '	(:(:!6" ,Y-D-D#6$ +4*>*>1:1L1L090J0J#,==$-NN-6!122 ?+228<W  -Z $F++ &<%C%C%J%Jaf%J%g%p%p%r"G---335	),):):)A)AT])A)^)d)d)f&&)&8&8$'$4$4"+.D*3*L  	 .  -03G '!  ' *66 f#W^<LJ.W$Xadeef2  WW(?Ax&HIRUVVWsB   -N/ J N	 B7N/ 	 N,)N/ +N,,N/ /
O9OOOPOSTc                     U R                   nUR                  SS5      R                  5       nU(       d  [        SS0[        R
                  S9$ [        5       nUR                  U5      n[        SUS   US   US	   S
.5      $ ! [         a0  n[        SS[        U5       30[        R                  S9s SnA$ SnAff = f)z(
Main chat endpoint for the RAG chatbot
r"   rq   r'   zMessage is requiredr   Tresponsesourcesr   )r   r)  r*  r   An error occurred: N)r   r   r   r   r   HTTP_400_BAD_REQUESTr   chatr=   r2   HTTP_500_INTERNAL_SERVER_ERROR)r>   r   r"   chatbotr   rI   s         rJ   chat_endpointr0    s    9||((9b)//1.113 3
 $% g&z*i( .	
  	  9*3q6(3
779 	99s$   AB 3B 
B=%B82B=8B=c                      [        5       nUR                  5       n[        SUS.5      $ ! [         a0  n[        SS[	        U5       30[
        R                  S9s SnA$ SnAff = f)z1
Get statistics about the chatbot knowledge base
T)r   
statisticsr'   r+  r   N)r   get_statisticsr   r=   r2   r   r.  )r>   r/  statsrI   s       rJ   chatbot_statsr5  )  ss    9#%&&(
  	
  9*3q6(3
779 	99   '* 
A$%AA$A$c                      [        5       nUR                  5         [        SSS.5      $ ! [         a0  n[        SS[	        U5       30[
        R                  S9s SnA$ SnAff = f)z.
Rebuild the FAISS vector store from database
Tz!Vector store rebuilt successfully)r   r"   r'   r+  r   N)r   build_vector_storer   r=   r2   r   r.  )r>   r/  rI   s      rJ   rebuild_vector_storer9  >  sq    9#%""$:
  	
  9*3q6(3
779 	99r6  c                     U R                   R                  SS5      nU R                   R                  SS5      nU R                   R                  SS5      nU R                   R                  SS5      n[        R                  R	                  5       nU(       a,  UR                  [        US9[        US9-  [        US9-  5      nU(       a  UR                  US9nU(       a  UR                  US	9nU(       a  UR                  US9nUS
S n/ nU Hx  nUR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                   S.	5        Mz     [#        SU[%        U5      S.5      $ ! [&         a0  n[#        SS[)        U5       30[*        R,                  S9s S
nA$ S
nAff = f)z5
Search development applications by various criteria
qrq   councilrO   r~   )application_id__icontains)council_name__icontains)development_type__icontains)decision__icontainsN2   )	rN   rS   rO   rP   rQ   rR   r~   r   r   T)r   r  r  r'   r+  r   )r   r   r   r   r  r  r   r   rN   rS   rO   rP   rQ   rR   r~   r   r   r   r<   r=   r2   r   r.  )	r>   queryr<  rO   r~   r  resultsr$  rI   s	            rJ   search_applicationsrD  S  s   29R(++//)R0;;??:r2";;??+=rB-5599;'..E2%01e45L '..w.OL'..8.LL'..K[.\L $CR(CNN"%"4"4 # 0 0LL"%"4"4#&#6#6$'$8$8'*'>'>%(%:%:
 
   #\
  	  9*3q6(3
779 	99s   FF" "
G,%GGGc                     [         R                  R                  US9n/ nUR                  R	                  5        GH  nUR
                  UR                  UR                  UR                  UR                  UR                  UR                  S.n[        US5      (       a  UR                  (       a  UR                  nUR                  UR                  UR                   UR"                  UR$                  UR&                  UR(                  UR*                  UR,                  UR.                  UR0                  S.US'   UR3                  U5        GM     UR4                  UR6                  UR8                  UR:                  UR<                  UR>                  UR@                  URB                  URD                  URF                  URH                  URJ                  URL                  US.n[O        SUS.5      $ ! [         RP                   a    [O        SS	0[R        RT                  S
9s $ [V         a0  n[O        SS[Y        U5       30[R        RZ                  S
9s SnA$ SnAff = f)z7
Get detailed information about a specific application
r   )r   r   r   r   r   r   r   r   )r   r   r  rz   r  r  r  r  r  r  r  )rN   r   rS   rO   rP   rQ   rR   r~   r   r   r   r   r   r   T)r   r  r'   zApplication not foundr   r+  N).r   r   r   r   r  r   r   r   r   r   r   r   r  r   r   r   r  rz   r  r  r  r  r  r  r  r   rN   r   rS   rO   rP   rQ   rR   r~   r   r   r   r   r   r   r  r   HTTP_404_NOT_FOUNDr=   r2   r.  )	r>   rN   r  r   pdfr!  r{   r   rI   s	            rJ   get_application_detailsrH    s,   @9,44888W ,,002C ]]!$!2!2LL!nn"#&#6#6%(%:%:H s,--#2D2D..	(1(B(B-6-L-L$-$:$:#,#8#8&/&>&>$-$:$:%.%<%<%.%<%< ) 2 2%.%<%<"+"6"6.)*   *9 3> *88*::'44#,,)88*::$$ + < <#.#B#B!,!>!> + < <$/$D$D&1&H&H*
" !
  	
 ".. -,
++- 	-  9*3q6(3
779 	99s$   G0G3 3.I#	I,%IIIc                 .    [         R                  R                  SSS9R                  5       nU Vs/ sH  o"(       d  M  UPM     nn[	        SUS.5      $ s  snf ! [
         a0  n[	        SS[        U5       30[        R                  S9s SnA$ SnAff = f)	z*
Get list of all councils in the database
rS   Tflat)r   councilsr'   r+  r   N	r   r   values_listr  r   r=   r2   r   r.  )r>   rL  r<  rI   s       rJ   get_councilsrO    s    9)11==nSW=Xaac+3?8wG8? 
  	 @  9*3q6(3
779 	993   0A 	AAA A 
B$%B	BBc                 .    [         R                  R                  SSS9R                  5       nU Vs/ sH  o"(       d  M  UPM     nn[	        SUS.5      $ s  snf ! [
         a0  n[	        SS[        U5       30[        R                  S9s SnA$ SnAff = f)	z3
Get list of all development types in the database
r~   TrJ  )r   development_typesr'   r+  r   NrM  )r>   rR  dev_typerI   s       rJ   get_development_typesrT    s    92::FFGY`dFennp6GT6G(8X6GT!2
  	 U  9*3q6(3
779 	99rP  c                     [        U S5      $ )z
Serve the chatbot interface
zchatbot.htmlr	   )r>   s    rJ   chatbot_interfacerV    s     '>**ru   );r*   r   pandasr.   django.confr   django.httpr   django.views.decorators.httpr   r  r   openaishaoApp.promptsr   reshaoApp.functionsr   django.shortcutsr
   django.views.decorators.csrfr   django.utils.decoratorsr   django.viewsr   rest_framework.decoratorsr   r   rest_framework.permissionsr   rest_framework.responser   rest_frameworkr   rag_servicer   modelsr   r   django.db.modelsr   rK   re   ro   rt   r   r   r   r   r&  r0  r5  r9  rD  rH  rO  rT  rV   ru   rJ   <module>rk     sg   	     $ = 1  : 	   # $ 4 = 4   B / , ! * I I  ug,N ,N^ ugS S>6 ugJT JTZ ug7N 7Nt ug%S %SP ugiW iWX ugjW jWZ 
6(XJ9   9@ 
5'XJ9   9& 
6(XJ9   9& 
5'XJ69   69r 
5'XJD9   D9N 
5'XJ9   9& 
5'XJ9   9&+ru   