o
    kh                     @   sT  d dl mZmZmZ d dlmZ d dlmZmZm	Z	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G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZdZeddddZejedgddgdgd eddd Zejded d!ed"efd#d$Zd%d& Z d'e!fd(d)Z"dS )*    )FastAPIHTTPExceptionBackgroundTasks)	BaseModel)DictListAnyOptionalN)CORSMiddlewarec                   @   s&   e Zd ZU eed< eed< eed< dS )Skillskill_id
skill_type
skill_nameN__name__
__module____qualname__str__annotations__ r   r   R/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Kunal/edurigo_ai_kunal_fast_api.pyr      s   
 r   c                   @   sN   e Zd ZU eed< eed< g Zee ed< g Zee ed< g Zee	 ed< dS )UserInfo	user_name	managerIdassignedCoursescompletedCoursesskillsN)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   
 r   c                   @   s>   e Zd ZU eed< eed< eed< eed< g Zee ed< dS )CoursecourseIdnameshort_descriptiondescriptionr   N)r   r   r   r   r   r   r   r   r   r   r   r   r      s   
 r   c                   @   sB   e Zd ZU eed< eed< eeef ed< eeee	 f ed< dS )CareerGuidanceRequest	client_id
user_query	user_dataclient_all_courses_dataN)
r   r   r   intr   r   r   r   r   r   r   r   r   r   r"      s
   
 r"   c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )RecommendedCourser   
courseName
matchScorereasonscoreNr   r   r   r   r   r(   %   s   
 r(   c                   @   sb   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< d	Zeed
< eed< dS )OnlineCourser   r)   platform
instructordurationr*   r+   r,   TisOnlineCourseurlN)r   r   r   r   r   r1   boolr   r   r   r   r-   ,   s   
 r-   c                   @   s6   e Zd ZU eed< ee ed< g Zeee	  ed< dS )CareerGuidanceResponseanswerrecommended_coursesonline_course_recommendationsN)
r   r   r   r   r   r   r(   r7   r	   r-   r   r   r   r   r4   9   s   
 r4   ztest.pyzCareer Guidance APIzCAPI for career guidance recommendations using Ollama with Gemma 12Bz1.0.0)titler!   version*T)allow_originsallow_credentialsallow_methodsallow_headers/c                      s   dddS )zHealth check endpointzAPI is runningzWelcome to Career Guidance API)statusmessager   r   r   r   r   rootR   s   
rB   z/career-guidance)response_modelrequestbackground_tasksc           
   
      s  zG|   }tjdddd}|j}t|| W d   n1 s"w   Y  t }|dt	t
|I dH }|j }t|}|t| |W S  tjy_ }	 z
tdd|	j dd}	~	w tjyv }	 ztdd	t|	 dd}	~	w ty }	 ztdd
t|	 dd}	~	ww )zG
    Process career guidance request by invoking the Python script
    zw+z.jsonF)modesuffixdeleteNi  zError executing Python script: )status_codedetailzError parsing JSON output: zUnexpected error: )dicttempfileNamedTemporaryFiler   jsondumpasyncioget_event_looprun_in_executorexecute_subprocessPYTHON_SCRIPT_PATHstdoutstriploadsadd_taskcleanup_temp_file
subprocessCalledProcessErrorr   stderrJSONDecodeErrorr   	Exception)
rD   rE   payloadtmp_filetemp_file_pathloopresultoutputresponse_dataer   r   r   get_career_guidanceW   s.   

rg   c                 C   s   t jd| |gdddd}|S )z)Execute the subprocess and capture outputpython3T)capture_outputtextcheck)rZ   run)script_pathra   rc   r   r   r   rS   {   s   rS   	file_pathc              
   C   sJ   zt j| rt |  W dS W dS  ty$ } zW Y d}~dS d}~ww )z&Remove temporary file after processingN)ospathexistsunlinkr^   )rn   rf   r   r   r   rY      s   rY   )#fastapir   r   r   pydanticr   typingr   r   r   r	   rZ   rN   rL   ro   fastapi.middleware.corsr
   rP   r   r   r   r"   r(   r-   r4   rT   appadd_middlewaregetrB   postrg   rS   r   rY   r   r   r   r   <module>   sF    
#
