shukdevdatta123 commited on
Commit
f847fb0
·
verified ·
1 Parent(s): 1b21a19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -16,29 +16,28 @@ def generate_response(input_text, image, openai_api_key, reasoning_effort="mediu
16
 
17
  openai.api_key = openai_api_key
18
 
19
- # Process the input depending on whether it's text or an image
20
  if image:
21
  # Convert the image to base64 string
22
  image_info = get_base64_string_from_image(image)
23
  input_text = f"data:image/png;base64,{image_info}"
24
 
25
- # Prepare the messages for OpenAI API
 
 
 
 
26
  if model_choice == "o1" and input_text:
27
- messages = [
28
- {"role": "user", "content": [{"type": "image_url", "image_url": {"url": input_text}}]}
29
- ]
30
  elif model_choice == "o3-mini" and input_text:
31
- messages = [
32
- {"role": "user", "content": [{"type": "text", "text": input_text}]}
33
- ]
34
-
35
  try:
36
  # Call OpenAI API with the selected model
37
  response = openai.ChatCompletion.create(
38
  model=model_choice, # Dynamically choose the model (o1 or o3-mini)
39
  messages=messages,
40
- reasoning_effort=reasoning_effort, # Set reasoning_effort for the response
41
- max_completion_tokens=2000 # Limit response tokens to 2000
42
  )
43
 
44
  return response["choices"][0]["message"]["content"]
@@ -277,4 +276,4 @@ def create_interface():
277
 
278
  if __name__ == "__main__":
279
  demo = create_interface() # Gradio multimodal chatbot
280
- demo.launch()
 
16
 
17
  openai.api_key = openai_api_key
18
 
19
+ # If the user uploaded an image, convert it to base64 and use it for API call
20
  if image:
21
  # Convert the image to base64 string
22
  image_info = get_base64_string_from_image(image)
23
  input_text = f"data:image/png;base64,{image_info}"
24
 
25
+ # Check for text input and pass to API
26
+ if not input_text:
27
+ return "Error: Please provide either text, image, or voice input."
28
+
29
+ # Prepare the messages for OpenAI API based on the selected model
30
  if model_choice == "o1" and input_text:
31
+ messages = [{"role": "user", "content": input_text}]
 
 
32
  elif model_choice == "o3-mini" and input_text:
33
+ messages = [{"role": "user", "content": input_text}]
34
+
 
 
35
  try:
36
  # Call OpenAI API with the selected model
37
  response = openai.ChatCompletion.create(
38
  model=model_choice, # Dynamically choose the model (o1 or o3-mini)
39
  messages=messages,
40
+ max_tokens=2000 # Limit response tokens to 2000
 
41
  )
42
 
43
  return response["choices"][0]["message"]["content"]
 
276
 
277
  if __name__ == "__main__":
278
  demo = create_interface() # Gradio multimodal chatbot
279
+ demo.launch()