o
    y…h÷  ã                   @   sÌ   d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
Z
eƒ Zejedgddgdgd� e d	¡Ze	 d	¡Zeje_G d
d„ deƒZe d¡defdd„ƒZedkrdd dlZejeddd� dS dS )é    )ÚFastAPIÚHTTPException)ÚCORSMiddleware)Ú	BaseModel)ÚGPT2LMHeadModelÚGPT2TokenizerNÚ*T)Zallow_originsZallow_credentialsZallow_methodsZallow_headersZ
distilgpt2c                   @   s   e Zd ZU eed< dS )ÚQuestionÚtextN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__© r   r   ú=C:\Users\Buzz\Documents\TitanTS\Discord\TitanBot\files\api.pyr	      s   
 r	   z/askÚquestionc              
   C   s    z8d| j › d�}tj|dd�}tj||jd d ddd	d
tjd�}tj|d d
d�}|t|ƒd …  	¡ }d|iW S  t
yO } ztddt|ƒ› �d�‚d }~ww )Nu	   OtÃ¡zka: u   
OdpovÄ›Ä�:Úpt)Zreturn_tensorsé   éd   gffffffæ?gÍÌÌÌÌÌì?gš™™™™™ñ?T)Ú
max_lengthZtemperatureZtop_pZrepetition_penaltyZ	do_sampleZpad_token_idr   )Zskip_special_tokensÚansweriô  zChyba API: )Ústatus_codeZdetail)r
   Ú	tokenizerÚencodeÚmodelÚgenerateÚshapeZeos_token_idÚdecodeÚlenÚstripÚ	Exceptionr   r   )r   ÚpromptZinputsZoutputsr   Úer   r   r   Úask_ai   s&   ù	
€ÿr$   Ú__main__z0.0.0.0i@  )ÚhostÚport)Zfastapir   r   Zfastapi.middleware.corsr   Zpydanticr   Ztransformersr   r   ZtorchÚappZadd_middlewareZfrom_pretrainedr   r   Z	eos_tokenZ	pad_tokenr	   Úpostr$   r   ZuvicornÚrunr   r   r   r   Ú<module>   s.    û
	
þ