+
    5ieP                        R t ^ RIt^ RIt^ RIt^ RIt^ RIHt ]P                  P                  ]P                  P                  ]4      4      t	]P                  P                  ]	R4      t]P                  P                  ]R4      t]P                  P                  ]R4      tRR^ZR^R	R
R^RR/RR^iRRR	^RRRR/RR^R^
R	^R^RR/RR^R^R	^-RR
RR/RR^R^R	^ R^RR/RR^2R^R	^R^RR//tR tR tR$R ltR$R ltR$R ltR tR tR t]R8X  d   ^ RIt]! ]P4                  4      ^8  d   ]! R4       ]P8                  ! ^4        ]P:                  ! ]P4                  ^,          4      t]! ]4      t#]! ]PH                  ! ]#R"^R#7      4       R# R#   ] dW    ] ! ]P4                  ^,          R R!7      ;_uu_ 4       t!]PD                  ! ]!4      tRRR4        Ko    + '       g   i      L; ii ; i)%zcProcess a single food event JSON and update CSVs.
Usage: import and call process_event(event_dict)
N)datetimedatazdashboard_food_log.csvzdashboard_daily_summary.csv   ביצהcalproteincarbs      ?fatunit   בננה?333333?   יוגורטcupu   אורז   עוףservingu   קפה עם חלבc                 `   . pT p\         P                  4        EF  pR pW29   d   VP                  V4      pRpMRP                  VP	                  4       4      R,          pV'       d}   \
        P                  ! \
        P                  ! V4      R,           V4      '       dE   \
        P                  ! \
        P                  ! V4      R,           V4      pVP                  4       pRpV'       g   K  ^pRp	V\        ^ X^,
          4      V p
\
        P                  ! RV
4      pV'       d   \        VP                  ^4      4      pMDW%V^,            p\
        P                  ! RV4      pV'       d   \        VP                  ^4      4      pVP                  RVRVRV	R	\         V,          /4       EK  	  V# )
FT :N   Nz\w*r
   u   (\d+)\s*(?:גרם|g)?	food_namequantitydb)FOOD_DBkeysfindjoinsplitresearchescapestartmaxintgroupappend)raw_textitemstextnamefoundidxrootmposqtyr
   leftmrightm2s   &             F/home/roman/AI-Coach/health-watcher-prod/scripts/process_food_event.pydetect_itemsr3      sL    ED<))D/CE 774::<(,D		"))D/&"8$??yy4!7>jjlC3q5M#&II.5AGGAJC SV$E3U;B$k$z#fT4PTVW; < L    c                   V P                  4       pVP                  RR4      pVR;9   d   RRR. RRRRR	R
/# RV9   g   RV9   g   VR8X  g   RV9   d"   RRRRRRRR^dRRR^R^R^ RRRRRRRR/.RRRR/# R V9   d0   R!V9   g   R"V9   d"   RRRRRRRR^RRR^R^.R^ RR#RRRRRR$/.RRRR/# R%V9   g   VR%8X  g   R&V9   g   R%V9   d"   RRRRR'RR(R^RRR^R^RR<R^RRRRRR)/.RRRR/# R*V9   d@   R+V9   d9   RRRRR*RR,R^RR-R^R^
R^R^RRRRRR./RR+RR/R^RRR^iRR0R^RR1RRRRRR2/.RRRR/# \        V 4      pV'       d   . pV F  pVP                  RVR3,          RVR3,          RVR,          RVR,          RVR4,          R5,          VR,          ,          RVR4,          R6,          VR,          ,          RVR4,          P	                  R7R4      RVR4,          P	                  R8R4      RRRR9RR:/4       K  	  RRRVRRRR/# RRR. RRRRR	R
/# )=zStub that simulates GPT-5-mini structured output for known test phrases.
Returns dict matching the required GPT structured output format.
u   ‏r   is_food_logFr&   needs_clarificationclarification_question_heNreasonnot_food_eventu   חזה עוף 100u   חזה עוף 100 גרםu   100 גרם עוףTfood_name_heu   חזה עוףcanonical_food_namechicken_breastr   r
   gestimated_caloriesestimated_protein_gestimated_carbs_gestimated_fat_gg@#nutrition_values_for_input_quantity
confidencehighassumptionsz100g cooked chicken breast150r   u   חזהg@z150g cooked chicken breastu   2 ביציםu   שתי ביציםr   eggz2 typical eggsr   r   yogurtr   z1 cup yogurtbananar   r   z1 medium bananar   r   r   r   r   r	   lowzfallback db)u#   תעדכן לי את הדשבורדr   g      ?)stripreplacer3   r$   get)r%   rtlr&   	out_itemsits   &     r2   gpt_parse_stubrS   <   sy    
	B


8BA	77ugr3H%Qlnrt|  O  P  	Pa#6"#<C]@]atxyaytW~oNcdtu  AD  EK  LO  Pd  eh  i~  A  BU  VW  Xi  jm  nS  UY  Zf  gm  n{  |X  /Y  .Z  \q  sx  zU  W[  \  	\zx1}AtW~oNcdtu  AD  EK  LO  Pd  eh  i~  A  BU  VW  Xi  jm  nS  UY  Zf  gm  n{  |X  /Y  .Z  \q  sx  zU  W[  \  	\a/3F!3K~acOctW~jI^_deopqrxy  AU  VZ  [p  qt  uH  IN  O`  ad  eJ  LP  Q]  ^d  er  sC  /D  .E  G\  ^c  e@  BF  G  	GzQtWN+@*UVW]^cdxy|  ~S  TV  Wj  km  n  @A  Bg  im  nz  {A  BO  P^  _J'<XjQRSYZ`auvy  {P  QT  Uh  ik  l}  ~A  Bg  im  nz  {A  BO  Pa  b.
 !%)DdL 	L
 "E	B;1F;Yceghresu{}  AG  ~H$bhuobn&DF[]_`d]efo]pqst~q]  BU  WY  Z^  W_  Wc  Wc  dk  ln  Wo  qB  DF  GK  DL  DP  DP  QV  WY  DZ  \A  CH  JV  W\  ^k  ly  
 tWi9NPUWrtxyy5'2/DeMhjnpx  {K  L  Lr4   c           	     T	   T;'       g    / p\        V\        4      '       d   VP                  R4      MRp\        V\        4      '       d   VP                  R4      MRpV'       d   VR8w  d   RR/# \        P                  P                  R4      pV'       g   RR	/#  ^ R
IpVP                  P                  R4      pVf   RR/# VP                  R4      p	Rp
\        T\        4      '       d   TP                  RR4      p
T
'       g)   \        P                  P                  R4      R8w  d   RR/# RpRT  R\        P                  ! T;'       g    / 4       2p^pR
p\        T^,           4       EFu  p R
p \        T	R4      '       Ed	   T	P                  P                  TP                  RR4      TR,           T,           TP                  RR4      R7      p\        TR4      '       d   \        TP                   \"        \$        34      '       dh   TP                   '       dV   \        TP                   ^ ,          R4      '       d/   TP                   ^ ,          P&                  ^ ,          P(                  MR
pEM\+        TRR
4      pEM \+        TRR
4      pM\        T	R4      '       d   T	P,                  P                  TP                  RR4      RRRT/RR RT/.TP                  RR4      R!7      p\        T\        4      '       d   TP                  R"4      M\+        TR"R
4      pT'       d]   T^ ,          p\        T\        4      '       d"   TP                  R#/ 4      P                  R4      M\+        TR#R
4      P                  R4      pT'       g   T;'       g    R$p\1        R$4      h\        P2                  ! T4      p\        T\        4      '       d   R%T9  d   R&p\1        T4      hTu # 	  R
#   \         d    RR/u # i ; i  \         d   p\/        T4      pR
p R
p?LR
p?ii ; i  \         dy   pT;'       g    \/        T4      pY8  d   \4        P7                  R'4        R
p?EK  T'       d*   TP                  R(4      '       d   \9        T 4      u R
p?u # RR)/u R
p?u # R
p?ii ; i)*zAdapter placeholder for real GPT call. Will be implemented to call GPT-5 mini.

For now, this function is disabled and will raise if use_live_gpt is True.
When enabled, it should call the OpenAI API, parse JSON, and return the structured dict.
use_live_gptFparser_modestubliveerrorlive_gpt_disabledOPENAI_API_KEYmissing_api_keyNopenaiopenai_package_missingallow_live_callALLOW_LIVE_GPT_CALL1zYou are a parser that extracts structured JSON from Hebrew free-text food logs. Return ONLY valid JSON following the schema. Do not provide extra text. Do not give diet or medical advice.z=Parse this food log into the required JSON schema. Raw text: z
Event context: 	responsesmodelz
gpt-5-mini
max_output_tokensi   )rc   inputre   outputcontentoutput_textChatCompletionrolesystemuser)rc   messages
max_tokenschoicesmessageno_response_textr6   schema_missing_is_food_logr   fallback_to_stub_on_gpt_errorgpt_invalid_schema)
isinstancedictrN   osenviron	importlibutil	find_specimport_module	Exceptionjsondumpsrangehasattrrb   createrg   listtuplerh   r'   getattrrj   str
ValueErrorloadstimesleeprS   )r%   event_contextcfg
parser_cfguse_livemodeapi_keyrz   openai_specr]   
allow_callsystem_promptuser_promptmax_retries
last_errorattempt	resp_textresponserp   firsteparseds   &&&                   r2   parse_food_with_gptr   b   sW    J1;J1M1Mz~~n-SXH,6z4,H,H:>>-(fDtv~+,, jjnn-.G*++3nn..x8566((2 J*d##^^$5u=
"**..)>?D+,,
	. 
 RRZQ[[lmqmwmw  yF  yL  yL  JL  nM  mN  OKKJQ'=	2 I !6;//%//66(nnW\B,t3kA*4..9LT*R  7  H x22%hooU|DDNUV^VeVefgVhirNsNs(:(B(B1(E(J(Jy}I(/-(NI$+HmT$J	 v'788#)#8#8#?#?",.."F'-hy&OQWX^_hitPu%v'1~~6I$'O $@ $ >HRV=W=W(,,y"9]demnwx|]}"$+AJER\]bdhRiRi		)B(?(C(CI(Novw|  ~G  HL  pM  pQ  pQ  R[  p\I
 '==+=
 !344 ZZ	*F fd++}F/J9
 ,,Mg (-  31223r  ! V
 	!$  		2#--s1vJ$

3jnn-LMM%h//011		2s   &O* O* 3P$6B(O?AO?2P$4O?P$O?P$BO?'AO?P$P$AP$*O<;O<?P!
PP$P!!P$$R'/0R"&R".R"
R"R'R"R'"R'c                     \        V 4      # )N)rS   )r%   r   r   s   &&&r2   parse_food_with_stubr      s    (##r4   c                X   Vfr    ^ RI p^ RIpVP                  P                  \        RR4      pVP                  P                  V4      '       d   VP                  \        VRR7      4      pM	RRRR	R
//p \        V\        4      '       d   VP                  R4      MRpV'       d   VP                  R4      MRpV'       d   VP                  R	4      MR
pV'       d   \        WVR7      # VR
8X  g	   V'       g   \        WVR7      # \        WVR7      #   \         d    RRRR	R
//p Li ; i)zUnified parser entrypoint. Chooses live GPT or stub based on cfg.
Returns the GPT-structured dict or raises on critical errors.
Nconfigzpipeline_config.jsonutf-8encodingparserrU   FrV   rW   )r   r   )r   rx   pathr   BASE_DIRexistsloadopenr~   rv   rw   rN   r   r   )	r%   r   r   _json_oscfg_pathr   r   r   s	   &&&      r2   parse_food_eventr      s     {	M+xx}}Xx9OPHxxx((jjh!AB.%!OP '1d&;&;"J1;z~~n-H,6:>>-(FD"8jYYv~X#HzZZ:VV  	Mne]FKLC	Ms   A	D D -D D)(D)c                    V ^,          p\         P                  P                  \        4      '       db    \	        \        RR7      ;_uu_ 4       p\
        P                  ! V4       F$  pVP                  R4      V8X  g   K   RRR4       R# 	  RRR4       \         P                  P                  \        4      p\	        \        RRRR7      ;_uu_ 4       p\
        P                  ! V4      pV'       g   VP                  . RO4       VP                  V 4       RRR4       R#   + '       g   i     L; i  \         d     Li ; i  + '       g   i     R# ; i)	   r   r   row_idNar   newliner   )event_idr   dater   sourcer%   	meal_typer   r   r
   calories	protein_gcarbs_gfat_grD   processing_statusnotes)rx   r   r   LOG_CSVr   csv
DictReaderrN   r~   writerwriterow)rowr   frr   r   s   &     r2   
append_logr      s    VF	ww~~g	g00A*AuuX&0	 10* 1 WW^^G$F	gc"g	6	6!zz!}OO  P  Q	 
7	6 10
  		 
7	6	6sN   D2 -D?D D2 
DD2 AED/	*D2 /D2 2E ?E E	c                 &   . p\        \        R R7      ;_uu_ 4       p\        P                  ! V4      pV F$  pVR,          V 8X  g   K  VP	                  V4       K&  	  RRR4       \        R V 4       4      p\        R V 4       4      p. p\        P                  P                  \        4      '       dR   \        \        R R7      ;_uu_ 4       p\        P                  ! V4      pV F  qGP	                  V4       K  	  RRR4       RpV Fj  pVR,          V 8X  g   K  \        \        V4      4      VR&   \        \        V^4      4      VR&   \        P                  ! 4       P                  4       VR	&   R
pKl  	  V'       gh   VP	                  RV R\        \        V4      4      R\        \        V^4      4      RRRRRRRRR	\        P                  ! 4       P                  4       /4       \        \        RRR R7      ;_uu_ 4       p\        P                   ! V4      p	V	P#                  . RO4       V F  pV	P#                  VP%                  RR4      VP%                  RR4      VP%                  RR4      VP%                  RR4      VP%                  RR4      VP%                  RR4      VP%                  RR4      VP%                  R	R4      .4       K  	  RRR4       R#   + '       g   i     EL; i  + '       g   i     EL; i  + '       g   i     R# ; i)r   r   r   Nc              3   Z   "   T F!  p\        VR ,          ;'       g    ^ 4      x  K#  	  R# 5i)r   Nfloat.0r   s   & r2   	<genexpr>+update_daily_summary_for.<locals>.<genexpr>  s$     <t!E!J-,,1--t   ++c              3   Z   "   T F!  p\        VR ,          ;'       g    ^ 4      x  K#  	  R# 5i)r   Nr   r   s   & r2   r   r     s$     >AU1[>..Q//r   Fr   r   last_updatedTsteps0burnedcreatinefalsesupplementsz[]wr   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r$   sumrx   r   r   	DAILY_CSVr   r"   roundr   utcnow	isoformatr   r   rN   )
date_strrowsr   readerr   	total_cal
total_protdailyr)   r   s
   &         r2   update_daily_summary_forr     s   	D	gw	'	'1~~a Ay("A  
(
 <t<<I>>>J
E	ww~~i  )W-->>!$F\\!_V . EV9hY(AeHU:a01AiL&oo/99;AnE  fXeCI,?	#eT^_`NaJbcjknowx{  }G  HO  P]  ^b  cq  rz  rA  rA  rC  rM  rM  rO  P  	Q	iB	8	8Azz!}ijAOOQUU6"-aeeE"oaeeIb>QRSRWRWX_`bRcdedidijrsudvwxw|w|  ~H  IK  xL  MN  MR  MR  S`  ac  Md  ef  ej  ej  ky  z|  e}  ~    
9	81 
(	'	' .-- 
9	8	8s*   'KK0K+CK?K(	+K<	?L	c                 :
   V P                  R 4      R8w  d$   RRRRRV P                  R4      R. RR	^ R
^ /RRRR/# V P                  R4      ;'       g    V P                  RR4      pV P                  R4      pV'       dW    \        P                  ! V4      pVP                  4       P	                  4       pVP                  4       P	                  RR7      pM\V P                  R4      ;'       g2    \        P                  ! 4       P                  4       P	                  4       pV P                  RR4      pV P                  R4      ;'       g    V P                  R4      ;'       g    Rp\        WR7      pVP                  R4      '       g$   RRRRRV P                  R4      R. RR	^ R
^ /RRRR/# VP                  R4      '       d=   RRRRRV P                  R4      R. RR	^ R
^ /RVP                  R4      ;'       g    RRR/# VP                  R. 4      pV'       g$   RRRRRV P                  R4      R. RR	^ R
^ /RRRR/# . p	^ p
^ p\        V4       EF  w  rV P                  R4      pV R V 2pVP                  R!4      '       g   RRRRRVR. RR	^ R
^ /RR"RR#/u # \        VP                  R$4      ;'       g    ^ 4      p\        VP                  R%4      ;'       g    ^ 4      pVP                  R&4      pVP                  R'4      pVP                  R(4      ;'       g    ^pVP                  R)4      ;'       g    R)pVR*8  g   V^8  d   RRRRRVR. RR	^ R
^ /RR+RR,/u # YYEYaRVP                  R-4      ;'       g    VP                  R.4      VVVV\        V4      \        V4      VP                  R/R04      R1R.p\        V4       T	P                  R2VP                  R-4      ;'       g    VP                  R.4      R(VR)VR	VR
VR3VR4VR/VP                  R/R04      /4       V
V,          p
VV,          pEK  	  \        V4       . pV	 F2  pVP                  VR2,           R5VR	,           R6VR
,           R724       K4  	  R8R9P!                  V4      ,           pRR:RR;RV P                  R4      RV	RR	V
R
V/RVRR</#   \         da    T P                  R4      ;'       g2    \        P                  ! 4       P                  4       P	                  4       pT P                  RR4      p ELi ; i)=
event_typefood_logstatusrY   dashboard_updatedFr   r&   totalsr   r   user_message_heuU   המידע התקבל, אבל לא נשמר בדשבורד. סיבה: not_food_event.error_reasonr:   r%   	user_textr   	timestampseconds)timespecr   r   r   
source_bot)r   r6   r7   r8   u   נדרשת הבהרהuK   המידע התקבל, אבל לא נמצא פריט אוכל בזיהוי.no_items_detected-rC   ud   המידע התקבל, אבל לא נשמר בדשבורד. סיבה: nutrition_value_basis_unclear.nutrition_value_basis_unclearr?   r@   rA   rB   r   r
   i  uc   המידע התקבל, אבל לא נשמר בדשבורד. סיבה: nutrition_values_unrealistic.nutrition_values_unrealisticr;   r<   rD   rE   	processedr   r   r   z - u    קלוריות, u    גרם חלבוןu$   המידע נשמר בדשבורד:
rd   successTN)rN   r   fromisoformatr   r   r   r~   r   r   	enumerater"   r   r   r   r$   r   r   )eventrawtsdtr   r   r   gpt_outr&   rb   r   r   r*   rR   eidr   r   protr   r	   r-   r
   r   items_linesuser_msgs   &                        r2   process_eventr  )  s   yy
* W1%UYYzEZRJqQ#?v+	
 	
 ))J

<
<599[#<C	;	B		(''+B779&&(D779&&	&:D
 yy HHHOO$5$:$:$<$F$F$Hyy#YYx AAEIIl$;AArFs8G;;}%%W1%UYYzEZRJqQ#?v+	
 	
 {{())W1%UYYzEZRJqQ#?w{{+FGbbKb0	
 	
 KK$EW1%UYYzEZRJqQ#?l.	
 	
 IIJE"IIj!au~vv;<<"5eZX
1['C!  $J>	  "&&-.33!4RVV1277a8*+ff&'ffZ %%Avvf~''t8tCx"5eZX
1['C!  &I=	  $Fr"&&2H2i2iBFFShLijmnrsvw{|  AF  }G  HK  LO  HP  QS  QW  QW  Xd  ek  Ql  mx  y{  |3+rvvn'='^'^H]A^_ijmntuy  {E  FI  JU  VZ  [d  ej  kr  sv  wC  DF  DJ  DJ  KW  X^  D_  `  	aS	d
= #@ T"Kbo.c"Z.1AARSUVaSbRccuvw 6;9OOH/j%))JBW:y+zB8 S  	(99V$LL(9(>(>(@(J(J(LD99VB'D	(s   ?AR/ /"TATT__main__z%pass a JSON event file or JSON stringr   r   F)ensure_asciiindent)NN)%__doc__r   r   r   rx   r   r   dirname__file__r   r   DATA_DIRr   r   r   r3   rS   r   r   r   r   r   r  __name__syslenargvprintexitr   evtr~   r   r   r   resr    r4   r2   <module>r     s     77??277??84577<<&)
'',,x!9
:GGLL#@A	 r)Agc%&Hs9SE#fVLU3yGBuQveLs9Qwr%F5IuS2gaayI5Ia%&O#L#LLp2f$W6(BaF Z
388}Q56GJJsxx{# 	cC	$**Se1
56   G#((1+w//1$))A,c////Gs*   #F *G-:GG-G)#G-)G-