Update app.py
Browse files
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 |
-
#
|
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 |
-
#
|
|
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
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()
|