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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import base64
|
4 |
+
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", model_choice="o1"):
|
9 |
+
if not openai_api_key:
|
10 |
+
return "Error: No API key provided."
|
11 |
+
|
12 |
+
openai.api_key = openai_api_key
|
13 |
+
|
14 |
+
# Process the input depending on whether it's text or an image
|
15 |
+
if image:
|
16 |
+
# Convert the image to base64 string
|
17 |
+
image_info = get_base64_string_from_image(image)
|
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
|
37 |
+
)
|
38 |
+
|
39 |
+
return response["choices"][0]["message"]["content"]
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error calling OpenAI API: {str(e)}"
|
42 |
+
|
43 |
+
# Function to convert an uploaded image to a base64 string
|
44 |
+
def get_base64_string_from_image(pil_image):
|
45 |
+
# Convert PIL Image to bytes
|
46 |
+
buffered = io.BytesIO()
|
47 |
+
pil_image.save(buffered, format="PNG")
|
48 |
+
img_bytes = buffered.getvalue()
|
49 |
+
base64_str = base64.b64encode(img_bytes).decode("utf-8")
|
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}"))
|
58 |
+
|
59 |
+
return "", history
|
60 |
+
|
61 |
+
# Function to clear the chat history
|
62 |
+
def clear_history():
|
63 |
+
return "", []
|
64 |
+
|
65 |
+
# Custom CSS styles with animations and button colors
|
66 |
+
custom_css = """
|
67 |
+
/* General body styles */
|
68 |
+
.gradio-container {
|
69 |
+
font-family: 'Arial', sans-serif;
|
70 |
+
background-color: #f8f9fa;
|
71 |
+
color: #333;
|
72 |
+
}
|
73 |
+
/* Header styles */
|
74 |
+
.gradio-header {
|
75 |
+
background-color: #007bff;
|
76 |
+
color: white;
|
77 |
+
padding: 20px;
|
78 |
+
text-align: center;
|
79 |
+
border-radius: 8px;
|
80 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
81 |
+
animation: fadeIn 1s ease-out;
|
82 |
+
}
|
83 |
+
.gradio-header h1 {
|
84 |
+
font-size: 2.5rem;
|
85 |
+
}
|
86 |
+
.gradio-header h3 {
|
87 |
+
font-size: 1.2rem;
|
88 |
+
margin-top: 10px;
|
89 |
+
}
|
90 |
+
/* Chatbot container styles */
|
91 |
+
.gradio-chatbot {
|
92 |
+
background-color: #fff;
|
93 |
+
border-radius: 10px;
|
94 |
+
padding: 20px;
|
95 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
96 |
+
max-height: 500px;
|
97 |
+
overflow-y: auto;
|
98 |
+
animation: fadeIn 2s ease-out;
|
99 |
+
}
|
100 |
+
/* Input field styles */
|
101 |
+
.gradio-textbox, .gradio-dropdown, .gradio-image {
|
102 |
+
border-radius: 8px;
|
103 |
+
border: 2px solid #ccc;
|
104 |
+
padding: 10px;
|
105 |
+
margin-bottom: 10px;
|
106 |
+
width: 100%;
|
107 |
+
font-size: 1rem;
|
108 |
+
transition: all 0.3s ease;
|
109 |
+
}
|
110 |
+
.gradio-textbox:focus, .gradio-dropdown:focus, .gradio-image:focus {
|
111 |
+
border-color: #007bff;
|
112 |
+
}
|
113 |
+
/* Button styles */
|
114 |
+
/* Send Button: Sky Blue */
|
115 |
+
#submit-btn {
|
116 |
+
background-color: #00aaff; /* Sky blue */
|
117 |
+
color: white;
|
118 |
+
border: none;
|
119 |
+
border-radius: 8px;
|
120 |
+
padding: 10px 19px;
|
121 |
+
font-size: 1.1rem;
|
122 |
+
cursor: pointer;
|
123 |
+
transition: all 0.3s ease;
|
124 |
+
margin-left: auto;
|
125 |
+
margin-right: auto;
|
126 |
+
display: block;
|
127 |
+
margin-top: 10px;
|
128 |
+
}
|
129 |
+
#submit-btn:hover {
|
130 |
+
background-color: #0099cc; /* Slightly darker blue */
|
131 |
+
}
|
132 |
+
#submit-btn:active {
|
133 |
+
transform: scale(0.95);
|
134 |
+
}
|
135 |
+
/* Clear History Button: Light Red */
|
136 |
+
#clear-history {
|
137 |
+
background-color: #f04e4e; /* Slightly Darker red */
|
138 |
+
color: white;
|
139 |
+
border: none;
|
140 |
+
border-radius: 8px;
|
141 |
+
padding: 10px 13px;
|
142 |
+
font-size: 1.1rem;
|
143 |
+
cursor: pointer;
|
144 |
+
transition: all 0.3s ease;
|
145 |
+
margin-top: 10px;
|
146 |
+
}
|
147 |
+
#clear-history:hover {
|
148 |
+
background-color: #f5a4a4; /* Light red */
|
149 |
+
}
|
150 |
+
#clear-history:active {
|
151 |
+
transform: scale(0.95);
|
152 |
+
}
|
153 |
+
/* Chat history styles */
|
154 |
+
.gradio-chatbot .message {
|
155 |
+
margin-bottom: 10px;
|
156 |
+
}
|
157 |
+
.gradio-chatbot .user {
|
158 |
+
background-color: #007bff;
|
159 |
+
color: white;
|
160 |
+
padding: 10px;
|
161 |
+
border-radius: 12px;
|
162 |
+
max-width: 70%;
|
163 |
+
animation: slideInUser 0.5s ease-out;
|
164 |
+
}
|
165 |
+
.gradio-chatbot .assistant {
|
166 |
+
background-color: #f1f1f1;
|
167 |
+
color: #333;
|
168 |
+
padding: 10px;
|
169 |
+
border-radius: 12px;
|
170 |
+
max-width: 70%;
|
171 |
+
margin-left: auto;
|
172 |
+
animation: slideInAssistant 0.5s ease-out;
|
173 |
+
}
|
174 |
+
/* Animation keyframes */
|
175 |
+
@keyframes fadeIn {
|
176 |
+
0% { opacity: 0; }
|
177 |
+
100% { opacity: 1; }
|
178 |
+
}
|
179 |
+
@keyframes slideInUser {
|
180 |
+
0% { transform: translateX(-100%); }
|
181 |
+
100% { transform: translateX(0); }
|
182 |
+
}
|
183 |
+
@keyframes slideInAssistant {
|
184 |
+
0% { transform: translateX(100%); }
|
185 |
+
100% { transform: translateX(0); }
|
186 |
+
}
|
187 |
+
/* Mobile responsiveness */
|
188 |
+
@media (max-width: 768px) {
|
189 |
+
.gradio-header h1 {
|
190 |
+
font-size: 1.8rem;
|
191 |
+
}
|
192 |
+
.gradio-header h3 {
|
193 |
+
font-size: 1rem;
|
194 |
+
}
|
195 |
+
.gradio-chatbot {
|
196 |
+
max-height: 400px;
|
197 |
+
}
|
198 |
+
.gradio-textbox, .gradio-dropdown, .gradio-image {
|
199 |
+
width: 100%;
|
200 |
+
}
|
201 |
+
#submit-btn, #clear-history {
|
202 |
+
width: 100%;
|
203 |
+
margin-left: 0;
|
204 |
+
}
|
205 |
+
}
|
206 |
+
"""
|
207 |
+
|
208 |
+
# Gradio interface setup
|
209 |
+
def create_interface():
|
210 |
+
with gr.Blocks(css=custom_css) as demo:
|
211 |
+
gr.Markdown("""
|
212 |
+
<div class="gradio-header">
|
213 |
+
<h1>Multimodal Chatbot (Text + Image)</h1>
|
214 |
+
<h3>Interact with a chatbot using text or image inputs</h3>
|
215 |
+
</div>
|
216 |
+
""")
|
217 |
+
|
218 |
+
# Add a description with an expandable accordion
|
219 |
+
with gr.Accordion("Click to expand for details", open=False):
|
220 |
+
gr.Markdown("""
|
221 |
+
### Description:
|
222 |
+
This is a multimodal chatbot that can handle both text and image inputs.
|
223 |
+
- You can ask questions or provide text, and the assistant will respond.
|
224 |
+
- You can also upload an image, and the assistant will process it and answer questions about the image.
|
225 |
+
- Enter your OpenAI API key to start interacting with the model.
|
226 |
+
- You can use the 'Clear History' button to remove the conversation history.
|
227 |
+
- "o1" is for image chat and "o3-mini" is for text chat.
|
228 |
+
### Reasoning Effort:
|
229 |
+
The reasoning effort controls how complex or detailed the assistant's answers should be.
|
230 |
+
- **Low**: Provides quick, concise answers with minimal reasoning or details.
|
231 |
+
- **Medium**: Offers a balanced response with a reasonable level of detail and thought.
|
232 |
+
- **High**: Produces more detailed, analytical, or thoughtful responses, requiring deeper reasoning.
|
233 |
+
""")
|
234 |
+
|
235 |
+
with gr.Row():
|
236 |
+
openai_api_key = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="sk-...", interactive=True)
|
237 |
+
|
238 |
+
with gr.Row():
|
239 |
+
image_input = gr.Image(label="Upload an Image", type="pil") # Image upload input
|
240 |
+
input_text = gr.Textbox(label="Enter Text Question", placeholder="Ask a question or provide text", lines=2)
|
241 |
+
|
242 |
+
with gr.Row():
|
243 |
+
reasoning_effort = gr.Dropdown(
|
244 |
+
label="Reasoning Effort",
|
245 |
+
choices=["low", "medium", "high"],
|
246 |
+
value="medium"
|
247 |
+
)
|
248 |
+
model_choice = gr.Dropdown(
|
249 |
+
label="Select Model",
|
250 |
+
choices=["o1", "o3-mini"],
|
251 |
+
value="o1" # Default to 'o1' for image-related tasks
|
252 |
+
)
|
253 |
+
submit_btn = gr.Button("Ask!", elem_id="submit-btn")
|
254 |
+
clear_btn = gr.Button("Clear History", elem_id="clear-history")
|
255 |
+
|
256 |
+
chat_history = gr.Chatbot()
|
257 |
+
|
258 |
+
# Button interactions
|
259 |
+
submit_btn.click(fn=chatbot, inputs=[input_text, image_input, openai_api_key, reasoning_effort, model_choice, chat_history], outputs=[input_text, chat_history])
|
260 |
+
clear_btn.click(fn=clear_history, inputs=[], outputs=[chat_history, chat_history])
|
261 |
+
|
262 |
+
return demo
|
263 |
+
|
264 |
+
# Run the interface
|
265 |
+
if __name__ == "__main__":
|
266 |
+
demo = create_interface()
|
267 |
+
demo.launch()
|