shukdevdatta123 commited on
Commit
bb3cead
·
verified ·
1 Parent(s): 380b344

Update abc3.txt

Browse files
Files changed (1) hide show
  1. abc3.txt +115 -26
abc3.txt CHANGED
@@ -22,6 +22,41 @@ def extract_text_from_pdf(pdf_file):
22
  except Exception as e:
23
  return f"Error extracting text from PDF: {str(e)}"
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  # Function to send the request to OpenAI API with an image, text or PDF input
26
  def generate_response(input_text, image, pdf_content, openai_api_key, reasoning_effort="medium", model_choice="o1"):
27
  if not openai_api_key:
@@ -50,11 +85,11 @@ def generate_response(input_text, image, pdf_content, openai_api_key, reasoning_
50
  ]
51
  else:
52
  messages = [
53
- {"role": "user", "content": [{"type": "text", "text": input_content}]}
54
  ]
55
  elif model_choice == "o3-mini":
56
  messages = [
57
- {"role": "user", "content": [{"type": "text", "text": input_content}]}
58
  ]
59
 
60
  try:
@@ -62,11 +97,10 @@ def generate_response(input_text, image, pdf_content, openai_api_key, reasoning_
62
  response = openai.ChatCompletion.create(
63
  model=model_choice,
64
  messages=messages,
65
- reasoning_effort=reasoning_effort,
66
  max_completion_tokens=2000
67
  )
68
 
69
- return response["choices"][0]["message"]["content"]
70
  except Exception as e:
71
  return f"Error calling OpenAI API: {str(e)}"
72
 
@@ -97,12 +131,15 @@ def transcribe_audio(audio, openai_api_key):
97
 
98
  # Transcribe the audio to text using OpenAI's whisper model
99
  audio_file_transcription = openai.Audio.transcribe(file=audio_file_obj, model="whisper-1")
100
- return audio_file_transcription['text']
101
  except Exception as e:
102
  return f"Error transcribing audio: {str(e)}"
103
 
104
  # The function that will be used by Gradio interface
105
- def chatbot(input_text, image, audio, pdf_file, openai_api_key, reasoning_effort, model_choice, pdf_content, history=[]):
 
 
 
106
  # If there's audio, transcribe it to text
107
  if audio:
108
  input_text = transcribe_audio(audio, openai_api_key)
@@ -112,14 +149,27 @@ def chatbot(input_text, image, audio, pdf_file, openai_api_key, reasoning_effort
112
  if pdf_file is not None:
113
  new_pdf_content = extract_text_from_pdf(pdf_file)
114
 
115
- # Generate the response
116
- response = generate_response(input_text, image, new_pdf_content, openai_api_key, reasoning_effort, model_choice)
117
-
118
- # Append the response to the history
119
- if input_text:
120
- history.append((f"User: {input_text}", f"Assistant: {response}"))
 
 
121
  else:
122
- history.append((f"User: [Uploaded content]", f"Assistant: {response}"))
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  return "", None, None, None, new_pdf_content, history
125
 
@@ -136,13 +186,15 @@ def process_pdf(pdf_file):
136
  # Function to update visible components based on input type selection
137
  def update_input_type(choice):
138
  if choice == "Text":
139
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
140
  elif choice == "Image":
141
- return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
142
  elif choice == "Voice":
143
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
144
  elif choice == "PDF":
145
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
 
 
146
 
147
  # Custom CSS styles with animations and button colors
148
  custom_css = """
@@ -180,7 +232,7 @@ custom_css = """
180
  animation: fadeIn 2s ease-out;
181
  }
182
  /* Input field styles */
183
- .gradio-textbox, .gradio-dropdown, .gradio-image, .gradio-audio, .gradio-file {
184
  border-radius: 8px;
185
  border: 2px solid #ccc;
186
  padding: 10px;
@@ -189,7 +241,7 @@ custom_css = """
189
  font-size: 1rem;
190
  transition: all 0.3s ease;
191
  }
192
- .gradio-textbox:focus, .gradio-dropdown:focus, .gradio-image:focus, .gradio-audio:focus, .gradio-file:focus {
193
  border-color: #007bff;
194
  }
195
  /* Button styles */
@@ -299,7 +351,7 @@ custom_css = """
299
  .gradio-chatbot {
300
  max-height: 400px;
301
  }
302
- .gradio-textbox, .gradio-dropdown, .gradio-image, .gradio-audio, .gradio-file {
303
  width: 100%;
304
  }
305
  #submit-btn, #clear-history {
@@ -314,7 +366,7 @@ def create_interface():
314
  with gr.Blocks(css=custom_css) as demo:
315
  gr.Markdown("""
316
  <div class="gradio-header">
317
- <h1>Multimodal Chatbot (Text + Image + Voice + PDF)</h1>
318
  <h3>Interact with a chatbot using text, image, voice, or PDF inputs</h3>
319
  </div>
320
  """)
@@ -323,11 +375,12 @@ def create_interface():
323
  with gr.Accordion("Click to expand for details", open=False):
324
  gr.Markdown("""
325
  ### Description:
326
- This is a multimodal chatbot that can handle text, image, voice, and PDF inputs.
327
  - You can ask questions or provide text, and the assistant will respond.
328
  - You can upload an image, and the assistant will process it and answer questions about the image.
329
  - Voice input is supported: You can upload or record an audio file, and it will be transcribed to text and sent to the assistant.
330
  - PDF support: Upload a PDF and ask questions about its content.
 
331
  - Enter your OpenAI API key to start interacting with the model.
332
  - You can use the 'Clear History' button to remove the conversation history.
333
  - "o1" is for image, voice, PDF and text chat and "o3-mini" is for text, PDF and voice chat only.
@@ -347,7 +400,7 @@ def create_interface():
347
  # Input type selector
348
  with gr.Row():
349
  input_type = gr.Radio(
350
- ["Text", "Image", "Voice", "PDF"],
351
  label="Choose Input Type",
352
  value="Text"
353
  )
@@ -382,6 +435,23 @@ def create_interface():
382
  file_types=[".pdf"],
383
  visible=False
384
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
  with gr.Row():
387
  reasoning_effort = gr.Dropdown(
@@ -403,7 +473,7 @@ def create_interface():
403
  input_type.change(
404
  fn=update_input_type,
405
  inputs=[input_type],
406
- outputs=[input_text, image_input, audio_input, pdf_input]
407
  )
408
 
409
  # Process PDF when uploaded
@@ -416,8 +486,27 @@ def create_interface():
416
  # Button interactions
417
  submit_btn.click(
418
  fn=chatbot,
419
- inputs=[input_text, image_input, audio_input, pdf_input, openai_api_key, reasoning_effort, model_choice, pdf_content],
420
- outputs=[input_text, image_input, audio_input, pdf_input, pdf_content, chat_history]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
  )
422
 
423
  clear_btn.click(
 
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:
 
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:
 
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
 
 
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)
 
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
 
 
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 = """
 
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;
 
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 */
 
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 {
 
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
  """)
 
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.
 
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
  )
 
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(
 
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
 
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(