Ashhar commited on
Commit
cb87cf5
·
1 Parent(s): ccd8cad

better logging

Browse files
Files changed (2) hide show
  1. app.py +34 -0
  2. utils.py +13 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import base64
2
  import streamlit as st
3
  from openai import OpenAI
4
  import os
 
5
 
6
  # Load environment variables
7
  from dotenv import load_dotenv
@@ -56,6 +57,9 @@ st.markdown("""
56
  """, unsafe_allow_html=True)
57
 
58
  # Initialize session state
 
 
 
59
  if 'cooking_equipment' not in st.session_state:
60
  st.session_state.cooking_equipment = {
61
  'Stove': True,
@@ -120,12 +124,27 @@ def analyze_and_generate_recipe(uploaded_image, available_equipment=None, langua
120
  ]
121
  }
122
  ]
 
 
 
 
 
 
 
 
123
 
124
  # Make the LLM call
125
  response = client.chat.completions.create(
126
  model="gpt-4o-mini",
127
  messages=messages
128
  )
 
 
 
 
 
 
 
129
 
130
  return response.choices[0].message.content
131
  except Exception as e:
@@ -136,6 +155,14 @@ def analyze_and_generate_recipe(uploaded_image, available_equipment=None, langua
136
  def refine_recipe(original_recipe, user_refinement, language='English'):
137
  """Refine the recipe based on user input"""
138
  try:
 
 
 
 
 
 
 
 
139
  response = client.chat.completions.create(
140
  model="gpt-4o-mini",
141
  messages=[
@@ -156,6 +183,13 @@ def refine_recipe(original_recipe, user_refinement, language='English'):
156
  }
157
  ]
158
  )
 
 
 
 
 
 
 
159
 
160
  return response.choices[0].message.content
161
  except Exception as e:
 
2
  import streamlit as st
3
  from openai import OpenAI
4
  import os
5
+ from utils import pprint
6
 
7
  # Load environment variables
8
  from dotenv import load_dotenv
 
57
  """, unsafe_allow_html=True)
58
 
59
  # Initialize session state
60
+ if "ipAddress" not in st.session_state:
61
+ st.session_state.ipAddress = st.context.headers.get("x-forwarded-for")
62
+
63
  if 'cooking_equipment' not in st.session_state:
64
  st.session_state.cooking_equipment = {
65
  'Stove': True,
 
124
  ]
125
  }
126
  ]
127
+
128
+ # Log API call details
129
+ pprint({
130
+ "function": "analyze_and_generate_recipe",
131
+ "model": "gpt-4o-mini",
132
+ "language": language,
133
+ "available_equipment": available_equipment
134
+ })
135
 
136
  # Make the LLM call
137
  response = client.chat.completions.create(
138
  model="gpt-4o-mini",
139
  messages=messages
140
  )
141
+
142
+ # Log response details
143
+ pprint({
144
+ "function": "analyze_and_generate_recipe_response",
145
+ "tokens_used": response.usage.total_tokens if response.usage else None,
146
+ "response_length": len(response.choices[0].message.content)
147
+ })
148
 
149
  return response.choices[0].message.content
150
  except Exception as e:
 
155
  def refine_recipe(original_recipe, user_refinement, language='English'):
156
  """Refine the recipe based on user input"""
157
  try:
158
+ # Log API call details
159
+ pprint({
160
+ "function": "refine_recipe",
161
+ "model": "gpt-4o-mini",
162
+ "language": language,
163
+ "user_refinement_length": len(user_refinement)
164
+ })
165
+
166
  response = client.chat.completions.create(
167
  model="gpt-4o-mini",
168
  messages=[
 
183
  }
184
  ]
185
  )
186
+
187
+ # Log response details
188
+ pprint({
189
+ "function": "refine_recipe_response",
190
+ "tokens_used": response.usage.total_tokens if response.usage else None,
191
+ "response_length": len(response.choices[0].message.content)
192
+ })
193
 
194
  return response.choices[0].message.content
195
  except Exception as e:
utils.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime as DT
2
+ import pytz
3
+ import streamlit as st
4
+
5
+
6
+ def __nowInIST() -> DT.datetime:
7
+ return DT.datetime.now(pytz.timezone("Asia/Kolkata"))
8
+
9
+
10
+ def pprint(log: str):
11
+ now = __nowInIST()
12
+ now = now.strftime("%Y-%m-%d %H:%M:%S")
13
+ print(f"[{now}] [{st.session_state.ipAddress}] {log}")