
    kj                         S SK r S SKrS SKr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Jr  S r " S S	5      r " S
 S5      rS rS r\S:X  a  \R(                  " \" 5       5        gg)    N)tqdm)OllamaEmbeddings)FAISS)PromptTemplate)
ChatOllamac                    [        SS9nSU  SU 3n[        R                  R                  U5      (       az  [        R                  " U5       Vs/ s H  oDR                  S5      (       d  M  UPM     nnU(       a%  [        R                  " X2SS9n[        SU 35        U$ [        S	U S
35         g [        SU  SU S
35        g s  snf )Nznomic-embed-text)modelzmy_embeddings_video//)z.faissz.pklT)allow_dangerous_deserializationzEmbeddings loaded from zNo embedding files found in .z"No embeddings found for client_id z and reference_id )	r   ospathisdirlistdirendswithr   
load_localprint)	client_idreference_id
embeddingsreference_dirfembedding_filesvectorss          J/var/www/eduai.edurigo.com/question_generate/testing/generate_mcq_video.pyload_all_embeddingsr      s    !(:;J*9+Q|nEM	ww}}]##&(jj&?b&?::N`Ca1&?b&&}bfgG+M?;<N0qAB  	29+=OP\~]^_` cs   C+Cc                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
QuestionHistory   c                 J    SU SU S3U l         U R                  5       U l        g )Nzquestion_history_video/r
   z.json)	file_pathload	questions)selfr   r   s      r   __init__QuestionHistory.__init__   s%    29+Q|nER    c                     [         R                  R                  U R                  5      (       an   [	        U R                  S5       nUR                  5       R                  5       nS S S 5        W(       a  [        [        R                  " U5      5      $ [        5       $ [        S5        [        5       $ ! , (       d  f       NS= f! [        R                   a&    [        SU R                   S35        [        5       s $ f = f)NrzWarning: Invalid JSON in z!. Starting with an empty history.z6No history file found. Starting with an empty history.)r   r   existsr!   openreadstripsetjsonloadsJSONDecodeErrorr   )r$   r   contents      r   r"   QuestionHistory.load"   s    77>>$..))$..#.!ffhnn.G /3:s4::g./EE
 JL5L /. '' 1$..1AAbcdus/   C B2%-C 	C 2
C <C 7C=<C=c                 0   [         R                  " [         R                  R                  U R                  5      SS9  [        U R                  S5       n[        R                  " [        U R                  5      U5        S S S 5        g ! , (       d  f       g = f)NT)exist_okw)
r   makedirsr   dirnamer!   r+   r/   dumplistr#   )r$   r   s     r   saveQuestionHistory.save/   sT    
BGGOODNN3dC$..#&!IId4>>*A. '&&s   +B
Bc                 :    U R                   R                  U5        g N)r#   addr$   questions     r   r?   QuestionHistory.add4   s    8$r'   c                     XR                   ;   $ r>   )r#   r@   s     r   __contains__QuestionHistory.__contains__7   s    >>))r'   c                 ,    [        U R                  5      $ r>   )lenr#   )r$   s    r   __len__QuestionHistory.__len__:   s    4>>""r'   )r!   r#   N)__name__
__module____qualname____firstlineno__r%   r"   r;   r?   rD   rH   __static_attributes__ r'   r   r   r      s     %/
%*#r'   r   c                        \ rS rSrS rS rSrg)RateLimiter=   c                 *    Xl         X l        / U l        g r>   max_requestsperiodrequests)r$   rU   rV   s      r   r%   RateLimiter.__init__>   s    (r'   c                   #    [         R                   " 5       nU R                   Vs/ s H  o!U-
  U R                  :  d  M  UPM     snU l        [        U R                  5      U R                  :  aB  U R                  XR                  S   -
  -
  nUS:  a  [
        R                  " U5      I S h  vN   U R                  R                  [         R                   " 5       5        g s  snf  N87f)Nr   )timerW   rV   rG   rU   asynciosleepappend)r$   nowreq
sleep_times       r   waitRateLimiter.waitC   s     iik(,QsT[[9PQt}}!2!22mmA.>(>?JA~mmJ///TYY[) R 0s#   $C)C" C"A(C).C'/9C)rT   N)rJ   rK   rL   rM   r%   ra   rN   rO   r'   r   rQ   rQ   =   s    
*r'   rQ   c           	        ^^^^^#    [        X5      nU(       d  [        S5        / SS4$ [        X5      mUR                  SS0S9n[	        SSS9m[        S	S
/SS9m[        R                  " S[        R                  5      m/ nSnSn[        SSS9mUUUUU4S jn	[        USS9 n
[        R                  " 5       S-   nSnUR                  U5      I S h  vN nU Vs/ s H  oR                  PM     nn[        U5      U:  Ga+  [        R                  " 5       U:  Ga  U[        U5      -
  n[        US5      n[         R"                  " U5      n U	" UUU5      I S h  vN nU H  n[        U5      U:  d  M  US   T;  d  M  UR%                  U5        TR'                  US   5        US-  nU[        US   R)                  5       5      [+        S US   R-                  5        5       5      -   -  nM     U
R/                  S5        [        U5      U:  a  [        R                  " 5       U:  a  GM  [        U5      U:  a  [        S[        U5       SU S35        TR3                  5         U[        T5      U4sS S S 5        $  GNs  snf  GN,! [0         a  n[        SU 35         S nAGM  S nAff = f! , (       d  f       g = f7f)Nz=No embeddings found for the given client ID and reference ID.r   k
   )search_kwargszhttp://127.0.0.1:11434z
gemma3:12b)base_urlr	   contextnum_questionsa:  
        {context}
        Generate {num_questions} multiple-choice questions based on the information provided above. Each question should have one correct option and three incorrect options. Use the following format:
        Q:
        A)
        B)
        C)
        D)
        Correct:
        Ensure questions are concise and directly related to the content. Try to cover different aspects of the provided information. Do not include phrases like "in the document", "according to the text", or any other references to the source material in the questions.
        )input_variablestemplatezFQ: (.+?)\nA\) (.+?)\nB\) (.+?)\nC\) (.+?)\nD\) (.+?)\nCorrect: ([A-D])      <   )rU   rV   c           
        >#    TR                  5       I S h  vN   TR                  XS9nTR                  U/5      I S h  vN nUR                  n/ nTR	                  U5       H  nUR                  5       u  pppUR                  5       n[        R                  " SSU[        R                  S9R                  5       n[        R                  " SSU[        R                  S9R                  5       nUT;  d  M  UR                  U[        U5      -   UU	R                  5       U
R                  5       UR                  5       UR                  5       S.US.5        M     U$  GNG GN!7f)N)rh   ri   ze\b(in|from|according to|mentioned in|stated in|as per) (the|this)? ?(document|text|passage|content)\b )flagszQ^(The|This) (document|text|passage|content) (states|mentions|says|indicates) that)ABCD)numberrA   optionscorrect_answer)ra   formatainvoker2   finditergroupsr-   resub
IGNORECASEr]   rG   )rh   start_number
batch_sizepromptresponseresponse_strr#   matchrA   option_aoption_boption_coption_dcorrect_letterllm	mcq_regexmcq_templatequestion_historyrate_limiters                 r   process_context+generate_mcq_video.<locals>.process_contextm   s}    !!!$$W$OfX..''	''5EOT||~LHH~~'H vv  G  IK  MU  ]_  ]j  ]j  k  q  q  sHvvrtv  yA  IK  IV  IV  W  ]  ]  _H//  *S^; (%^^-%^^-%^^-%^^-	  '5
" 
 6( 5 	".s)   E$E(E$ E!B4E$9A&E$!E$zGenerating MCQs)totaldesci,  z6Provide a comprehensive overview of the entire content   rA   c              3   T   #    U  H  n[        UR                  5       5      v   M      g 7fr>   )rG   split).0options     r   	<genexpr>%generate_mcq_video.<locals>.<genexpr>   s2       PK  sJhnPSTZT`T`TbPcPc  sJs   &(rw   zError processing context: zWarning: Only generated z unique questions out of z requested.)r   r   r   as_retrieverr   r   r}   compileDOTALLrQ   r   rZ   rz   page_contentrG   minrandomchoicer]   r?   r   sumvaluesupdate	Exceptionr;   )r   ri   r   GPUr   	retrievermcqsquestion_numbertotal_token_countr   pbartimeoutqueryresultsresultcontextsremaining_questionsr   rh   new_mcqsmcqer   r   r   r   r   s                         @@@@@r   generate_mcq_videor   L   s    !):GMN1ax&y?$$C9$=I
)C ""O4
L 

dfhfofopIDOBr:L : 
M(9	:d))+#H!))%006=>gF''g>$i-'DIIK',A"/#d)";0!4JmmH-G!0/:!VV#C4y=0S_L\5\C((,,S_='1,)SZ1F1F1H-IC  PKruv  sA  sH  sH  sJ  PK  MK  .K  K) $ A $i-'DIIK',A( t9}$,SYK7PQ^P__jklS)*,=== 
;	: 1> W  21#67- 
;	:s   BK%/J2J J2J2AJ2JJJ7JBJ(J2/AJ26
K J2J2J
J/J*#J2*J//J22
K <Kc                  d  #    Sn SnSn[        XU5      I S h  vN u  p4nU(       d  [        S5        g [        SU 35        [        SU 35        U HX  n[        SUS    SUS	    35        US
   R                  5        H  u  px[        U SU 35        M     [        SUS    S35        MZ     g  N7f)Nr   r   z'No multiple-choice questions generated.zTotal questions in history: zTotal token count: z	Question rv   z: rA   rw   z) zCorrect Answer: rx   
)r   r   items)	r   ri   r   r   history_counttoken_countr   r   texts	            r   mainr      s     IML-?	Zf-g'g$D78	(
89	}
-.	#h-3z?*;<=	N002LFVHBtf%& 3 %5!6 7r:;	  (hs   B0B.BB0__main__)r   r   r/   r[   r}   rZ   r   langchain_ollamar    langchain_community.vectorstoresr   langchain_core.promptsr   r   r   r   rQ   r   r   rJ   runrO   r'   r   <module>r      si    	    	   - 2 1 ' # #@* *\>|<& zKK r'   