noumanjavaid commited on
Commit
9ae8453
·
verified ·
1 Parent(s): 41e6c76

Update realtime.py

Browse files
Files changed (1) hide show
  1. realtime.py +27 -1
realtime.py CHANGED
@@ -111,6 +111,30 @@ pricing_data = {
111
  }
112
  }
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def make_api_call(text_input, model="gpt-4o-mini-audio-preview"):
115
  """Make actual API call to OpenAI"""
116
  try:
@@ -251,7 +275,9 @@ with tab2:
251
  if isinstance(response, str) and response.startswith("Error"):
252
  st.error(response)
253
  else:
254
- st.code(json.dumps(response, indent=2), language="json")
 
 
255
 
256
  # Calculate cost for this request
257
  text_costs = calculate_cost(demo_model, "Text", len(demo_text.split()) / 150)
 
111
  }
112
  }
113
 
114
+ def chat_completion_to_dict(response):
115
+ """Convert ChatCompletion object to a dictionary"""
116
+ if isinstance(response, str):
117
+ return {"error": response}
118
+
119
+ return {
120
+ "id": response.id,
121
+ "choices": [{
122
+ "index": choice.index,
123
+ "message": {
124
+ "role": choice.message.role,
125
+ "content": choice.message.content
126
+ },
127
+ "finish_reason": choice.finish_reason
128
+ } for choice in response.choices],
129
+ "created": response.created,
130
+ "model": response.model,
131
+ "usage": {
132
+ "prompt_tokens": response.usage.prompt_tokens,
133
+ "completion_tokens": response.usage.completion_tokens,
134
+ "total_tokens": response.usage.total_tokens
135
+ }
136
+ }
137
+
138
  def make_api_call(text_input, model="gpt-4o-mini-audio-preview"):
139
  """Make actual API call to OpenAI"""
140
  try:
 
275
  if isinstance(response, str) and response.startswith("Error"):
276
  st.error(response)
277
  else:
278
+ # Convert the response to a dictionary before JSON serialization
279
+ response_dict = chat_completion_to_dict(response)
280
+ st.code(json.dumps(response_dict, indent=2), language="json")
281
 
282
  # Calculate cost for this request
283
  text_costs = calculate_cost(demo_model, "Text", len(demo_text.split()) / 150)