shukdevdatta123 commited on
Commit
a2fd17f
·
verified ·
1 Parent(s): dbcdb5d

Delete abc2.txt

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