
    q(i                     T    d dl Z d dlmZmZ d dlmZ  e       Z G d d      Zdad Zy)    N)GPT2TokenizerGPT2LMHeadModel)
get_configc                   ,    e Zd ZdZd ZddZddZd Zy)RecipeGeneratorz#GPT-2 based recipe generation modelc                    t         j                  | _        t         j                  | _        	 t        j                  | j                        | _        t        j                  | j                        | _	        | j                  j                  | j                         | j                  j                          t        d| j                          y# t        $ r&}t        d|        d| _	        d| _        Y d}~yd}~ww xY w)z"Initialize the model and tokenizeru   ✓ Recipe model loaded on u   ✗ Error loading model: N)configDEVICEdevice
MODEL_NAME
model_namer   from_pretrained	tokenizerr   modeltoevalprint	Exception)selfes     E/Users/apple/Desktop/TU/AI/AI-Recipe-Generator/models/recipe_model.py__init__zRecipeGenerator.__init__
   s    mm ++		"*::4??KDN(88IDJJJMM$++&JJOO/}=> 	"-aS12DJ!DNN	"s   BC 	C;C66C;c                    | j                   | j                  y	 dj                  |      }d| }|r|d| z  }|dz  }| j                  j                  |d      j	                  | j
                        }t        j                         5  | j                   j                  ||j                  d	   |z   d	t        j                  t        j                  d
d| j                  j                  t        j                  |      	      }ddd       | j                  j                  d   d
      }|S # 1 sw Y   +xY w# t         $ r&}	t#        d|	        dt%        |	       cY d}	~	S d}	~	ww xY w)a  
        Generate a recipe based on ingredients and meal type
        
        Args:
            ingredients (list): List of ingredients (e.g., ["chicken", "garlic", "tomato"])
            meal_type (str): Type of meal (e.g., "breakfast", "lunch", "dinner")
            num_words (int): Approximate number of words to generate
        
        Returns:
            str: Generated recipe text
        Nz0Model not available. Please check configuration.z, zRecipe with z for z:
pt)return_tensors   T   )
max_lengthnum_return_sequencestemperaturetop_p	do_sampleno_repeat_ngram_sizepad_token_idattention_maskr   )skip_special_tokenszError generating recipe: z"Error: Could not generate recipe. )r   r   joinencoder   r   torchno_gradgenerateshaper	   TEMPERATURETOP_Peos_token_id	ones_likedecoder   r   str)
r   ingredients	meal_type	num_wordsingredients_strprompt	input_ids
output_idsreciper   s
             r   generate_recipezRecipeGenerator.generate_recipe   sY    ::!7E"	A"ii4O#O#45FE)--eOF --fT-JMMdkkZI  !ZZ00(q1I=)* & 2 2 ,,")*!%!<!<#(??9#= 1 

 ^^**:a=d*SFM! $  	A-aS127Ax@@	As7   A/D8 
A8D,)D8 ,D51D8 8	E'E"E'"E'c                 `    g }|D ]&  }| j                  |||      }|j                  |       ( |S )a  
        Generate multiple recipes
        
        Args:
            ingredients_list (list): List of ingredient lists
            meal_type (str): Type of meal
            num_words (int): Words to generate
        
        Returns:
            list: List of generated recipes
        )r;   append)r   ingredients_listr4   r5   recipesr3   r:   s          r   generate_batchzRecipeGenerator.generate_batchM   s?     + 	#K))+y)LFNN6"	#     c                 >    | j                   duxr | j                  duS )z&Check if model is loaded and availableN)r   r   )r   s    r   is_availablezRecipeGenerator.is_available`   s    zz%D$..*DDrA   N)    )__name__
__module____qualname____doc__r   r;   r@   rC    rA   r   r   r      s    -" 1Af&ErA   r   c                  .    t         
t               a t         S )z6Get or create recipe generator instance (lazy loading))recipe_generatorr   rJ   rA   r   get_recipe_generatorrM   h   s     *,rA   )	r)   transformersr   r   r	   r   r   rL   rM   rJ   rA   r   <module>rO      s1     7 	[E [E~  rA   