File size: 8,705 Bytes
be99efd
 
 
 
 
 
 
 
eb7843e
 
21378d2
eb7843e
be99efd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f7afce
be99efd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import gradio as gr
import PyPDF2
import os
import subprocess
import tempfile
import google.generativeai as genai  
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
import nltk
nltk.download('stopwords')
nltk.download('punkt')

def summarize_text(text):
    # Tokenizing the text
    stopWords = set(stopwords.words("english"))
    words = word_tokenize(text)

    # Creating a frequency table to keep the score of each word
    freqTable = dict()
    for word in words:
        word = word.lower()
        if word in stopWords:
            continue
        if word in freqTable:
            freqTable[word] += 1
        else:
            freqTable[word] = 1

    # Creating a dictionary to keep the score of each sentence
    sentences = sent_tokenize(text)
    sentenceValue = dict()

    for sentence in sentences:
        for word, freq in freqTable.items():
            if word in sentence.lower():
                if sentence in sentenceValue:
                    sentenceValue[sentence] += freq
                else:
                    sentenceValue[sentence] = freq

    sumValues = 0
    for sentence in sentenceValue:
        sumValues += sentenceValue[sentence]

    # Average value of a sentence from the original text
    average = int(sumValues / len(sentenceValue))

    # Storing sentences into our summary.
    summary = ''
    for sentence in sentences:
        if (sentence in sentenceValue) and (sentenceValue[sentence] > (1.2 * average)):
            summary += " " + sentence

    return summary



def Notes_data(API,prompt):
    genai.configure(api_key=API)
    model = genai.GenerativeModel('gemini-pro')
    response = model.generate_content(prompt)
    return response.text
def markdown_to_pdf(markdown_content):
    # Create a temporary file to store the markdown content
    with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".md", encoding="utf-8") as temp_md_file:
        temp_md_file.write(markdown_content)
        temp_md_filename = temp_md_file.name

    # Define output PDF filename
    output_pdf_filename = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False).name

    # Execute mdpdf command
    command = f"mdpdf -o {str(output_pdf_filename)} {str(temp_md_filename)}"
    subprocess.run(command, shell=True)

    # Delete temporary markdown file
    os.remove(temp_md_filename)

    return output_pdf_filename
# Authenticate using the API key
# Set the API key as an environment variable
def extract_text_from_pdf(pdf_file):
    text = ""
    with open(pdf_file, "rb") as file:
        pdf_reader = PyPDF2.PdfReader(file)
        for page_num in range(len(pdf_reader.pages)):
            page = pdf_reader.pages[page_num]
            text += page.extract_text()
    return text
def get_prompt(text,subject,topic):
  prompt = f"""
	  you are given a {subject} topic
	  {text}
	  Create the well Structured detailed Notes as a html code in the following templat. Consider the following points while creating the template.
	  1. Introduction part must have minimum 500 words
	  2. In the notes part please describe each topic in detail with word count 1000
	  3. Key points part must have minimum 250 words.
	  4. The notes should be on topic.
	<!DOCTYPE html>
	<html>
	<head>
	    <meta charset="UTF-8">
	    <title>Chapter Study Material </title>
	</head>
	<body>
	    <h1>Study Material</h1>
	    <h3>1. Introduction</h3>
	    <p>
		<!-- Replace with your introduction content-->
	    </p>

	    <h3>2.Important Topics in detail from the chapter. </h3>
		<!--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-->
	  	<h4><!-- Heading of topic 1>r</h4>
	  	<p>
	  	   <!--Desribe the topice in 100 words and bullet out the subtopic and describe if any-->
	  	   <!-- Euations and formulas if any-->
	  	</P>
	  	 <h4><!-- Heading of topic 2>r</h4>
	  	<p>
	  	   <!--Desribe the topice in 100 words bullet out the subtopic and describe if any-->
	  	   <!-- Euations and formulas if any-->
	  	</P>
	  	<h4><!-- Heading of topic 3>r</h4>
	  	<p>
	  	   <!--Desribe the topice in 100 words bullet out the subtopic and describe if any-->
	  	   <!-- Euations and formulas if any-->
	  	</P>
	  	<!-- Like wise add all possible topics from the chapter-->
	     <h3> Key Points from the chapter </h3>
	     <p> <!--Replace with your key points content in bulletpoints-->
	</body>
	</html>

	  """
  prompt2 = f"""
  	Create detailed study material from the given given Chapter from a book by considering the following points in mind
		1.Ensure a thorough understanding of the chapter's main ideas and themes.
		2.Identify and emphasise essential concepts and arguments.
		3.Create a well-organised outline reflecting the chapter's structure.
		4.Provide concise, to-the-point summaries for each section.
		5.Define and clarify key terms and concepts introduced in the chapter.
		6.Pose engaging questions for self-assessment and discussion.
		7.Ensure accurate citations for quotes and references.
		8.use given text only for reference you can add as much as detail you can.
		9.The response should be  complete.
		10.Generate response as a html page with all type of formatting.
		11.Each point you take in the study material please describe it in detail.
	Use the following HTML template to structure your notes:
	<!DOCTYPE html>
	<html>
	<head>
	    <meta charset="UTF-8">
	    <title>Chapter Study Material</title>
	</head>
	<body>
	    <h1>Chapter Study Material</h1>
	    <h3>1. Introduction</h3>
	    <p>
		<!-- Replace with your introduction content -->
	    </p>

	    <h3>2. Describe important Points in detail from the chapter</h3>
	    <h4><!-- Heading of topic 1>r</h4>
	  	<p>
	  	   <!--Desribe the topice in detail and elaborate it and bullet out the subtopic and describe if any-->
	  	</P>
	  	 <h4><!-- Heading of topic 2>r</h4>
	  	<p>
	  	   <!--Desribe the topice in detail and elaborate bullet out the subtopic and describe if any-->
	  
	  	</P>
	  	<h4><!-- Heading of topic 3>r</h4>
	  	<p>
	  	   <!--Desribe the topice 3 in detail and elaborate-->
	  	</P>
			<!-- Like wise add all possible topics from the chapter-->
	    <h3>3. Summary</h3>
	    <p>
		<!-- Replace with your summary content -->
	    </p>

	    <!-- Additional content can be added as needed -->
	</body>
	</html>
	Chapter Context:
	{text}"""

  prompt3 = f"""
  Create detailed study material from the given given topic from a book chapter by considering the following points in mind
	1.Notes will be based on topic specific.
	2.Ensure that explanations are clear and concise, using simple language to make complex concepts understandable.
	3.Highlight key concepts, theories, and important formulas related to each topic in detail.
	4.For  mathematical components like physics and chemistry, provide step-by-step solutions for numerical problems.
	5.The notes should be in detail cover all the concepts related to the topic.
	6.use given text only for reference you can add as much as detail you can.
	7.The response should be  complete.
	8.Ignore unneccessory informstion in the context.
	9.Generate response as a valid 'mardown formate' page with all type of formatting.
    
     Topics:  {topic}

	Chapter Reference:- 
	 {text}"""
  return prompt2 if subject=="English" else prompt3

def pdf_generator(API,class_name, subject,chapter,query=None):
    # Create a list to store the generated PDF file paths
    pdf_file_path = f"Books/{class_name}/{subject}/{chapter}.pdf"
    pdf_text = summarize_text(extract_text_from_pdf(pdf_file_path))
    study_material = generate_study_material(API,pdf_text,subject,query)
    study_material = markdown_to_pdf(study_material)
    return study_material
def generate_study_material(API,pdf_text,subject,topic):
    response = Notes_data(API,get_prompt(pdf_text,subject,topic))
    return response
    
# Define Gradio input and output components
input_components = [
    gr.Textbox(label="Enter your Gemini API"),
    gr.Dropdown(["Class 10", "Class 11", "Class 12"], label="Select Class"),
    gr.Dropdown(["Math", "Science", "English","Chemistry"], label="Select Subject"),
    gr.Dropdown(["Chapter 1", "Chapter 2", "Chapter 3"], label="Select Chapters"),
    gr.Textbox(label="Write the topics name saperated by ','")
]
output_component = gr.File(label="Notes")

# Create the Gradio interface
iface = gr.Interface(
    fn=pdf_generator,
    inputs=input_components,
    outputs=output_component,
    title="PDF Generator",
    description="Generate PDFs based on class, subject, and chapter selection.",
)

# Launch the Gradio app
iface.launch()