Anujgr8 commited on
Commit
be99efd
1 Parent(s): 43c892a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +236 -0
app.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import PyPDF2
3
+ import os
4
+ import subprocess
5
+ import tempfile
6
+ import google.generativeai as genai
7
+ from nltk.corpus import stopwords
8
+ from nltk.tokenize import word_tokenize, sent_tokenize
9
+ def summarize_text(text):
10
+ # Tokenizing the text
11
+ stopWords = set(stopwords.words("english"))
12
+ words = word_tokenize(text)
13
+
14
+ # Creating a frequency table to keep the score of each word
15
+ freqTable = dict()
16
+ for word in words:
17
+ word = word.lower()
18
+ if word in stopWords:
19
+ continue
20
+ if word in freqTable:
21
+ freqTable[word] += 1
22
+ else:
23
+ freqTable[word] = 1
24
+
25
+ # Creating a dictionary to keep the score of each sentence
26
+ sentences = sent_tokenize(text)
27
+ sentenceValue = dict()
28
+
29
+ for sentence in sentences:
30
+ for word, freq in freqTable.items():
31
+ if word in sentence.lower():
32
+ if sentence in sentenceValue:
33
+ sentenceValue[sentence] += freq
34
+ else:
35
+ sentenceValue[sentence] = freq
36
+
37
+ sumValues = 0
38
+ for sentence in sentenceValue:
39
+ sumValues += sentenceValue[sentence]
40
+
41
+ # Average value of a sentence from the original text
42
+ average = int(sumValues / len(sentenceValue))
43
+
44
+ # Storing sentences into our summary.
45
+ summary = ''
46
+ for sentence in sentences:
47
+ if (sentence in sentenceValue) and (sentenceValue[sentence] > (1.2 * average)):
48
+ summary += " " + sentence
49
+
50
+ return summary
51
+
52
+
53
+
54
+ def Notes_data(API,prompt):
55
+ genai.configure(api_key=API)
56
+ model = genai.GenerativeModel('gemini-pro')
57
+ response = model.generate_content(prompt)
58
+ return response.text
59
+ def markdown_to_pdf(markdown_content):
60
+ # Create a temporary file to store the markdown content
61
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".md", encoding="utf-8") as temp_md_file:
62
+ temp_md_file.write(markdown_content)
63
+ temp_md_filename = temp_md_file.name
64
+
65
+ # Define output PDF filename
66
+ output_pdf_filename = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False).name
67
+
68
+ # Execute mdpdf command
69
+ command = f"mdpdf -o {str(output_pdf_filename)} {str(temp_md_filename)}"
70
+ subprocess.run(command, shell=True)
71
+
72
+ # Delete temporary markdown file
73
+ os.remove(temp_md_filename)
74
+
75
+ return output_pdf_filename
76
+ # Authenticate using the API key
77
+ # Set the API key as an environment variable
78
+ def extract_text_from_pdf(pdf_file):
79
+ text = ""
80
+ with open(pdf_file, "rb") as file:
81
+ pdf_reader = PyPDF2.PdfReader(file)
82
+ for page_num in range(len(pdf_reader.pages)):
83
+ page = pdf_reader.pages[page_num]
84
+ text += page.extract_text()
85
+ return text
86
+ def get_prompt(text,subject,topic):
87
+ prompt = f"""
88
+ you are given a {subject} topic
89
+ {text}
90
+ Create the well Structured detailed Notes as a html code in the following templat. Consider the following points while creating the template.
91
+ 1. Introduction part must have minimum 500 words
92
+ 2. In the notes part please describe each topic in detail with word count 1000
93
+ 3. Key points part must have minimum 250 words.
94
+ 4. The notes should be on topic.
95
+ <!DOCTYPE html>
96
+ <html>
97
+ <head>
98
+ <meta charset="UTF-8">
99
+ <title>Chapter Study Material </title>
100
+ </head>
101
+ <body>
102
+ <h1>Study Material</h1>
103
+ <h3>1. Introduction</h3>
104
+ <p>
105
+ <!-- Replace with your introduction content-->
106
+ </p>
107
+
108
+ <h3>2.Important Topics in detail from the chapter. </h3>
109
+ <!--DEcribe the Imortant topics don't forget the quations formulas and other things replated to the topic.Describe and elaborate each of the topic and formula and equation-->
110
+ <h4><!-- Heading of topic 1>r</h4>
111
+ <p>
112
+ <!--Desribe the topice in 100 words and bullet out the subtopic and describe if any-->
113
+ <!-- Euations and formulas if any-->
114
+ </P>
115
+ <h4><!-- Heading of topic 2>r</h4>
116
+ <p>
117
+ <!--Desribe the topice in 100 words bullet out the subtopic and describe if any-->
118
+ <!-- Euations and formulas if any-->
119
+ </P>
120
+ <h4><!-- Heading of topic 3>r</h4>
121
+ <p>
122
+ <!--Desribe the topice in 100 words bullet out the subtopic and describe if any-->
123
+ <!-- Euations and formulas if any-->
124
+ </P>
125
+ <!-- Like wise add all possible topics from the chapter-->
126
+ <h3> Key Points from the chapter </h3>
127
+ <p> <!--Replace with your key points content in bulletpoints-->
128
+ </body>
129
+ </html>
130
+
131
+ """
132
+ prompt2 = f"""
133
+ Create detailed study material from the given given Chapter from a book by considering the following points in mind
134
+ 1.Ensure a thorough understanding of the chapter's main ideas and themes.
135
+ 2.Identify and emphasise essential concepts and arguments.
136
+ 3.Create a well-organised outline reflecting the chapter's structure.
137
+ 4.Provide concise, to-the-point summaries for each section.
138
+ 5.Define and clarify key terms and concepts introduced in the chapter.
139
+ 6.Pose engaging questions for self-assessment and discussion.
140
+ 7.Ensure accurate citations for quotes and references.
141
+ 8.use given text only for reference you can add as much as detail you can.
142
+ 9.The response should be complete.
143
+ 10.Generate response as a html page with all type of formatting.
144
+ 11.Each point you take in the study material please describe it in detail.
145
+ Use the following HTML template to structure your notes:
146
+ <!DOCTYPE html>
147
+ <html>
148
+ <head>
149
+ <meta charset="UTF-8">
150
+ <title>Chapter Study Material</title>
151
+ </head>
152
+ <body>
153
+ <h1>Chapter Study Material</h1>
154
+ <h3>1. Introduction</h3>
155
+ <p>
156
+ <!-- Replace with your introduction content -->
157
+ </p>
158
+
159
+ <h3>2. Describe important Points in detail from the chapter</h3>
160
+ <h4><!-- Heading of topic 1>r</h4>
161
+ <p>
162
+ <!--Desribe the topice in detail and elaborate it and bullet out the subtopic and describe if any-->
163
+ </P>
164
+ <h4><!-- Heading of topic 2>r</h4>
165
+ <p>
166
+ <!--Desribe the topice in detail and elaborate bullet out the subtopic and describe if any-->
167
+
168
+ </P>
169
+ <h4><!-- Heading of topic 3>r</h4>
170
+ <p>
171
+ <!--Desribe the topice 3 in detail and elaborate-->
172
+ </P>
173
+ <!-- Like wise add all possible topics from the chapter-->
174
+ <h3>3. Summary</h3>
175
+ <p>
176
+ <!-- Replace with your summary content -->
177
+ </p>
178
+
179
+ <!-- Additional content can be added as needed -->
180
+ </body>
181
+ </html>
182
+ Chapter Context:
183
+ {text}"""
184
+
185
+ prompt3 = f"""
186
+ Create detailed study material from the given given topic from a book chapter by considering the following points in mind
187
+ 1.Notes will be based on topic specific.
188
+ 2.Ensure that explanations are clear and concise, using simple language to make complex concepts understandable.
189
+ 3.Highlight key concepts, theories, and important formulas related to each topic in detail.
190
+ 4.For mathematical components like physics and chemistry, provide step-by-step solutions for numerical problems.
191
+ 5.The notes should be in detail cover all the concepts related to the topic.
192
+ 6.use given text only for reference you can add as much as detail you can.
193
+ 7.The response should be complete.
194
+ 8.Ignore unneccessory informstion in the context.
195
+ 9.Generate response as a valid 'mardown formate' page with all type of formatting.
196
+
197
+ Topics: {topic}
198
+
199
+ Chapter Reference:-
200
+ {text}"""
201
+ return prompt2 if subject=="English" else prompt3
202
+
203
+ def pdf_generator(API,class_name, subject,chapter,query=None):
204
+ # Create a list to store the generated PDF file paths
205
+ pdf_file_path = f"Main_content/Books/{class_name}/{subject}/{chapter}.pdf"
206
+ pdf_text = summarize_text(extract_text_from_pdf(pdf_file_path))
207
+ study_material = generate_study_material(API,pdf_text,subject,query)
208
+ study_material = markdown_to_pdf(study_material)
209
+ return study_material
210
+ def generate_study_material(API,pdf_text,subject,topic):
211
+ response = Notes_data(API,get_prompt(pdf_text,subject,topic))
212
+ return response
213
+
214
+ # Define Gradio input and output components
215
+ input_components = [
216
+ gr.Textbox(label="Enter your Gemini API"),
217
+ gr.Dropdown(["Class 10", "Class 11", "Class 12"], label="Select Class"),
218
+ gr.Dropdown(["Math", "Science", "English","Chemistry"], label="Select Subject"),
219
+ gr.Dropdown(["Chapter 1", "Chapter 2", "Chapter 3"], label="Select Chapters"),
220
+ gr.Textbox(label="Write the topics name saperated by ','")
221
+ ]
222
+ output_component = gr.File(label="Notes")
223
+
224
+ # Create the Gradio interface
225
+ iface = gr.Interface(
226
+ fn=pdf_generator,
227
+ inputs=input_components,
228
+ outputs=output_component,
229
+ title="PDF Generator",
230
+ description="Generate PDFs based on class, subject, and chapter selection.",
231
+ )
232
+
233
+ # Launch the Gradio app
234
+ iface.launch()
235
+
236
+