Update app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,7 @@ from openai import OpenAI
|
|
5 |
import os
|
6 |
import numpy as np
|
7 |
from sklearn.metrics.pairwise import cosine_similarity
|
8 |
-
from docx import Document
|
9 |
-
from docx.shared import Pt
|
10 |
-
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
11 |
-
from docx.oxml.ns import nsdecls
|
12 |
-
from docx.oxml import parse_xml
|
13 |
import io
|
14 |
-
import tempfile
|
15 |
-
#import pyvo as vo
|
16 |
import pandas as pd
|
17 |
from pinecone import Pinecone
|
18 |
import logging
|
@@ -20,6 +13,7 @@ import re
|
|
20 |
|
21 |
from utils.ads_references import extract_keywords_with_gpt, fetch_nasa_ads_references
|
22 |
from utils.data_insights import fetch_exoplanet_data, generate_data_insights
|
|
|
23 |
|
24 |
|
25 |
from langchain_openai import ChatOpenAI
|
@@ -174,81 +168,6 @@ def generate_response(user_input, science_objectives="", relevant_context="", re
|
|
174 |
# Return two clearly separated responses
|
175 |
return full_response, response_only
|
176 |
|
177 |
-
def export_to_word(response_content, subdomain_definition, science_goal, context, max_tokens, temperature, top_p, frequency_penalty, presence_penalty):
|
178 |
-
doc = Document()
|
179 |
-
|
180 |
-
# Add a title (optional, you can remove this if not needed)
|
181 |
-
doc.add_heading('AI Generated SCDD', 0)
|
182 |
-
|
183 |
-
# Insert the Subdomain Definition at the top
|
184 |
-
doc.add_heading('Subdomain Definition:', level=1)
|
185 |
-
doc.add_paragraph(subdomain_definition)
|
186 |
-
|
187 |
-
# Insert the Science Goal at the top
|
188 |
-
doc.add_heading('Science Goal:', level=1)
|
189 |
-
doc.add_paragraph(science_goal)
|
190 |
-
|
191 |
-
# Insert the User-defined Context
|
192 |
-
doc.add_heading('User-defined Context:', level=1)
|
193 |
-
doc.add_paragraph(context)
|
194 |
-
|
195 |
-
# Insert Model Parameters
|
196 |
-
doc.add_heading('Model Parameters:', level=1)
|
197 |
-
doc.add_paragraph(f"Max Tokens: {max_tokens}")
|
198 |
-
doc.add_paragraph(f"Temperature: {temperature}")
|
199 |
-
doc.add_paragraph(f"Top-p: {top_p}")
|
200 |
-
doc.add_paragraph(f"Frequency Penalty: {frequency_penalty}")
|
201 |
-
doc.add_paragraph(f"Presence Penalty: {presence_penalty}")
|
202 |
-
|
203 |
-
# Split the response into sections based on ### headings
|
204 |
-
sections = response_content.split('### ')
|
205 |
-
|
206 |
-
for section in sections:
|
207 |
-
if section.strip():
|
208 |
-
# Handle the "Observations Requirements Table" separately with proper formatting
|
209 |
-
if 'Observations Requirements Table' in section:
|
210 |
-
doc.add_heading('Observations Requirements Table', level=1)
|
211 |
-
|
212 |
-
# Extract table lines
|
213 |
-
table_lines = section.split('\n')[2:] # Start after the heading line
|
214 |
-
|
215 |
-
# Check if it's an actual table (split lines by '|' symbol)
|
216 |
-
table_data = [line.split('|')[1:-1] for line in table_lines if '|' in line]
|
217 |
-
|
218 |
-
if table_data:
|
219 |
-
# Add table to the document
|
220 |
-
table = doc.add_table(rows=len(table_data), cols=len(table_data[0]))
|
221 |
-
table.style = 'Table Grid'
|
222 |
-
for i, row in enumerate(table_data):
|
223 |
-
for j, cell_text in enumerate(row):
|
224 |
-
cell = table.cell(i, j)
|
225 |
-
cell.text = cell_text.strip()
|
226 |
-
# Apply text wrapping for each cell
|
227 |
-
cell._element.get_or_add_tcPr().append(parse_xml(r'<w:tcW w:w="2500" w:type="pct" ' + nsdecls('w') + '/>'))
|
228 |
-
|
229 |
-
# Process any paragraphs that follow the table
|
230 |
-
paragraph_after_table = '\n'.join([line for line in table_lines if '|' not in line and line.strip()])
|
231 |
-
if paragraph_after_table:
|
232 |
-
doc.add_paragraph(paragraph_after_table.strip())
|
233 |
-
|
234 |
-
# Handle the "ADS References" section
|
235 |
-
elif section.startswith('ADS References'):
|
236 |
-
doc.add_heading('ADS References', level=1)
|
237 |
-
references = section.split('\n')[1:] # Skip the heading
|
238 |
-
for reference in references:
|
239 |
-
if reference.strip():
|
240 |
-
doc.add_paragraph(reference.strip())
|
241 |
-
|
242 |
-
# Add all other sections as plain paragraphs
|
243 |
-
else:
|
244 |
-
doc.add_paragraph(section.strip())
|
245 |
-
|
246 |
-
# Save the document to a temporary file
|
247 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".docx")
|
248 |
-
doc.save(temp_file.name)
|
249 |
-
|
250 |
-
return temp_file.name
|
251 |
-
|
252 |
def extract_table_from_response(gpt_response):
|
253 |
# Split the response into lines
|
254 |
lines = gpt_response.strip().split("\n")
|
|
|
5 |
import os
|
6 |
import numpy as np
|
7 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
|
|
|
|
|
8 |
import io
|
|
|
|
|
9 |
import pandas as pd
|
10 |
from pinecone import Pinecone
|
11 |
import logging
|
|
|
13 |
|
14 |
from utils.ads_references import extract_keywords_with_gpt, fetch_nasa_ads_references
|
15 |
from utils.data_insights import fetch_exoplanet_data, generate_data_insights
|
16 |
+
from utils.gen_doc import export_to_word
|
17 |
|
18 |
|
19 |
from langchain_openai import ChatOpenAI
|
|
|
168 |
# Return two clearly separated responses
|
169 |
return full_response, response_only
|
170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
def extract_table_from_response(gpt_response):
|
172 |
# Split the response into lines
|
173 |
lines = gpt_response.strip().split("\n")
|