File size: 5,518 Bytes
fcf024d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
### 1. **Imports and Dependencies:**
   - **Gradio:** Gradio is used to create the interactive web interface where users can interact with the chatbot.
   - **OpenAI:** The `openai` library is used to interact with OpenAI's language models (like GPT) to generate responses based on the provided input.
   - **Base64 and PIL:** `base64` is used to convert images into a text-based format for processing, and `PIL` (Python Imaging Library) is used for handling and manipulating image files.
   - **PyMuPDF (`fitz`):** This library is used for handling PDFs. It allows us to extract text from the uploaded PDF file.

### 2. **Functions:**

   - **`extract_text_from_pdf(pdf_file):`**
     - This function takes a PDF file and extracts all the text from it.
     - It opens the PDF, processes each page, and collects the text.
     - If there's an error (e.g., the PDF is corrupted), it catches the exception and returns an error message.

   - **`generate_mcq_quiz(pdf_content, num_questions, openai_api_key, model_choice):`**
     - This function generates a multiple-choice quiz from the extracted text of a PDF.
     - It sends a request to OpenAI's model (via the `openai` library) to generate quiz questions based on the content of the PDF.
     - The function constructs a prompt with the content, the number of questions, and asks OpenAI to generate questions in a multiple-choice format.
     - It limits the text sent to OpenAI to 8,000 characters to avoid sending too much data.
     - The response from OpenAI includes questions, possible answers, and explanations.

   - **`generate_image_response(input_text, image, openai_api_key, model_choice):`**
     - This function handles image input.
     - When the user uploads an image, the image is converted to base64 (a text format) and sent to OpenAI with any accompanying text (from the user).
     - OpenAI generates a response based on both the image and the text provided by the user. It could analyze the image and give a description or answer questions about it.

   - **`chatbot(input_text, image, pdf_file, openai_api_key, model_choice, pdf_content, num_quiz_questions, pdf_quiz_mode, history):`**
     - This is the main chatbot function. It checks which type of input (text, image, or PDF) is provided and performs the appropriate action.
     - If the input type is **PDF**, it will either extract the content and generate a quiz or display a message prompting the user to upload a PDF.
     - If the input type is **Image**, it will call the `generate_image_response` function to analyze the image and respond.
     - It also handles the conversation history (so that the chat doesn't lose context).

   - **`clear_history():`**
     - This function clears the conversation history, resetting the chat.
   
   - **`update_input_type(choice):`**
     - Based on the user's choice (Image or PDF Quiz), this function hides or shows certain UI elements.
     - For example, if the user chooses "PDF Quiz", it will show the PDF upload field and quiz settings but hide the image upload field.

### 3. **Gradio Interface:**

   - **Custom CSS (`custom_css`):**
     - This is custom styling that makes the chatbot look nice. It sets a background color, applies gradients to buttons, and customizes the chatbox and headers.
  
   - **Interface Setup (`create_interface`):**
     - The `gr.Blocks` function is used to create the layout of the interface.
     - Inside the `gr.Blocks()`, different elements like textboxes, images, buttons, and chat history are defined:
       - **Textbox for OpenAI API key:** The user needs to provide their API key to use OpenAI's models.
       - **Radio buttons for Input Type:** The user selects between "Image" or "PDF(QUIZ)".
       - **Textbox for user questions:** For image-based input, the user can type questions.
       - **Image input field:** Users can upload an image for analysis.
       - **PDF input field:** If the user selects "PDF(QUIZ)", they can upload a PDF to generate quiz questions.
       - **Quiz settings:** Includes a slider to choose the number of questions and a checkbox to confirm that the user wants a quiz.

   - **Buttons:**
     - **Submit button:** Sends the user's input to the chatbot function (either an image, PDF, or question).
     - **Clear button:** Clears the chat history and resets the input fields.

   - **Chat history (`gr.Chatbot()`):**
     - This displays the conversation history between the user and the chatbot, so the user can see the entire interaction.

### 4. **Logic for Handling User Inputs:**

   - When the user selects **"Image"** as the input type, the interface will display fields for the image and a question text box. The bot will generate a response based on the image and any question the user asks.
   - When the user selects **"PDF(QUIZ)"** as the input type, the interface will show a PDF upload field and allow the user to specify how many quiz questions they want to generate. The bot will extract text from the PDF and use OpenAI to create a quiz.

### 5. **Launching the Interface:**
   - The `demo.launch()` function runs the web interface, making the chatbot available to users in their browser.

### 6. **Overall Flow:**
   - The user chooses an input type (Image or PDF Quiz).
   - Depending on the choice, the relevant fields (text, image, PDF) appear.
   - The user submits their query or uploads their file, and the chatbot generates a response.
   - The conversation history is maintained for context and can be cleared by the user.