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function to extract text from PDF files
|
9 |
+
def extract_text_from_pdf(pdf_file):
|
10 |
+
try:
|
11 |
+
text = ""
|
12 |
+
pdf_document = fitz.open(pdf_file)
|
13 |
+
for page_num in range(len(pdf_document)):
|
14 |
+
page = pdf_document[page_num]
|
15 |
+
text += page.get_text()
|
16 |
+
pdf_document.close()
|
17 |
+
return text
|
18 |
+
except Exception as e:
|
19 |
+
return f"Error extracting text from PDF: {str(e)}"
|
20 |
+
|
21 |
+
# Function to generate MCQ quiz from PDF content
|
22 |
+
def generate_mcq_quiz(pdf_content, num_questions, openai_api_key, model_choice):
|
23 |
+
if not openai_api_key:
|
24 |
+
return "Error: No API key provided."
|
25 |
+
|
26 |
+
openai.api_key = openai_api_key
|
27 |
+
limited_content = pdf_content[:8000] if len(pdf_content) > 8000 else pdf_content
|
28 |
+
|
29 |
+
prompt = f"""Based on the following document content, generate {num_questions} multiple-choice quiz questions.
|
30 |
+
For each question:
|
31 |
+
1. Create a clear question based on key concepts in the document
|
32 |
+
2. Provide 4 possible answers (A, B, C, D)
|
33 |
+
3. Indicate the correct answer
|
34 |
+
4. Briefly explain why the answer is correct
|
35 |
+
Format the output clearly with each question numbered and separated.
|
36 |
+
Document content:
|
37 |
+
{limited_content}
|
38 |
+
"""
|
39 |
+
|
40 |
+
try:
|
41 |
+
response = openai.ChatCompletion.create(
|
42 |
+
model=model_choice,
|
43 |
+
messages=[{"role": "user", "content": prompt}]
|
44 |
+
)
|
45 |
+
return response.choices[0].message.content
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error generating quiz: {str(e)}"
|
48 |
+
|
49 |
+
# Function to handle image inputs
|
50 |
+
def generate_image_response(input_text, image, openai_api_key, model_choice):
|
51 |
+
if not openai_api_key:
|
52 |
+
return "Error: No API key provided."
|
53 |
+
|
54 |
+
openai.api_key = openai_api_key
|
55 |
+
|
56 |
+
# Convert image to base64
|
57 |
+
buffered = io.BytesIO()
|
58 |
+
image.save(buffered, format="PNG")
|
59 |
+
base64_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
60 |
+
|
61 |
+
try:
|
62 |
+
response = openai.ChatCompletion.create(
|
63 |
+
model=model_choice,
|
64 |
+
messages=[
|
65 |
+
{
|
66 |
+
"role": "user",
|
67 |
+
"content": [
|
68 |
+
{"type": "text", "text": input_text},
|
69 |
+
{"type": "image_url",
|
70 |
+
"image_url": {"url": f"data:image/png;base64,{base64_str}"}
|
71 |
+
}
|
72 |
+
]
|
73 |
+
}
|
74 |
+
],
|
75 |
+
max_tokens=2000
|
76 |
+
)
|
77 |
+
return response.choices[0].message.content
|
78 |
+
except Exception as e:
|
79 |
+
return f"Error processing image: {str(e)}"
|
80 |
+
|
81 |
+
# Main chatbot function
|
82 |
+
def chatbot(input_text, image, pdf_file, openai_api_key, model_choice, pdf_content, num_quiz_questions, pdf_quiz_mode, history):
|
83 |
+
if history is None:
|
84 |
+
history = []
|
85 |
+
|
86 |
+
new_pdf_content = pdf_content
|
87 |
+
if pdf_file is not None:
|
88 |
+
new_pdf_content = extract_text_from_pdf(pdf_file)
|
89 |
+
|
90 |
+
if pdf_quiz_mode:
|
91 |
+
if new_pdf_content:
|
92 |
+
quiz_response = generate_mcq_quiz(new_pdf_content, int(num_quiz_questions), openai_api_key, model_choice)
|
93 |
+
history.append((f"π€: [PDF Quiz - {num_quiz_questions} questions]", f"π€: {quiz_response}"))
|
94 |
+
else:
|
95 |
+
history.append(("π€: [PDF Quiz]", "π€: Please upload a PDF file to generate questions."))
|
96 |
+
else:
|
97 |
+
if image is not None:
|
98 |
+
response = generate_image_response(input_text, image, openai_api_key, model_choice)
|
99 |
+
if input_text.strip():
|
100 |
+
history.append((f"π€: {input_text}", f"π€: {response}"))
|
101 |
+
else:
|
102 |
+
history.append((f"π€: [Image]", f"π€: {response}"))
|
103 |
+
|
104 |
+
return "", None, None, new_pdf_content, history
|
105 |
+
|
106 |
+
def clear_history():
|
107 |
+
return "", None, None, "", []
|
108 |
+
|
109 |
+
def update_input_type(choice):
|
110 |
+
if choice == "Image":
|
111 |
+
return (
|
112 |
+
gr.update(visible=True),
|
113 |
+
gr.update(visible=True),
|
114 |
+
gr.update(visible=False),
|
115 |
+
gr.update(visible=False),
|
116 |
+
gr.update(value=False)
|
117 |
+
)
|
118 |
+
elif choice == "PDF(QUIZ)":
|
119 |
+
return (
|
120 |
+
gr.update(visible=False),
|
121 |
+
gr.update(visible=False),
|
122 |
+
gr.update(visible=True),
|
123 |
+
gr.update(visible=True),
|
124 |
+
gr.update(value=True)
|
125 |
+
)
|
126 |
+
|
127 |
+
# Custom CSS styling
|
128 |
+
custom_css = """
|
129 |
+
.gradio-container {
|
130 |
+
font-family: 'Arial', sans-serif;
|
131 |
+
background-color: #f0f4f8;
|
132 |
+
}
|
133 |
+
.gradio-header {
|
134 |
+
background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%);
|
135 |
+
color: white;
|
136 |
+
padding: 20px;
|
137 |
+
border-radius: 8px;
|
138 |
+
text-align: center;
|
139 |
+
}
|
140 |
+
.gradio-chatbot {
|
141 |
+
background-color: white;
|
142 |
+
border-radius: 10px;
|
143 |
+
padding: 20px;
|
144 |
+
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
|
145 |
+
}
|
146 |
+
#submit-btn {
|
147 |
+
background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%);
|
148 |
+
color: white;
|
149 |
+
border-radius: 8px;
|
150 |
+
}
|
151 |
+
#clear-history {
|
152 |
+
background: linear-gradient(135deg, #e53e3e 0%, #f56565 100%);
|
153 |
+
color: white;
|
154 |
+
border-radius: 8px;
|
155 |
+
}
|
156 |
+
"""
|
157 |
+
|
158 |
+
def create_interface():
|
159 |
+
with gr.Blocks(css=custom_css) as demo:
|
160 |
+
gr.Markdown("""
|
161 |
+
<div class="gradio-header">
|
162 |
+
<h1>Multimodal Chatbot (Image + PDF Quiz)</h1>
|
163 |
+
<h3>Analyze images or generate quizzes from PDFs</h3>
|
164 |
+
</div>
|
165 |
+
""")
|
166 |
+
|
167 |
+
with gr.Accordion("Instructions", open=False):
|
168 |
+
gr.Markdown("""
|
169 |
+
- **Image Chat**: Upload an image and ask questions about it
|
170 |
+
- **PDF Quiz**: Upload a PDF and generate multiple-choice questions
|
171 |
+
- Always provide your OpenAI API key
|
172 |
+
- Choose appropriate model (o1 for images, o3-mini for text)
|
173 |
+
""")
|
174 |
+
|
175 |
+
pdf_content = gr.State("")
|
176 |
+
|
177 |
+
with gr.Row():
|
178 |
+
openai_api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...")
|
179 |
+
|
180 |
+
with gr.Row():
|
181 |
+
input_type = gr.Radio(["Image", "PDF(QUIZ)"], label="Input Type", value="Image")
|
182 |
+
|
183 |
+
with gr.Row():
|
184 |
+
input_text = gr.Textbox(label="Question (for images)", visible=True)
|
185 |
+
image_input = gr.Image(label="Upload Image", type="pil", visible=True)
|
186 |
+
pdf_input = gr.File(label="Upload PDF", visible=False)
|
187 |
+
quiz_slider = gr.Slider(1, 20, value=5, step=1, label="Number of Questions", visible=False)
|
188 |
+
quiz_mode = gr.Checkbox(visible=False)
|
189 |
+
|
190 |
+
with gr.Row():
|
191 |
+
model_choice = gr.Dropdown(["o1", "o3-mini"], label="Model", value="o1")
|
192 |
+
submit_btn = gr.Button("Submit", elem_id="submit-btn")
|
193 |
+
clear_btn = gr.Button("Clear History", elem_id="clear-history")
|
194 |
+
|
195 |
+
chat_history = gr.Chatbot()
|
196 |
+
|
197 |
+
input_type.change(
|
198 |
+
update_input_type,
|
199 |
+
inputs=[input_type],
|
200 |
+
outputs=[input_text, image_input, pdf_input, quiz_slider, quiz_mode]
|
201 |
+
)
|
202 |
+
|
203 |
+
submit_btn.click(
|
204 |
+
chatbot,
|
205 |
+
inputs=[input_text, image_input, pdf_input, openai_api_key, model_choice, pdf_content, quiz_slider, quiz_mode, chat_history],
|
206 |
+
outputs=[input_text, image_input, pdf_input, pdf_content, chat_history]
|
207 |
+
)
|
208 |
+
|
209 |
+
clear_btn.click(
|
210 |
+
clear_history,
|
211 |
+
outputs=[input_text, image_input, pdf_input, pdf_content, chat_history]
|
212 |
+
)
|
213 |
+
|
214 |
+
return demo
|
215 |
+
|
216 |
+
if __name__ == "__main__":
|
217 |
+
demo = create_interface()
|
218 |
+
demo.launch()
|