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|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Mon Dec 25 18:18:27 2023
|
| 4 |
+
|
| 5 |
+
@author: alish
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import fitz # PyMuPDF
|
| 10 |
+
import questiongenerator as qs
|
| 11 |
+
import random
|
| 12 |
+
|
| 13 |
+
from questiongenerator import QuestionGenerator
|
| 14 |
+
qg = QuestionGenerator()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def Extract_QA(qlist):
|
| 19 |
+
Q_All=''
|
| 20 |
+
A_All=''
|
| 21 |
+
|
| 22 |
+
for i in range(len(qlist)):
|
| 23 |
+
question_i= qlist[i]['question']
|
| 24 |
+
Choices_ans= []
|
| 25 |
+
Choice_is_correct=[]
|
| 26 |
+
for j in range(4):
|
| 27 |
+
Choices_ans= Choices_ans+ [qlist[i]['answer'][j]['answer']]
|
| 28 |
+
Choice_is_correct= Choice_is_correct+ [qlist[i]['answer'][j]['correct']]
|
| 29 |
+
|
| 30 |
+
Q=f"""
|
| 31 |
+
Q_{i+1}: {question_i}
|
| 32 |
+
A. {Choices_ans[0]}
|
| 33 |
+
B. {Choices_ans[1]}
|
| 34 |
+
C. {Choices_ans[2]}
|
| 35 |
+
D. {Choices_ans[3]}
|
| 36 |
+
|
| 37 |
+
"""
|
| 38 |
+
xs=['A','B','C','D']
|
| 39 |
+
result = [x for x, y in zip(xs, Choice_is_correct) if y ]
|
| 40 |
+
A= f"""
|
| 41 |
+
Answer_{i+1}: {result[0]}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
"""
|
| 45 |
+
Q_All= Q_All+Q
|
| 46 |
+
A_All=A_All+ A
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
return (Q_All,A_All)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def extract_text_from_pdf(pdf_file_path):
|
| 57 |
+
# Read the PDF file
|
| 58 |
+
global extracted_text
|
| 59 |
+
text = []
|
| 60 |
+
with fitz.open(pdf_file_path) as doc:
|
| 61 |
+
for page in doc:
|
| 62 |
+
text.append(page.get_text())
|
| 63 |
+
extracted_text= '\n'.join(text)
|
| 64 |
+
extracted_text= get_sub_text(extracted_text)
|
| 65 |
+
|
| 66 |
+
return ("The pdf is uploaded Successfully from:"+ str(pdf_file_path))
|
| 67 |
+
|
| 68 |
+
qg = qs.QuestionGenerator()
|
| 69 |
+
|
| 70 |
+
def get_sub_text(TXT):
|
| 71 |
+
sub_texts= qg._split_into_segments(TXT)
|
| 72 |
+
if isinstance(sub_texts, list):
|
| 73 |
+
return sub_texts
|
| 74 |
+
else:
|
| 75 |
+
return [sub_texts]
|
| 76 |
+
|
| 77 |
+
def pick_One_txt(sub_texts):
|
| 78 |
+
global selected_extracted_text
|
| 79 |
+
N= len(sub_texts)
|
| 80 |
+
if N==1:
|
| 81 |
+
selected_extracted_text= sub_texts[0]
|
| 82 |
+
return(selected_extracted_text)
|
| 83 |
+
# Generate a random number between low and high
|
| 84 |
+
random_number = random.uniform(0, N)
|
| 85 |
+
# Pick the integer part of the random number
|
| 86 |
+
random_number = int(random_number)
|
| 87 |
+
selected_extracted_text= sub_texts[random_number]
|
| 88 |
+
|
| 89 |
+
return(selected_extracted_text)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def pipeline(NoQs):
|
| 93 |
+
global Q,A
|
| 94 |
+
text= selected_extracted_text
|
| 95 |
+
qlist= qg.generate(text, num_questions=NoQs, answer_style="multiple_choice")
|
| 96 |
+
Q,A= Extract_QA(qlist)
|
| 97 |
+
A= A + '\n'+text
|
| 98 |
+
return (Q,A)
|
| 99 |
+
|
| 100 |
+
def ReurnAnswer():
|
| 101 |
+
return A
|
| 102 |
+
|
| 103 |
+
def GetQuestion(NoQs):
|
| 104 |
+
NoQs=int(NoQs)
|
| 105 |
+
pick_One_txt(extracted_text)
|
| 106 |
+
Q,A=pipeline(NoQs)
|
| 107 |
+
return Q
|
| 108 |
+
|
| 109 |
+
with gr.Blocks() as demo:
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
#input_file=gr.File(type="filepath", label="Upload PDF Document")
|
| 113 |
+
input_file=gr.UploadButton(label='Select a file!', file_types=[".pdf"])
|
| 114 |
+
#upload_btn = gr.Button(value="Upload File")
|
| 115 |
+
#txt= extract_text_from_pdf(input_file)
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column():
|
| 118 |
+
upload_btn = gr.Button(value="Upload the pdf File.")
|
| 119 |
+
Gen_Question = gr.Button(value="Show the Question(s)")
|
| 120 |
+
Gen_Answer = gr.Button(value="Show the Answer(s)")
|
| 121 |
+
No_Qs= gr.Slider(minimum=1, maximum=5,step=1, label='No Questions')
|
| 122 |
+
'''
|
| 123 |
+
with gr.Accordion("Instruction"):
|
| 124 |
+
gr.Markdown("Start by selecting a 'pdf' file using 'Select file' tab." )
|
| 125 |
+
gr.Markdown("Upload the selceted 'pdf' file using 'Upload the pdf file' tab." )
|
| 126 |
+
gr.Markdown("The code will randomly select a block of the text and generate N questions form it each time you push 'Show Question(s)" )
|
| 127 |
+
'''
|
| 128 |
+
gr.Markdown(""" **Instruction**
|
| 129 |
+
* Start by selecting a 'pdf' file using 'Select file' tab.
|
| 130 |
+
* Upload the selceted 'pdf' file using 'Upload the pdf file' tab.
|
| 131 |
+
* The code will randomly select a block of the text and generate N questions form it each time you push 'Show Question(s)'. """ )
|
| 132 |
+
|
| 133 |
+
gr.Image("PupQuizAI.png")
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
with gr.Column():
|
| 138 |
+
file_stat= gr.Textbox(label="File Status")
|
| 139 |
+
question = gr.Textbox(label="Question(s)")
|
| 140 |
+
Answer = gr.Textbox(label="Answer(s)")
|
| 141 |
+
'''
|
| 142 |
+
with gr.Accordion("Instruction"):
|
| 143 |
+
gr.Markdown("Start by selecting a 'pdf' file using 'Select file' tab." )
|
| 144 |
+
gr.Markdown("Upload the selceted 'pdf' file using 'Upload the pdf file' tab." )
|
| 145 |
+
gr.Markdown("The code will randomly select a block of the text and generate N questions form it each time you push 'Show Question(s)" )
|
| 146 |
+
'''
|
| 147 |
+
|
| 148 |
+
upload_btn.click(extract_text_from_pdf, inputs=input_file, outputs=file_stat, api_name="QuestioGenerator")
|
| 149 |
+
Gen_Question.click(GetQuestion, inputs=No_Qs, outputs=question, api_name="QuestioGenerator")
|
| 150 |
+
Gen_Answer.click(ReurnAnswer, inputs=None, outputs=Answer, api_name="QuestioGenerator")
|
| 151 |
+
#examples = gr.Examples(examples=["I went to the supermarket yesterday.", "Helen is a good swimmer."],
|
| 152 |
+
# inputs=[english])
|
| 153 |
+
|
| 154 |
+
demo.launch()
|