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