aquibmoin commited on
Commit
cb31dc6
1 Parent(s): cca7bae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -2
app.py CHANGED
@@ -5,6 +5,10 @@ import os
5
  import numpy as np
6
  from sklearn.metrics.pairwise import cosine_similarity
7
  from docx import Document
 
 
 
 
8
  import io
9
  import tempfile
10
  from astroquery.nasa_ads import ADS
@@ -191,10 +195,64 @@ def generate_data_insights(user_input, exoplanet_data, max_tokens=500, temperatu
191
 
192
  def export_to_word(response_content):
193
  doc = Document()
 
 
194
  doc.add_heading('AI Generated SCDD', 0)
195
- for line in response_content.split('\n'):
196
- doc.add_paragraph(line)
 
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".docx")
199
  doc.save(temp_file.name)
200
 
 
5
  import numpy as np
6
  from sklearn.metrics.pairwise import cosine_similarity
7
  from docx import Document
8
+ from docx.shared import Pt
9
+ from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
10
+ from docx.oxml.ns import nsdecls
11
+ from docx.oxml import parse_xml
12
  import io
13
  import tempfile
14
  from astroquery.nasa_ads import ADS
 
195
 
196
  def export_to_word(response_content):
197
  doc = Document()
198
+
199
+ # Add a title
200
  doc.add_heading('AI Generated SCDD', 0)
201
+
202
+ # Split the response by sections (### is used to mark sections)
203
+ sections = response_content.split('### ')
204
 
205
+ for section in sections:
206
+ if section.strip():
207
+ # Check for subsections based on section titles
208
+ if section.startswith('Science Objectives'):
209
+ doc.add_heading('Science Objectives', level=1)
210
+ objectives = section.split('\n')[1:]
211
+ for objective in objectives:
212
+ if objective.strip().startswith('1.') or objective.strip().startswith('-'):
213
+ doc.add_paragraph(objective.strip(), style='List Number')
214
+ elif section.startswith('Physical Parameters'):
215
+ doc.add_heading('Physical Parameters', level=1)
216
+ params = section.split('\n')[1:]
217
+ for param in params:
218
+ if param.strip():
219
+ doc.add_paragraph(param.strip(), style='Normal')
220
+ elif section.startswith('Observables'):
221
+ doc.add_heading('Observables', level=1)
222
+ observables = section.split('\n')[1:]
223
+ for observable in observables:
224
+ if observable.strip():
225
+ doc.add_paragraph(observable.strip(), style='Normal')
226
+ elif section.startswith('Description of Desired Observations'):
227
+ doc.add_heading('Description of Desired Observations', level=1)
228
+ observations = section.split('\n')[1:]
229
+ for obs in observations:
230
+ if obs.strip().startswith('1.') or obs.strip().startswith('-'):
231
+ doc.add_paragraph(obs.strip(), style='List Number')
232
+ elif section.startswith('Technical Requirements Table'):
233
+ doc.add_heading('Technical Requirements Table', level=1)
234
+ # Extract the table part from the section
235
+ table_lines = section.split('\n')[2:]
236
+ # Assuming the table is split by pipes "|", let's convert it to a Word table
237
+ table_data = [line.split('|')[1:-1] for line in table_lines if '|' in line]
238
+ if table_data:
239
+ # Add the table to the document
240
+ table = doc.add_table(rows=len(table_data), cols=len(table_data[0]))
241
+ table.style = 'Table Grid'
242
+ for i, row in enumerate(table_data):
243
+ for j, cell_text in enumerate(row):
244
+ cell = table.cell(i, j)
245
+ cell.text = cell_text.strip()
246
+ # Apply text wrapping for each cell
247
+ cell._element.get_or_add_tcPr().append(parse_xml(r'<w:tcW w:w="2500" w:type="pct" ' + nsdecls('w') + '/>'))
248
+ elif section.startswith('ADS References'):
249
+ doc.add_heading('ADS References', level=1)
250
+ references = section.split('\n')[1:]
251
+ for reference in references:
252
+ if reference.strip():
253
+ doc.add_paragraph(reference.strip(), style='Normal')
254
+
255
+ # Save the document to a temporary file
256
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".docx")
257
  doc.save(temp_file.name)
258