
    +g                     d    d dl Z d dlZd dlmZ  edd      Zd Zd Zd Zd	 Zd
 Z	d Z
d Zd Zy)    N)pipelinezquestion-answeringz'distilbert-base-uncased-distilled-squad)modelc                    t        dd      5 }|j                         }ddd       g d}| dk  s| t        |      kD  rt        d      || dz
     \  }}t	        ||      }t        |      }t        d|  d|dd	  d
       |j                  ||j                  |            t        |      z   }|dk(  rt        d|         |g fS |j                  d|      }|dk(  rt        |      }||| j                         }	t        d|  d|	        |	st        d|  d       |t               fS d}
g }|	j                         D ]  }t        j                  |
|      }|s|j                  d      j                         j                  dd      j                  dd      }|j                  t        |j                               |d        ||fS # 1 sw Y   xY w)z
    Extracts the instructions and questions for the given phase.
    :param phase: The current phase (1 to 12)
    :return: Tuple of sanitized instructions and questions for the phase
    z'/var/www/html/test1/structured_text.txtrN))zINSTRUCTIONS for phase 1QUESTION and DECISION)zINSTRUCTIONS for phase 2r   )zINSTRUCTIONS for phase 3r   )zINSTRUCTIONS for phase 4r   )zINSTRUCTIONS for phase 5r   )zINSTRUCTIONS for phase 6r   )zINSTRUCTIONS for phase 7r   )zINSTRUCTIONS for phase 8r   )zINSTRUCTIONS for phase 9r   )zINSTRUCTIONS for phase 10r   )zINSTRUCTIONS for phase 11r   )zINSTRUCTIONS for phase 12r   )zINSTRUCTIONS for phase 13r   )zINSTRUCTIONS for phase 14r      zPhase out of rangezPhase z - Extracted Instructions: d   ...z+No 'QUESTION and DECISION' found for phase PARTz - Extracted Questions Text: z&Warning: No questions found for phase z. Returning default questions.z\([A-Z]\)\s*r   ( )textchoice)openreadlen
IndexErrorextract_chaptersanitize_textprintfindstripget_default_questions
splitlinesrematchgroupreplaceappend)phasefiler   phase_markersstart_marker
end_markerinstructionsquestions_startquestions_endquestions_textquestion_pattern	questionsliner   r   s                   /var/www/html/test1/qa_system.pyget_next_partr0      s    
7	= yy{M$ qyEC..-..  -UQY7L* #4zBL !.L 
F5'4\$35G4H
LM ii
DIIl,CDs:VO";E7CDR IIfo6MD	 /-8>>@N 
F5'6~6F
GH 6ug=[\]2444 'I))+ V)40[[^))+33C<DDS"MFmDJJL&AVTU	V ""O s   GGc                 L    t        j                  dd|       j                         S )z
    Removes headers that are all uppercase and surrounded by dashes.
    :param text: The text to process.
    :return: Text with headers removed.
    z
-[A-Z\s]+-r   )r   subr   r   s    r/   remove_headersr4   Y   s      66-T*0022    c                 &    t        | |      }|d   S )z
    Get an answer from the model based on a question and context.
    :param question: The question asked
    :param context: The context for the question
    :return: The model's answer
    )questioncontextanswer)qa_pipeline)r7   r8   results      r/   
get_answerr<   c   s     (G<F(r5   c                     d| j                         v sd| j                         v ryd| j                         v sd| j                         v ryy)z
    Generate a recommendation based on the feedback given.
    :param feedback: User feedback text
    :return: Recommendation based on feedback
    goodokayzLThank you. It's about you how you will proceed, but I recommend going ahead.badz	not readyzCTake your time. If you're not ready, feel free to repeat the phase.zEI understand. You can decide whether to continue or repeat the phase.)lower)feedbacks    r/   get_recommendationrC   m   sG     !!Vx~~/?%?]	(.."	"kX^^5E&ETVr5   c                    | j                  |      }| j                  ||      }t        d| d|        |dk(  s|dk(  rt        d| d| d      | |t        |      z   | j	                         }t        d|dd	  d
       |S )z
    Extract text between two markers, excluding the start and end markers.
    :param text: The full text.
    :param start_marker: Start of the section.
    :param end_marker: End of the section.
    :return: Extracted text.
    zStart marker found at: z, End marker found at: r   zCould not find markers 'z' and 'z
' in text.z'Extracted text (after header removal): Nr	   r
   )r   r   
ValueErrorr   r   )r   r&   r'   start_index	end_indexextracted_texts         r/   r   r   z   s     ))L)K		*k2I 
#K=0G	{
ST bIO3L>T^_`` +L(99)DJJLN
 
3N4C4H3I
MNr5   c                 B    | j                  dd      j                         S )z
    Sanitizes the text by removing unwanted characters.
    :param text: The text to sanitize.
    :return: Sanitized text.
    r   )r!   r   r3   s    r/   r   r      s     <<b!''))r5   c                      ddddddgS )z{
    Returns default questions if no specific questions are found for a phase.
    :return: List of default questions.
    z(A) Proceed to the next phase.Ar   z(B) Repeat the current phase.B rN   r5   r/   r   r      s     2SA0C@ r5   c                 H    ddddddddd	d
ddddd}|j                  | d      S )z
    Returns the context for a given phase, used as input for the AI model.
    :param phase: The current phase
    :return: Context string for the phase
    zContext for phase 1zContext for phase 2zContext for phase 3zContext for phase 4zContext for phase 5zContext for phase 6zContext for phase 7zContext for phase 8zContext for phase 9zContext for phase 10zContext for phase 11zContext for phase 12zContext for phase 13zContext for phase 14)r                        	   
               zUnknown phase context)get)r#   context_mappings     r/   get_phase_contextr_      sK     !        """""O  u&=>>r5   )jsonr   transformersr   r:   r0   r4   r<   rC   r   r   r   r_   rN   r5   r/   <module>rb      sI     	 ! +3\]N#b3W8*?r5   