Ë
    N§Ñhm  ã                   ó  — d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlZd dlZd dlZd dlZd dlmZ d dlZd d	lmZ  e«         ej*                  d
«      ej,                  d
<   dZ edge¬«      Z edd¬«      Z eee¬«      Zdd„Zd„ Zedk(  rn ej<                  d¬«      ZejA                  ddd¬«       ejA                  de!dd¬«       ejE                  «       Z#e#jH                  Z$e#jJ                  Z% ee$e%«       yy)é    )ÚTokenTextSplitter)ÚPromptTemplate)ÚLLMChain)Ú
ChatOpenAI)ÚPyPDFLoader)Ú	PdfReaderN)Údatetime)Úload_dotenvÚOPENAI_API_KEYaZ  
You are an expert at creating multiple-choice questions (MCQs) with correct answers and detailed explanations.
Your goal is to prepare students for their exams by creating MCQs from the given material.
------------
{text}
------------
For each question, follow this format:
  Question: Write a clear and concise question.
  


  Options: Provide four options (labeled A, B, C, and D) with one correct answer.
  


  Correct Answer: Clearly specify which option is correct.
  


  Explanation: Provide a detailed explanation of the correct answer to help the student understand the concept.
  


MCQs:
Útext)Úinput_variablesÚtemplatezgpt-3.5-turbog333333Ó?)ÚmodelÚtemperature)ÚllmÚpromptc                 óp  — | j                  d«      d   }|dk(  r4t        | «      }|j                  «       }d}|D ]  }||j                  z  }Œ n@|dk(  r;t	        | d«      5 }t        j                  |«      }ddd«       dj                  «      }t        ||¬	«      }	|	j                  «      S # 1 sw Y   Œ8xY w)
aF  
    Splits the text into chunks based on token count using TokenTextSplitter.

    Args:
        text (str): The input text to be split.
        chunk_size (int): Maximum number of tokens per chunk.
        chunk_overlap (int): Number of tokens to overlap between chunks.

    Returns:
        list: List of text chunks.
    ú.éÿÿÿÿÚpdfÚ ÚjsonÚrNÚ ©Ú
chunk_sizeÚchunk_overlap)	Úsplitr   ÚloadÚpage_contentÚopenr   Újoinr   Ú
split_text)
Ú	file_pathr   r   Ú	file_typeÚloaderÚdataÚquestion_genÚpageÚfileÚsplitters
             ú//var/www/html/eduruby.in/utils/mcq_generator.pyÚchunk_text_with_token_splitterr-   .   s¸   € ð o‰o˜cÓ" 2Ñ&€IØ%ÒÜ˜YÓ'ˆØ{‰{‹}ˆØˆØò 	,ˆDØ
˜$×+Ñ+Ñ
+‰,ñ	,à	VÒ	Ü	˜3Óð  4Üy‰y˜‹ˆ÷à—X‘X˜d“^€lä ¨JÀmÔT€HØ×Ñ˜|Ó,Ð,÷ð ús   ÁB,Â,B5c           	      óþ  — t        | dd¬«      }t        dt        |«      › «       g }t        |«      D ]Ò  \  }}t        d|dz   › dt        |«      › d«       	 t        j                  d	|i«      }|j                  d
«      }|j                  d«      }t        |«      D ]  }	|j                  d«       Œ t        |«      }
t        |«       t        d|
d«      D ].  }||   ||dz      ||dz      ||dz      g}|j                  |«       Œ0 ŒÔ t        |dd¬«      5 }t        j                  ||dd¬«       ddd«       t        d|› «       |S # t        $ r}t        d|dz   › d|› «       Y d}~Œ8d}~ww xY w# 1 sw Y   ŒDxY w)zs
    Splits large text into chunks, generates MCQs for each chunk,
    and saves the results into a JSON file.
    éÜ  éÈ   r   zTotal Chunks Created: zProcessing Chunk é   ú/z...r   z

z---r   é   é   é   zError processing chunk z: NÚwzutf-8)ÚencodingF)ÚindentÚensure_asciizMCQs saved to )r-   ÚprintÚlenÚ	enumerateÚ	mcq_chainÚrunr   ÚcountÚrangeÚremoveÚextendÚ	Exceptionr!   r   Údump)Ú
input_textÚoutput_fileÚtext_chunksÚmcqsÚidxÚchunkÚresponseÚmcq_listr?   Ú_ÚnÚiÚtempÚeÚ	json_files                  r,   Úmcq_mainrS   L   s   € ô 1°ÈÐ\_Ô`€KÜ	Ð"¤3 {Ó#3Ð"4Ð
5Ô6à€DÜ Ó,ò <‰
ˆˆUÜÐ! #¨¡' ¨!¬C°Ó,<Ð+=¸SÐAÔBð	<ä —}‘} f¨e _Ó5ˆHà—~‘~ fÓ-ˆHØ—.‘. Ó'ˆEÜ˜5“\ò %Øo‰o˜eÕ$ð%äH“ˆAÜ(ŒOÜ˜1˜a “^ò "à˜Q‘KØ˜Q ™U‘OØ˜Q ™U‘OØ˜Q ™U‘Oð	ð —‘˜DÕ!ñ"ð<ôB 
ˆk˜3¨Ô	1ð A°YÜ	‰	$˜	¨!¸%Õ@÷Aô 
ˆN˜;˜-Ð
(Ô)ØÐøô# ò 	<ÜÐ+¨C°!©G¨9°B°q°cÐ:×;Ò;ûð	<ú÷Að Aús%   ÁB.EÄE3Å	E0ÅE+Å+E0Å3E<Ú__main__z3StudyBuddy app for generating questions and answers)Údescriptionz--file_pathTzPaths of the files)ÚrequiredÚhelpz--output_pathz)Output file path for storing the response)ÚtyperV   rW   )r/   éd   )&Úlangchain.text_splitterr   Úlangchain.promptsr   Úlangchain.chainsr   Úlangchain_openair   Ú$langchain_community.document_loadersr   ÚPyPDF2r   ÚpandasÚpdÚopenaiÚosr   r	   ÚargparseÚdotenvr
   ÚgetenvÚenvironÚmcq_prompt_templater   r   r=   r-   rS   Ú__name__ÚArgumentParserÚparserÚadd_argumentÚstrÚ
parse_argsÚargsr$   Úoutput_path© ó    r,   ú<module>rs      s  ðÝ 5Ý ,Ý %Ý 'Ý <Ý Û Û Û 	Û Ý Û Ý Ù „à(˜rŸy™yÐ)9Ó:€‡

ÐÑ ðÐ ñ$ 
ØHØ ô
€ñ °CÔ8€Ù˜ VÔ,€	ó-ò</ðd ˆzÒØ#ˆH×#Ñ#Ð0eÔf€FØ
×ÑØØØð ô ð
 ×ÑØØØØ6ð	 ô ð ×ÑÓ€DØ~‰~€IØ×!Ñ!€KÙˆY˜Õ$ð# rr   