Update app.py
Browse files
app.py
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:
|
@@ -102,7 +137,7 @@ def transcribe_audio(audio, openai_api_key):
|
|
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 +147,20 @@ 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 |
-
#
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
history.append((f"User: {input_text}", f"Assistant: {response}"))
|
121 |
else:
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
return "", None, None, None, new_pdf_content, history
|
125 |
|
@@ -136,13 +177,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 +223,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 +232,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 +342,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 +357,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 +366,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
|
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 +391,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 +426,23 @@ def create_interface():
|
|
382 |
file_types=[".pdf"],
|
383 |
visible=False
|
384 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
|
386 |
with gr.Row():
|
387 |
reasoning_effort = gr.Dropdown(
|
@@ -403,7 +464,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
|
@@ -413,10 +474,20 @@ def create_interface():
|
|
413 |
outputs=[pdf_content]
|
414 |
)
|
415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
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 |
+
prompt = f"""Based on the following document content, generate {num_questions} multiple-choice quiz questions.
|
33 |
+
For each question:
|
34 |
+
1. Create a clear question based on key concepts in the document
|
35 |
+
2. Provide 4 possible answers (A, B, C, D)
|
36 |
+
3. Indicate the correct answer
|
37 |
+
4. Briefly explain why the answer is correct
|
38 |
+
|
39 |
+
Format the output clearly with each question numbered and separated.
|
40 |
+
|
41 |
+
Document content:
|
42 |
+
{pdf_content[:8000]} # Limiting content to avoid token limits
|
43 |
+
"""
|
44 |
+
|
45 |
+
try:
|
46 |
+
messages = [
|
47 |
+
{"role": "user", "content": [{"type": "text", "text": prompt}]}
|
48 |
+
]
|
49 |
+
|
50 |
+
response = openai.ChatCompletion.create(
|
51 |
+
model=model_choice,
|
52 |
+
messages=messages,
|
53 |
+
max_completion_tokens=2000
|
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:
|
|
|
137 |
return f"Error transcribing audio: {str(e)}"
|
138 |
|
139 |
# The function that will be used by Gradio interface
|
140 |
+
def chatbot(input_text, image, audio, pdf_file, openai_api_key, reasoning_effort, model_choice, pdf_content, num_quiz_questions, pdf_quiz_mode, history=[]):
|
141 |
# If there's audio, transcribe it to text
|
142 |
if audio:
|
143 |
input_text = transcribe_audio(audio, openai_api_key)
|
|
|
147 |
if pdf_file is not None:
|
148 |
new_pdf_content = extract_text_from_pdf(pdf_file)
|
149 |
|
150 |
+
# Check if we're in PDF quiz mode
|
151 |
+
if pdf_quiz_mode and new_pdf_content:
|
152 |
+
# Generate MCQ quiz questions
|
153 |
+
response = generate_mcq_quiz(new_pdf_content, num_quiz_questions, openai_api_key, model_choice)
|
154 |
+
history.append((f"User: [Uploaded PDF for Quiz - {num_quiz_questions} questions]", f"Assistant: {response}"))
|
|
|
155 |
else:
|
156 |
+
# Regular chat mode - generate the response
|
157 |
+
response = generate_response(input_text, image, new_pdf_content, openai_api_key, reasoning_effort, model_choice)
|
158 |
+
|
159 |
+
# Append the response to the history
|
160 |
+
if input_text:
|
161 |
+
history.append((f"User: {input_text}", f"Assistant: {response}"))
|
162 |
+
else:
|
163 |
+
history.append((f"User: [Uploaded content]", f"Assistant: {response}"))
|
164 |
|
165 |
return "", None, None, None, new_pdf_content, history
|
166 |
|
|
|
177 |
# Function to update visible components based on input type selection
|
178 |
def update_input_type(choice):
|
179 |
if choice == "Text":
|
180 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
181 |
elif choice == "Image":
|
182 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
183 |
elif choice == "Voice":
|
184 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
185 |
elif choice == "PDF":
|
186 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
187 |
+
elif choice == "PDF(QUIZ)":
|
188 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
189 |
|
190 |
# Custom CSS styles with animations and button colors
|
191 |
custom_css = """
|
|
|
223 |
animation: fadeIn 2s ease-out;
|
224 |
}
|
225 |
/* Input field styles */
|
226 |
+
.gradio-textbox, .gradio-dropdown, .gradio-image, .gradio-audio, .gradio-file, .gradio-slider {
|
227 |
border-radius: 8px;
|
228 |
border: 2px solid #ccc;
|
229 |
padding: 10px;
|
|
|
232 |
font-size: 1rem;
|
233 |
transition: all 0.3s ease;
|
234 |
}
|
235 |
+
.gradio-textbox:focus, .gradio-dropdown:focus, .gradio-image:focus, .gradio-audio:focus, .gradio-file:focus, .gradio-slider:focus {
|
236 |
border-color: #007bff;
|
237 |
}
|
238 |
/* Button styles */
|
|
|
342 |
.gradio-chatbot {
|
343 |
max-height: 400px;
|
344 |
}
|
345 |
+
.gradio-textbox, .gradio-dropdown, .gradio-image, .gradio-audio, .gradio-file, .gradio-slider {
|
346 |
width: 100%;
|
347 |
}
|
348 |
#submit-btn, #clear-history {
|
|
|
357 |
with gr.Blocks(css=custom_css) as demo:
|
358 |
gr.Markdown("""
|
359 |
<div class="gradio-header">
|
360 |
+
<h1>Multimodal Chatbot (Text + Image + Voice + PDF + Quiz)</h1>
|
361 |
<h3>Interact with a chatbot using text, image, voice, or PDF inputs</h3>
|
362 |
</div>
|
363 |
""")
|
|
|
366 |
with gr.Accordion("Click to expand for details", open=False):
|
367 |
gr.Markdown("""
|
368 |
### Description:
|
369 |
+
This is a multimodal chatbot that can handle text, image, voice, PDF inputs, and generate quizzes from PDFs.
|
370 |
- You can ask questions or provide text, and the assistant will respond.
|
371 |
- You can upload an image, and the assistant will process it and answer questions about the image.
|
372 |
- Voice input is supported: You can upload or record an audio file, and it will be transcribed to text and sent to the assistant.
|
373 |
- PDF support: Upload a PDF and ask questions about its content.
|
374 |
+
- PDF Quiz: Upload a PDF and specify how many MCQ questions you want generated based on the content.
|
375 |
- Enter your OpenAI API key to start interacting with the model.
|
376 |
- You can use the 'Clear History' button to remove the conversation history.
|
377 |
- "o1" is for image, voice, PDF and text chat and "o3-mini" is for text, PDF and voice chat only.
|
|
|
391 |
# Input type selector
|
392 |
with gr.Row():
|
393 |
input_type = gr.Radio(
|
394 |
+
["Text", "Image", "Voice", "PDF", "PDF(QUIZ)"],
|
395 |
label="Choose Input Type",
|
396 |
value="Text"
|
397 |
)
|
|
|
426 |
file_types=[".pdf"],
|
427 |
visible=False
|
428 |
)
|
429 |
+
|
430 |
+
# Quiz specific components
|
431 |
+
quiz_questions_slider = gr.Slider(
|
432 |
+
minimum=1,
|
433 |
+
maximum=20,
|
434 |
+
value=5,
|
435 |
+
step=1,
|
436 |
+
label="Number of Quiz Questions",
|
437 |
+
visible=False
|
438 |
+
)
|
439 |
+
|
440 |
+
# Hidden state for quiz mode
|
441 |
+
quiz_mode = gr.Checkbox(
|
442 |
+
label="Quiz Mode",
|
443 |
+
visible=False,
|
444 |
+
value=False
|
445 |
+
)
|
446 |
|
447 |
with gr.Row():
|
448 |
reasoning_effort = gr.Dropdown(
|
|
|
464 |
input_type.change(
|
465 |
fn=update_input_type,
|
466 |
inputs=[input_type],
|
467 |
+
outputs=[input_text, image_input, audio_input, pdf_input, quiz_questions_slider, quiz_mode]
|
468 |
)
|
469 |
|
470 |
# Process PDF when uploaded
|
|
|
474 |
outputs=[pdf_content]
|
475 |
)
|
476 |
|
477 |
+
# Update quiz mode when PDF(QUIZ) is selected
|
478 |
+
def update_quiz_mode(choice):
|
479 |
+
return True if choice == "PDF(QUIZ)" else False
|
480 |
+
|
481 |
+
input_type.change(
|
482 |
+
fn=update_quiz_mode,
|
483 |
+
inputs=[input_type],
|
484 |
+
outputs=[quiz_mode]
|
485 |
+
)
|
486 |
+
|
487 |
# Button interactions
|
488 |
submit_btn.click(
|
489 |
fn=chatbot,
|
490 |
+
inputs=[input_text, image_input, audio_input, pdf_input, openai_api_key, reasoning_effort, model_choice, pdf_content, quiz_questions_slider, quiz_mode],
|
491 |
outputs=[input_text, image_input, audio_input, pdf_input, pdf_content, chat_history]
|
492 |
)
|
493 |
|