DpShirazi commited on
Commit
8a51490
1 Parent(s): 54fa669

Upload app4.py

Browse files
Files changed (1) hide show
  1. app4.py +197 -0
app4.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 sentence_transformers import SentenceTransformer, util
14
+ from questiongenerator import QuestionGenerator
15
+ qg = QuestionGenerator()
16
+
17
+
18
+
19
+ def highlight_similar_sentence(text1, text2, color='yellow'):
20
+ # Load the pre-trained sentence-transformers model
21
+ model = SentenceTransformer("paraphrase-MiniLM-L6-v2")
22
+
23
+ # Split text into sentences
24
+ sentences_text1 = [sentence.strip() for sentence in text1.split('.') if sentence.strip()]
25
+ sentences_text2 = [sentence.strip() for sentence in text2.split('.') if sentence.strip()]
26
+
27
+ # Compute embeddings for text1
28
+ #embeddings_text1 = model.encode(sentences_text1, convert_to_tensor=True)
29
+
30
+ highlighted_text2 = text2
31
+ max_similarity = 0.0
32
+
33
+ # Find the most similar sentence in text2 for each sentence in text1
34
+ for sentence_text1 in sentences_text1:
35
+ # Compute embeddings for the current sentence in text1
36
+ embedding_text1 = model.encode(sentence_text1, convert_to_tensor=True)
37
+
38
+ for sentence_text2 in sentences_text2:
39
+ # Compute cosine similarity between sentence in text1 and text2
40
+ embedding_text2 = model.encode(sentence_text2, convert_to_tensor=True)
41
+ similarity = util.pytorch_cos_sim(embedding_text1, embedding_text2).item()
42
+
43
+ # Highlight the most similar sentence in text2
44
+ if similarity > max_similarity:
45
+ max_similarity = similarity
46
+ highlighted_text2= highlight_text(text2, sentence_text2, color=color)
47
+ #highlighted_text2 = text2.replace(sentence_text2, f"<span style='background-color: {color};'>{sentence_text2}</span>")
48
+
49
+ return highlighted_text2
50
+
51
+
52
+ def Extract_QA(qlist,selected_extracted_text):
53
+ Q_All=''
54
+ A_All=''
55
+ xs=['A','B','C','D']
56
+ h_colors=['yellow', 'red', 'DodgerBlue', 'Orange', 'Violet']
57
+ for i in range(len(qlist)):
58
+ question_i= qlist[i]['question']
59
+ Choices_ans= []
60
+ Choice_is_correct=[]
61
+ for j in range(4):
62
+ Choices_ans= Choices_ans+ [qlist[i]['answer'][j]['answer']]
63
+ Choice_is_correct= Choice_is_correct+ [qlist[i]['answer'][j]['correct']]
64
+
65
+ Q=f"""
66
+ Q_{i+1}: {question_i}
67
+ A. {Choices_ans[0]}
68
+ B. {Choices_ans[1]}
69
+ C. {Choices_ans[2]}
70
+ D. {Choices_ans[3]}
71
+
72
+ """
73
+
74
+ result = [x for x, y in zip(xs, Choice_is_correct) if y ]
75
+ correct_answer= [x for x, y in zip(Choices_ans, Choice_is_correct) if y ]
76
+ A= f"""
77
+ <p>Answer_{i+1}: {result[0]} - {correct_answer[0]}<p>
78
+
79
+
80
+ """
81
+ color= h_colors[i]
82
+
83
+ A_sen= f""" The correct answer is {correct_answer[0]}."""
84
+
85
+ A= highlight_text(input_text=A, selcted_text=correct_answer[0], color=color)
86
+ selected_extracted_text= highlight_similar_sentence(A_sen, selected_extracted_text, color=color)
87
+
88
+
89
+ Q_All= Q_All+Q
90
+ A_All=A_All+ A
91
+
92
+
93
+ return (Q_All,A_All,selected_extracted_text)
94
+
95
+
96
+
97
+
98
+
99
+
100
+ def extract_text_from_pdf(pdf_file_path):
101
+ # Read the PDF file
102
+ global extracted_text
103
+ text = []
104
+ with fitz.open(pdf_file_path) as doc:
105
+ for page in doc:
106
+ text.append(page.get_text())
107
+ extracted_text= '\n'.join(text)
108
+ extracted_text= get_sub_text(extracted_text)
109
+
110
+ return ("The pdf is uploaded Successfully from:"+ str(pdf_file_path))
111
+
112
+ qg = qs.QuestionGenerator()
113
+
114
+ def get_sub_text(TXT):
115
+ sub_texts= qg._split_into_segments(TXT)
116
+ if isinstance(sub_texts, list):
117
+ return sub_texts
118
+ else:
119
+ return [sub_texts]
120
+
121
+ def highlight_text(input_text, selcted_text, color='yellow'):
122
+ # Replace 'highlight' with <span> tags for highlighting
123
+ highlighted_text = input_text.replace(selcted_text, f'<span style="background-color: {color}">{selcted_text}</span>')
124
+ return highlighted_text
125
+
126
+
127
+ def pick_One_txt(sub_texts):
128
+ global selected_extracted_text
129
+ N= len(sub_texts)
130
+ if N==1:
131
+ selected_extracted_text= sub_texts[0]
132
+ return(selected_extracted_text)
133
+ # Generate a random number between low and high
134
+ random_number = random.uniform(0, N)
135
+ # Pick the integer part of the random number
136
+ random_number = int(random_number)
137
+ selected_extracted_text= sub_texts[random_number]
138
+
139
+ return(selected_extracted_text)
140
+
141
+
142
+ def pipeline(NoQs):
143
+ global Q,A
144
+ text= selected_extracted_text
145
+ qlist= qg.generate(text, num_questions=NoQs, answer_style="multiple_choice")
146
+ Q,A,highligthed_text= Extract_QA(qlist,text)
147
+ A= A + '\n'+highligthed_text
148
+ return (Q,A)
149
+
150
+ def ReurnAnswer():
151
+ return A
152
+
153
+ def GetQuestion(NoQs):
154
+ NoQs=int(NoQs)
155
+ pick_One_txt(extracted_text)
156
+ Q,A=pipeline(NoQs)
157
+ return Q
158
+
159
+ with gr.Blocks() as demo:
160
+
161
+
162
+ with gr.Row():
163
+ with gr.Column(scale=1):
164
+ with gr.Row():
165
+ gr.Image("PupQuizAI.png")
166
+ 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 each time you push 'Show Questions'.
167
+ """ )
168
+
169
+ input_file=gr.UploadButton(label='Select a file!', file_types=[".pdf"])
170
+ input_file.upload(extract_text_from_pdf, input_file)
171
+ #upload_btn = gr.Button(value="Upload the pdf File.")
172
+ Gen_Question = gr.Button(value="Show Questions")
173
+ Gen_Answer = gr.Button(value="Show Answers")
174
+ No_Qs= gr.Slider(minimum=1, maximum=5,value=3, step=1, label='Max # of Questions')
175
+
176
+ gr.Markdown(""" 🐶
177
+ **Instructions:**
178
+ * 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)
179
+ * Select the number of questions you want generated from the "# of Questions" selector.
180
+ * Click "Show Questions"
181
+ * Then, if you want answers to the questions, select "Show Answers" """ )
182
+
183
+ #gr.Image("PupQuizAI.png")
184
+
185
+
186
+
187
+ with gr.Column(scale=2.0):
188
+ #file_stat= gr.Textbox(label="File Status")
189
+ question = gr.Textbox(label="Question(s)")
190
+ #Answer = gr.Textbox(label="Answer(s)")
191
+ Answer = gr.HTML(label="Answer(s)")
192
+
193
+ Gen_Question.click(GetQuestion, inputs=No_Qs, outputs=question, api_name="QuestioGenerator")
194
+ Gen_Answer.click(ReurnAnswer, inputs=None, outputs=Answer, api_name="QuestioGenerator")
195
+
196
+
197
+ demo.launch()