shukdevdatta123 commited on
Commit
fcf024d
·
verified ·
1 Parent(s): 59ead9f

Create Documentation.ipynb

Browse files
Files changed (1) hide show
  1. Documentation.ipynb +73 -0
Documentation.ipynb ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. **Imports and Dependencies:**
2
+ - **Gradio:** Gradio is used to create the interactive web interface where users can interact with the chatbot.
3
+ - **OpenAI:** The `openai` library is used to interact with OpenAI's language models (like GPT) to generate responses based on the provided input.
4
+ - **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.
5
+ - **PyMuPDF (`fitz`):** This library is used for handling PDFs. It allows us to extract text from the uploaded PDF file.
6
+
7
+ ### 2. **Functions:**
8
+
9
+ - **`extract_text_from_pdf(pdf_file):`**
10
+ - This function takes a PDF file and extracts all the text from it.
11
+ - It opens the PDF, processes each page, and collects the text.
12
+ - If there's an error (e.g., the PDF is corrupted), it catches the exception and returns an error message.
13
+
14
+ - **`generate_mcq_quiz(pdf_content, num_questions, openai_api_key, model_choice):`**
15
+ - This function generates a multiple-choice quiz from the extracted text of a PDF.
16
+ - It sends a request to OpenAI's model (via the `openai` library) to generate quiz questions based on the content of the PDF.
17
+ - The function constructs a prompt with the content, the number of questions, and asks OpenAI to generate questions in a multiple-choice format.
18
+ - It limits the text sent to OpenAI to 8,000 characters to avoid sending too much data.
19
+ - The response from OpenAI includes questions, possible answers, and explanations.
20
+
21
+ - **`generate_image_response(input_text, image, openai_api_key, model_choice):`**
22
+ - This function handles image input.
23
+ - 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).
24
+ - 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.
25
+
26
+ - **`chatbot(input_text, image, pdf_file, openai_api_key, model_choice, pdf_content, num_quiz_questions, pdf_quiz_mode, history):`**
27
+ - This is the main chatbot function. It checks which type of input (text, image, or PDF) is provided and performs the appropriate action.
28
+ - 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.
29
+ - If the input type is **Image**, it will call the `generate_image_response` function to analyze the image and respond.
30
+ - It also handles the conversation history (so that the chat doesn't lose context).
31
+
32
+ - **`clear_history():`**
33
+ - This function clears the conversation history, resetting the chat.
34
+
35
+ - **`update_input_type(choice):`**
36
+ - Based on the user's choice (Image or PDF Quiz), this function hides or shows certain UI elements.
37
+ - For example, if the user chooses "PDF Quiz", it will show the PDF upload field and quiz settings but hide the image upload field.
38
+
39
+ ### 3. **Gradio Interface:**
40
+
41
+ - **Custom CSS (`custom_css`):**
42
+ - 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.
43
+
44
+ - **Interface Setup (`create_interface`):**
45
+ - The `gr.Blocks` function is used to create the layout of the interface.
46
+ - Inside the `gr.Blocks()`, different elements like textboxes, images, buttons, and chat history are defined:
47
+ - **Textbox for OpenAI API key:** The user needs to provide their API key to use OpenAI's models.
48
+ - **Radio buttons for Input Type:** The user selects between "Image" or "PDF(QUIZ)".
49
+ - **Textbox for user questions:** For image-based input, the user can type questions.
50
+ - **Image input field:** Users can upload an image for analysis.
51
+ - **PDF input field:** If the user selects "PDF(QUIZ)", they can upload a PDF to generate quiz questions.
52
+ - **Quiz settings:** Includes a slider to choose the number of questions and a checkbox to confirm that the user wants a quiz.
53
+
54
+ - **Buttons:**
55
+ - **Submit button:** Sends the user's input to the chatbot function (either an image, PDF, or question).
56
+ - **Clear button:** Clears the chat history and resets the input fields.
57
+
58
+ - **Chat history (`gr.Chatbot()`):**
59
+ - This displays the conversation history between the user and the chatbot, so the user can see the entire interaction.
60
+
61
+ ### 4. **Logic for Handling User Inputs:**
62
+
63
+ - 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.
64
+ - 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.
65
+
66
+ ### 5. **Launching the Interface:**
67
+ - The `demo.launch()` function runs the web interface, making the chatbot available to users in their browser.
68
+
69
+ ### 6. **Overall Flow:**
70
+ - The user chooses an input type (Image or PDF Quiz).
71
+ - Depending on the choice, the relevant fields (text, image, PDF) appear.
72
+ - The user submits their query or uploads their file, and the chatbot generates a response.
73
+ - The conversation history is maintained for context and can be cleared by the user.