Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from PIL import Image
|
|
5 |
import io
|
6 |
|
7 |
# Function to send the request to OpenAI API with an image or text input
|
8 |
-
def generate_response(input_text, image, openai_api_key, reasoning_effort="medium"):
|
9 |
if not openai_api_key:
|
10 |
return "Error: No API key provided."
|
11 |
|
@@ -18,14 +18,19 @@ def generate_response(input_text, image, openai_api_key, reasoning_effort="mediu
|
|
18 |
input_text = f"data:image/png;base64,{image_info}"
|
19 |
|
20 |
# Prepare the messages for OpenAI API
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
try:
|
26 |
-
# Call OpenAI API with the
|
27 |
response = openai.ChatCompletion.create(
|
28 |
-
model=
|
29 |
messages=messages,
|
30 |
reasoning_effort=reasoning_effort, # Set reasoning_effort for the response
|
31 |
max_completion_tokens=2000 # Limit response tokens to 2000
|
@@ -45,8 +50,8 @@ def get_base64_string_from_image(pil_image):
|
|
45 |
return base64_str
|
46 |
|
47 |
# The function that will be used by Gradio interface
|
48 |
-
def chatbot(input_text, image, openai_api_key, reasoning_effort, history=[]):
|
49 |
-
response = generate_response(input_text, image, openai_api_key, reasoning_effort)
|
50 |
|
51 |
# Append the response to the history
|
52 |
history.append((f"User: {input_text}", f"Assistant: {response}"))
|
@@ -85,13 +90,19 @@ def create_interface():
|
|
85 |
choices=["low", "medium", "high"],
|
86 |
value="medium"
|
87 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
submit_btn = gr.Button("Send")
|
89 |
clear_btn = gr.Button("Clear History")
|
90 |
|
91 |
chat_history = gr.Chatbot()
|
92 |
|
93 |
# Button interactions
|
94 |
-
submit_btn.click(fn=chatbot, inputs=[input_text, image_input, openai_api_key, reasoning_effort, chat_history], outputs=[input_text, chat_history])
|
95 |
clear_btn.click(fn=clear_history, inputs=[], outputs=[chat_history, chat_history])
|
96 |
|
97 |
return demo
|
|
|
5 |
import io
|
6 |
|
7 |
# Function to send the request to OpenAI API with an image or text input
|
8 |
+
def generate_response(input_text, image, openai_api_key, reasoning_effort="medium", model_choice="o1"):
|
9 |
if not openai_api_key:
|
10 |
return "Error: No API key provided."
|
11 |
|
|
|
18 |
input_text = f"data:image/png;base64,{image_info}"
|
19 |
|
20 |
# Prepare the messages for OpenAI API
|
21 |
+
if model_choice == "o1":
|
22 |
+
messages = [
|
23 |
+
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": input_text}}]}
|
24 |
+
]
|
25 |
+
elif model_choice == "o3-mini":
|
26 |
+
messages = [
|
27 |
+
{"role": "user", "content": [{"type": "text", "text": input_text}]}
|
28 |
+
]
|
29 |
+
|
30 |
try:
|
31 |
+
# Call OpenAI API with the selected model
|
32 |
response = openai.ChatCompletion.create(
|
33 |
+
model=model_choice, # Dynamically choose the model (o1 or o3-mini)
|
34 |
messages=messages,
|
35 |
reasoning_effort=reasoning_effort, # Set reasoning_effort for the response
|
36 |
max_completion_tokens=2000 # Limit response tokens to 2000
|
|
|
50 |
return base64_str
|
51 |
|
52 |
# The function that will be used by Gradio interface
|
53 |
+
def chatbot(input_text, image, openai_api_key, reasoning_effort, model_choice, history=[]):
|
54 |
+
response = generate_response(input_text, image, openai_api_key, reasoning_effort, model_choice)
|
55 |
|
56 |
# Append the response to the history
|
57 |
history.append((f"User: {input_text}", f"Assistant: {response}"))
|
|
|
90 |
choices=["low", "medium", "high"],
|
91 |
value="medium"
|
92 |
)
|
93 |
+
model_choice = gr.Dropdown(
|
94 |
+
label="Select Model",
|
95 |
+
choices=["o1", "o3-mini"],
|
96 |
+
value="o1", # Default to 'o1' for image-related tasks
|
97 |
+
description="Choose 'o1' for image-related questions and 'o3-mini' for text-related questions."
|
98 |
+
)
|
99 |
submit_btn = gr.Button("Send")
|
100 |
clear_btn = gr.Button("Clear History")
|
101 |
|
102 |
chat_history = gr.Chatbot()
|
103 |
|
104 |
# Button interactions
|
105 |
+
submit_btn.click(fn=chatbot, inputs=[input_text, image_input, openai_api_key, reasoning_effort, model_choice, chat_history], outputs=[input_text, chat_history])
|
106 |
clear_btn.click(fn=clear_history, inputs=[], outputs=[chat_history, chat_history])
|
107 |
|
108 |
return demo
|