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

Delete abc3.txt

Browse files
Files changed (1) hide show
  1. abc3.txt +0 -523
abc3.txt DELETED
@@ -1,523 +0,0 @@
1
- import gradio as gr
2
- import openai
3
- import base64
4
- from PIL import Image
5
- import io
6
- import os
7
- import tempfile
8
- import fitz # PyMuPDF for PDF handling
9
-
10
- # Function to extract text from PDF files
11
- def extract_text_from_pdf(pdf_file):
12
- try:
13
- text = ""
14
- pdf_document = fitz.open(pdf_file)
15
-
16
- for page_num in range(len(pdf_document)):
17
- page = pdf_document[page_num]
18
- text += page.get_text()
19
-
20
- pdf_document.close()
21
- return text
22
- except Exception as e:
23
- return f"Error extracting text from PDF: {str(e)}"
24
-
25
- # Function to generate MCQ quiz from PDF content
26
- def generate_mcq_quiz(pdf_content, num_questions, openai_api_key, model_choice):
27
- if not openai_api_key:
28
- return "Error: No API key provided."
29
-
30
- openai.api_key = openai_api_key
31
-
32
- # Limit content length to avoid token limits
33
- limited_content = pdf_content[:8000] if len(pdf_content) > 8000 else pdf_content
34
-
35
- prompt = f"""Based on the following document content, generate {num_questions} multiple-choice quiz questions.
36
- For each question:
37
- 1. Create a clear question based on key concepts in the document
38
- 2. Provide 4 possible answers (A, B, C, D)
39
- 3. Indicate the correct answer
40
- 4. Briefly explain why the answer is correct
41
- Format the output clearly with each question numbered and separated.
42
- Document content:
43
- {limited_content}
44
- """
45
-
46
- try:
47
- messages = [
48
- {"role": "user", "content": prompt}
49
- ]
50
-
51
- response = openai.ChatCompletion.create(
52
- model=model_choice,
53
- messages=messages
54
- )
55
-
56
- return response.choices[0].message.content
57
- except Exception as e:
58
- return f"Error generating quiz: {str(e)}"
59
-
60
- # Function to send the request to OpenAI API with an image, text or PDF input
61
- def generate_response(input_text, image, pdf_content, openai_api_key, reasoning_effort="medium", model_choice="o1"):
62
- if not openai_api_key:
63
- return "Error: No API key provided."
64
-
65
- openai.api_key = openai_api_key
66
-
67
- # Process the input depending on whether it's text, image, or a PDF-related query
68
- if pdf_content and input_text:
69
- # For PDF queries, we combine the PDF content with the user's question
70
- prompt = f"Based on the following document content, please answer this question: '{input_text}'\n\nDocument content:\n{pdf_content}"
71
- input_content = prompt
72
- elif image:
73
- # Convert the image to base64 string
74
- image_info = get_base64_string_from_image(image)
75
- input_content = f"data:image/png;base64,{image_info}"
76
- else:
77
- # Plain text input
78
- input_content = input_text
79
-
80
- # Prepare the messages for OpenAI API
81
- if model_choice == "o1":
82
- if image and not pdf_content:
83
- messages = [
84
- {"role": "user", "content": [{"type": "image_url", "image_url": {"url": input_content}}]}
85
- ]
86
- else:
87
- messages = [
88
- {"role": "user", "content": input_content}
89
- ]
90
- elif model_choice == "o3-mini":
91
- messages = [
92
- {"role": "user", "content": input_content}
93
- ]
94
-
95
- try:
96
- # Call OpenAI API with the selected model
97
- response = openai.ChatCompletion.create(
98
- model=model_choice,
99
- messages=messages,
100
- max_completion_tokens=2000
101
- )
102
-
103
- return response.choices[0].message.content
104
- except Exception as e:
105
- return f"Error calling OpenAI API: {str(e)}"
106
-
107
- # Function to convert an uploaded image to a base64 string
108
- def get_base64_string_from_image(pil_image):
109
- # Convert PIL Image to bytes
110
- buffered = io.BytesIO()
111
- pil_image.save(buffered, format="PNG")
112
- img_bytes = buffered.getvalue()
113
- base64_str = base64.b64encode(img_bytes).decode("utf-8")
114
- return base64_str
115
-
116
- # Function to transcribe audio to text using OpenAI Whisper API
117
- def transcribe_audio(audio, openai_api_key):
118
- if not openai_api_key:
119
- return "Error: No API key provided."
120
-
121
- openai.api_key = openai_api_key
122
-
123
- try:
124
- # Open the audio file and pass it as a file object
125
- with open(audio, 'rb') as audio_file:
126
- audio_file_content = audio_file.read()
127
-
128
- # Use the correct transcription API call
129
- audio_file_obj = io.BytesIO(audio_file_content)
130
- audio_file_obj.name = 'audio.wav' # Set a name for the file object (as OpenAI expects it)
131
-
132
- # Transcribe the audio to text using OpenAI's whisper model
133
- audio_file_transcription = openai.Audio.transcribe(file=audio_file_obj, model="whisper-1")
134
- return audio_file_transcription.text
135
- except Exception as e:
136
- return f"Error transcribing audio: {str(e)}"
137
-
138
- # The function that will be used by Gradio interface
139
- def chatbot(input_text, image, audio, pdf_file, openai_api_key, reasoning_effort, model_choice, pdf_content, num_quiz_questions, pdf_quiz_mode, history):
140
- if history is None:
141
- history = []
142
-
143
- # If there's audio, transcribe it to text
144
- if audio:
145
- input_text = transcribe_audio(audio, openai_api_key)
146
-
147
- # If a new PDF is uploaded, extract its text
148
- new_pdf_content = pdf_content
149
- if pdf_file is not None:
150
- new_pdf_content = extract_text_from_pdf(pdf_file)
151
-
152
- # Check if we're in PDF quiz mode
153
- if pdf_quiz_mode:
154
- if new_pdf_content:
155
- # Generate MCQ quiz questions
156
- quiz_response = generate_mcq_quiz(new_pdf_content, int(num_quiz_questions), openai_api_key, model_choice)
157
- history.append((f"User: [Uploaded PDF for Quiz - {int(num_quiz_questions)} questions]", f"Assistant: {quiz_response}"))
158
- else:
159
- history.append(("User: [Attempted to generate quiz without PDF]", "Assistant: Please upload a PDF file to generate quiz questions."))
160
- else:
161
- # Regular chat mode - generate the response
162
- response = generate_response(input_text, image, new_pdf_content, openai_api_key, reasoning_effort, model_choice)
163
-
164
- # Append the response to the history
165
- if input_text:
166
- history.append((f"User: {input_text}", f"Assistant: {response}"))
167
- elif image is not None:
168
- history.append((f"User: [Uploaded image]", f"Assistant: {response}"))
169
- elif pdf_file is not None:
170
- history.append((f"User: [Uploaded PDF]", f"Assistant: {response}"))
171
- else:
172
- history.append((f"User: [No input provided]", f"Assistant: Please provide some input (text, image, or PDF) for me to respond to."))
173
-
174
- return "", None, None, None, new_pdf_content, history
175
-
176
- # Function to clear the chat history and PDF content
177
- def clear_history():
178
- return "", None, None, None, "", []
179
-
180
- # Function to process a newly uploaded PDF
181
- def process_pdf(pdf_file):
182
- if pdf_file is None:
183
- return ""
184
- return extract_text_from_pdf(pdf_file)
185
-
186
- # Function to update visible components based on input type selection
187
- def update_input_type(choice):
188
- if choice == "Text":
189
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(value=False)
190
- elif choice == "Image":
191
- return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(value=False)
192
- elif choice == "Voice":
193
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(value=False)
194
- elif choice == "PDF":
195
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(value=False)
196
- elif choice == "PDF(QUIZ)":
197
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(value=True)
198
-
199
- # Custom CSS styles with animations and button colors
200
- custom_css = """
201
- /* General body styles */
202
- .gradio-container {
203
- font-family: 'Arial', sans-serif;
204
- background-color: #f8f9fa;
205
- color: #333;
206
- }
207
- /* Header styles */
208
- .gradio-header {
209
- background-color: #007bff;
210
- color: white;
211
- padding: 20px;
212
- text-align: center;
213
- border-radius: 8px;
214
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
215
- animation: fadeIn 1s ease-out;
216
- }
217
- .gradio-header h1 {
218
- font-size: 2.5rem;
219
- }
220
- .gradio-header h3 {
221
- font-size: 1.2rem;
222
- margin-top: 10px;
223
- }
224
- /* Chatbot container styles */
225
- .gradio-chatbot {
226
- background-color: #fff;
227
- border-radius: 10px;
228
- padding: 20px;
229
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
230
- max-height: 500px;
231
- overflow-y: auto;
232
- animation: fadeIn 2s ease-out;
233
- }
234
- /* Input field styles */
235
- .gradio-textbox, .gradio-dropdown, .gradio-image, .gradio-audio, .gradio-file, .gradio-slider {
236
- border-radius: 8px;
237
- border: 2px solid #ccc;
238
- padding: 10px;
239
- margin-bottom: 10px;
240
- width: 100%;
241
- font-size: 1rem;
242
- transition: all 0.3s ease;
243
- }
244
- .gradio-textbox:focus, .gradio-dropdown:focus, .gradio-image:focus, .gradio-audio:focus, .gradio-file:focus, .gradio-slider:focus {
245
- border-color: #007bff;
246
- }
247
- /* Button styles */
248
- /* Send Button: Sky Blue */
249
- #submit-btn {
250
- background-color: #00aaff; /* Sky blue */
251
- color: white;
252
- border: none;
253
- border-radius: 8px;
254
- padding: 10px 19px;
255
- font-size: 1.1rem;
256
- cursor: pointer;
257
- transition: all 0.3s ease;
258
- margin-left: auto;
259
- margin-right: auto;
260
- display: block;
261
- margin-top: 10px;
262
- }
263
- #submit-btn:hover {
264
- background-color: #0099cc; /* Slightly darker blue */
265
- }
266
- #submit-btn:active {
267
- transform: scale(0.95);
268
- }
269
- #clear-history {
270
- background-color: #f04e4e; /* Slightly Darker red */
271
- color: white;
272
- border: none;
273
- border-radius: 8px;
274
- padding: 10px 13px;
275
- font-size: 1.1rem;
276
- cursor: pointer;
277
- transition: all 0.3s ease;
278
- margin-top: 10px;
279
- }
280
- #clear-history:hover {
281
- background-color: #f5a4a4; /* Light red */
282
- }
283
- #clear-history:active {
284
- transform: scale(0.95);
285
- }
286
- /* Input type selector buttons */
287
- #input-type-group {
288
- display: flex;
289
- justify-content: center;
290
- gap: 10px;
291
- margin-bottom: 20px;
292
- }
293
- .input-type-btn {
294
- background-color: #6c757d;
295
- color: white;
296
- border: none;
297
- border-radius: 8px;
298
- padding: 10px 15px;
299
- font-size: 1rem;
300
- cursor: pointer;
301
- transition: all 0.3s ease;
302
- }
303
- .input-type-btn.selected {
304
- background-color: #007bff;
305
- }
306
- .input-type-btn:hover {
307
- background-color: #5a6268;
308
- }
309
- /* Chat history styles */
310
- .gradio-chatbot .message {
311
- margin-bottom: 10px;
312
- }
313
- .gradio-chatbot .user {
314
- background-color: #007bff;
315
- color: white;
316
- padding: 10px;
317
- border-radius: 12px;
318
- max-width: 70%;
319
- animation: slideInUser 0.5s ease-out;
320
- }
321
- .gradio-chatbot .assistant {
322
- background-color: #f1f1f1;
323
- color: #333;
324
- padding: 10px;
325
- border-radius: 12px;
326
- max-width: 70%;
327
- margin-left: auto;
328
- animation: slideInAssistant 0.5s ease-out;
329
- }
330
- /* Animation keyframes */
331
- @keyframes fadeIn {
332
- 0% { opacity: 0; }
333
- 100% { opacity: 1; }
334
- }
335
- @keyframes slideInUser {
336
- 0% { transform: translateX(-100%); }
337
- 100% { transform: translateX(0); }
338
- }
339
- @keyframes slideInAssistant {
340
- 0% { transform: translateX(100%); }
341
- 100% { transform: translateX(0); }
342
- }
343
- /* Mobile responsiveness */
344
- @media (max-width: 768px) {
345
- .gradio-header h1 {
346
- font-size: 1.8rem;
347
- }
348
- .gradio-header h3 {
349
- font-size: 1rem;
350
- }
351
- .gradio-chatbot {
352
- max-height: 400px;
353
- }
354
- .gradio-textbox, .gradio-dropdown, .gradio-image, .gradio-audio, .gradio-file, .gradio-slider {
355
- width: 100%;
356
- }
357
- #submit-btn, #clear-history {
358
- width: 100%;
359
- margin-left: 0;
360
- }
361
- }
362
- """
363
-
364
- # Gradio interface setup
365
- def create_interface():
366
- with gr.Blocks(css=custom_css) as demo:
367
- gr.Markdown("""
368
- <div class="gradio-header">
369
- <h1>Multimodal Chatbot (Text + Image + Voice + PDF + Quiz)</h1>
370
- <h3>Interact with a chatbot using text, image, voice, or PDF inputs</h3>
371
- </div>
372
- """)
373
-
374
- # Add a description with an expandable accordion
375
- with gr.Accordion("Click to expand for details", open=False):
376
- gr.Markdown("""
377
- ### Description:
378
- This is a multimodal chatbot that can handle text, image, voice, PDF inputs, and generate quizzes from PDFs.
379
- - You can ask questions or provide text, and the assistant will respond.
380
- - You can upload an image, and the assistant will process it and answer questions about the image.
381
- - Voice input is supported: You can upload or record an audio file, and it will be transcribed to text and sent to the assistant.
382
- - PDF support: Upload a PDF and ask questions about its content.
383
- - PDF Quiz: Upload a PDF and specify how many MCQ questions you want generated based on the content.
384
- - Enter your OpenAI API key to start interacting with the model.
385
- - You can use the 'Clear History' button to remove the conversation history.
386
- - "o1" is for image, voice, PDF and text chat and "o3-mini" is for text, PDF and voice chat only.
387
- ### Reasoning Effort:
388
- The reasoning effort controls how complex or detailed the assistant's answers should be.
389
- - **Low**: Provides quick, concise answers with minimal reasoning or details.
390
- - **Medium**: Offers a balanced response with a reasonable level of detail and thought.
391
- - **High**: Produces more detailed, analytical, or thoughtful responses, requiring deeper reasoning.
392
- """)
393
-
394
- # Store PDF content as a state variable
395
- pdf_content = gr.State("")
396
-
397
- with gr.Row():
398
- openai_api_key = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="sk-...", interactive=True)
399
-
400
- # Input type selector
401
- with gr.Row():
402
- input_type = gr.Radio(
403
- ["Text", "Image", "Voice", "PDF", "PDF(QUIZ)"],
404
- label="Choose Input Type",
405
- value="Text"
406
- )
407
-
408
- # Create the input components (initially text is visible, others are hidden)
409
- with gr.Row():
410
- # Text input
411
- input_text = gr.Textbox(
412
- label="Enter Text Question",
413
- placeholder="Ask a question or provide text",
414
- lines=2,
415
- visible=True
416
- )
417
-
418
- # Image input
419
- image_input = gr.Image(
420
- label="Upload an Image",
421
- type="pil",
422
- visible=False
423
- )
424
-
425
- # Audio input
426
- audio_input = gr.Audio(
427
- label="Upload or Record Audio",
428
- type="filepath",
429
- visible=False
430
- )
431
-
432
- # PDF input
433
- pdf_input = gr.File(
434
- label="Upload your PDF",
435
- file_types=[".pdf"],
436
- visible=False
437
- )
438
-
439
- # Quiz specific components
440
- quiz_questions_slider = gr.Slider(
441
- minimum=1,
442
- maximum=20,
443
- value=5,
444
- step=1,
445
- label="Number of Quiz Questions",
446
- visible=False
447
- )
448
-
449
- # Hidden state for quiz mode
450
- quiz_mode = gr.Checkbox(
451
- label="Quiz Mode",
452
- visible=False,
453
- value=False
454
- )
455
-
456
- with gr.Row():
457
- reasoning_effort = gr.Dropdown(
458
- label="Reasoning Effort",
459
- choices=["low", "medium", "high"],
460
- value="medium"
461
- )
462
- model_choice = gr.Dropdown(
463
- label="Select Model",
464
- choices=["o1", "o3-mini"],
465
- value="o1" # Default to 'o1' for image-related tasks
466
- )
467
- submit_btn = gr.Button("Ask!", elem_id="submit-btn")
468
- clear_btn = gr.Button("Clear History", elem_id="clear-history")
469
-
470
- chat_history = gr.Chatbot()
471
-
472
- # Connect the input type selector to the update function
473
- input_type.change(
474
- fn=update_input_type,
475
- inputs=[input_type],
476
- outputs=[input_text, image_input, audio_input, pdf_input, quiz_questions_slider, quiz_mode]
477
- )
478
-
479
- # Process PDF when uploaded
480
- pdf_input.change(
481
- fn=process_pdf,
482
- inputs=[pdf_input],
483
- outputs=[pdf_content]
484
- )
485
-
486
- # Button interactions
487
- submit_btn.click(
488
- fn=chatbot,
489
- inputs=[
490
- input_text,
491
- image_input,
492
- audio_input,
493
- pdf_input,
494
- openai_api_key,
495
- reasoning_effort,
496
- model_choice,
497
- pdf_content,
498
- quiz_questions_slider,
499
- quiz_mode,
500
- chat_history
501
- ],
502
- outputs=[
503
- input_text,
504
- image_input,
505
- audio_input,
506
- pdf_input,
507
- pdf_content,
508
- chat_history
509
- ]
510
- )
511
-
512
- clear_btn.click(
513
- fn=clear_history,
514
- inputs=[],
515
- outputs=[input_text, image_input, audio_input, pdf_input, pdf_content, chat_history]
516
- )
517
-
518
- return demo
519
-
520
- # Run the interface
521
- if __name__ == "__main__":
522
- demo = create_interface()
523
- demo.launch()