Snigdhapaul2003 commited on
Commit
1df81a4
·
verified ·
1 Parent(s): f2b0bf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -7
app.py CHANGED
@@ -132,15 +132,55 @@ def process_pdfs_and_generate_question_paper(uploaded_files, name, number_of_sec
132
 
133
  return response
134
 
135
- # Function to save the generated question paper to a Word document
136
- def save_to_word(text, file_name):
 
 
 
 
 
 
 
 
 
 
137
  doc = Document()
138
- doc.add_heading("Generated Question Paper", 0)
139
- lines = text.split('\n')
 
 
 
 
 
 
 
 
 
140
  for line in lines:
141
- doc.add_paragraph(line)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  doc.save(file_name)
143
- return file_name
144
 
145
  def main_interface(uploaded_files, name, number_of_sections, difficulty_level, *args):
146
  sections = []
@@ -155,7 +195,7 @@ def main_interface(uploaded_files, name, number_of_sections, difficulty_level, *
155
  calculated_marks = sum(section['num_questions'] * section['marks_per_question'] for section in sections)
156
 
157
  response = process_pdfs_and_generate_question_paper(uploaded_files, name, number_of_sections, sections, difficulty_level, calculated_marks)
158
- word_file = save_to_word(response.text, f"{name}.docx")
159
  return response.text, word_file
160
 
161
 
 
132
 
133
  return response
134
 
135
+ # # Function to save the generated question paper to a Word document
136
+ # def save_to_word(text, file_name):
137
+ # doc = Document()
138
+ # doc.add_heading("Generated Question Paper", 0)
139
+ # lines = text.split('\n')
140
+ # for line in lines:
141
+ # doc.add_paragraph(line)
142
+ # doc.save(file_name)
143
+ # return file_name
144
+
145
+ def save_markdown_to_word(markdown_text, file_name):
146
+ # Create a new Document
147
  doc = Document()
148
+
149
+ # Add a title for the markdown content
150
+ doc.add_heading(f"{name}", 0)
151
+
152
+ # Replace bullet points and indent text
153
+ text = markdown_text.replace('•', ' *')
154
+ indented_text = textwrap.indent(text, '', predicate=lambda _: True)
155
+
156
+ # Split the text into lines
157
+ lines = indented_text.split('\n')
158
+
159
  for line in lines:
160
+ # Check if the line contains text surrounded by ** for bold formatting
161
+ if '**' in line:
162
+ # Remove the ** and make the text bold
163
+ bold_text = re.sub(r'\*\*(.*?)\*\*', r'\1', line) # Remove the ** and keep the text inside
164
+ # Add the line with bold formatting
165
+ p = doc.add_paragraph()
166
+ run = p.add_run(bold_text)
167
+ run.bold = True
168
+
169
+ elif '*' in line:
170
+ # Remove the * and make the text italic
171
+ italic_text = re.sub(r'\*(.*?)\*', r'\1', line) # Remove the * and keep the text inside
172
+ # Add the line with italic formatting
173
+ p = doc.add_paragraph()
174
+ run = p.add_run(italic_text)
175
+ run.italic = True
176
+
177
+ else:
178
+ # Add regular lines
179
+ doc.add_paragraph(line)
180
+
181
+ # Save the document
182
  doc.save(file_name)
183
+ print(f"Markdown content saved to {file_name}")
184
 
185
  def main_interface(uploaded_files, name, number_of_sections, difficulty_level, *args):
186
  sections = []
 
195
  calculated_marks = sum(section['num_questions'] * section['marks_per_question'] for section in sections)
196
 
197
  response = process_pdfs_and_generate_question_paper(uploaded_files, name, number_of_sections, sections, difficulty_level, calculated_marks)
198
+ word_file = save_markdown_to_word(response.text, f"{name}.docx")
199
  return response.text, word_file
200
 
201