Upload app3.py
Browse files
app3.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
112 |
+
with gr.Row():
|
113 |
+
with gr.Column(scale=1):
|
114 |
+
with gr.Row():
|
115 |
+
gr.Image("PupQuizAI.png")
|
116 |
+
gr.Markdown(""" 🐶 **PupQuizAI** is an Artificial-Intelligence tool that streamlines the studying process. Simply input a text pdf that you need to study from. Then, PupQuiz will create 1-5 custom questions for you to study from.
|
117 |
+
""" )
|
118 |
+
|
119 |
+
input_file=gr.UploadButton(label='Select a file!', file_types=[".pdf"])
|
120 |
+
input_file.upload(extract_text_from_pdf, input_file)
|
121 |
+
#upload_btn = gr.Button(value="Upload the pdf File.")
|
122 |
+
Gen_Question = gr.Button(value="Show Questions")
|
123 |
+
Gen_Answer = gr.Button(value="Show Answers")
|
124 |
+
No_Qs= gr.Slider(minimum=1, maximum=5,step=1, label='Max # of Questions')
|
125 |
+
|
126 |
+
gr.Markdown(""" 🐶
|
127 |
+
**Instructions:**
|
128 |
+
* Start by selecting a 'pdf' text file you want to upload by clicking the "Select file" button. (PupQuiz currently only supports files that can have highlightable text)
|
129 |
+
* Select the number of questions you want generated from the "# of Questions" selector.
|
130 |
+
* Click "Show Questions"
|
131 |
+
* Then, if you want answers to the questions, select "Show Answers" """ )
|
132 |
+
|
133 |
+
#gr.Image("PupQuizAI.png")
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
with gr.Column(scale=2.0):
|
138 |
+
#file_stat= gr.Textbox(label="File Status")
|
139 |
+
question = gr.Textbox(label="Question(s)")
|
140 |
+
Answer = gr.Textbox(label="Answer(s)")
|
141 |
+
|
142 |
+
Gen_Question.click(GetQuestion, inputs=No_Qs, outputs=question, api_name="QuestioGenerator")
|
143 |
+
Gen_Answer.click(ReurnAnswer, inputs=None, outputs=Answer, api_name="QuestioGenerator")
|
144 |
+
|
145 |
+
|
146 |
+
demo.launch()
|