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