
    #Yh                         d dl Z d dlZd dlmZ d dlZd dlZd dlZ edd      Zd Z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        j                  ddddd      }|j                  t         j                  j                        }|j                  d| |f       |j                         }|j                          t        j                  d|        |S )	N	localhostopenai_user,IOyg76H2l%252BewRX2xhsDJAo7qnfVDHtx9RB%253D%openai_promptsutf8mb4hostuserpasswddbcharsetz
        SELECT instruction_text, question_text, ai_prompt_id, feedbackRequest_text, prompt_template_name, section_label
        FROM phase_content
        WHERE app_id = %s AND phase_number = %s
    u$   🔎 QA_SYSTEM: Phase data fetched: )
MySQLdbconnectcursorcursors
DictCursorexecutefetchonecloselogginginfo)app_idphaseconnr   results        (/var/www/html/decompression/qa_system.pyget_next_partr       s    ??=D [[334F
NN  %	
 __FJJLLL7x@AM    c                    t        j                  ddddd      }|j                  t         j                  j                        }|j                  d| f       |j                         }|s|j                          g S |d   }|j                  d	|f       |j                         }|j                          |D cg c]  }t        |       c}S c c}w )
Nr   r   r   r	   r
   r   zE
        SELECT id FROM prompt_templates
        WHERE name = %s
    idzn
        SELECT role, content FROM prompt_messages
        WHERE prompt_id = %s
        ORDER BY sequence
    )
r   r   r   r   r   r   r   r   fetchalldict)namer   r   row	prompt_idmessagesmsgs          r   $get_prompt_messages_by_template_namer+      s    ??=D [[334F
NN   //
C

	D	I
NN  	
  HJJL!)*#DI***s   7C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]+- )resubstriptexts    r   remove_headersr3   A   s      66-T*0022r!   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)r5   r6   r   s      r   
get_answerr9   K   s     (G<F(r!   c                 H    | j                         } d| v sd| v ryd| v sd| 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_recommendationr@   U   s7     ~~HVx/]	(	kX5TVr!   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: zCould not find markers 'z' and 'z
' in text.z'Extracted text (after header removal): Nd   z...)findprint
ValueErrorlenr0   )r2   start_marker
end_markerstart_index	end_indexextracted_texts         r   extract_chapterrM   d   s     ))L)K		*k2I 
#K=0G	{
ST bIO3L>T^_`` +L(99)DJJLN
 
3N4C4H3I
MNr!   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-   )replacer0   r1   s    r   sanitize_textrQ      s     <<b!''))r!   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.A)r2   choicez(B) Repeat the current phase.B rV   r!   r   get_default_questionsrW      s     2SA0C@ r!   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)                        	   
               zUnknown phase context)get)r   context_mappings     r   get_phase_contextri      sK     !        """""O  u&=>>r!   c                 v    	 | j                  d      d   }ddddj                  |d      S # t        $ r Y yw xY w)N:rB   rY   rZ   r[   )500050015002)splitrg   	Exception)r   ports     r   detect_app_id_from_portrr      sM    zz#r"
 #dA,		
  s   ), 	88)jsonr.   transformersr   r   osr   r8   r    r+   r3   r9   r@   rM   rQ   rW   ri   rr   rV   r!   r   <module>rv      s[     	 !  	  +3\](+D3W8*?0	r!   