aquibmoin commited on
Commit
9ef9a98
·
verified ·
1 Parent(s): 7568aa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -2
app.py CHANGED
@@ -9,6 +9,7 @@ import io
9
  import tempfile
10
  from astroquery.nasa_ads import ADS
11
  import pyvo as vo
 
12
 
13
  # Load the NASA-specific bi-encoder model and tokenizer
14
  bi_encoder_model_name = "nasa-impact/nasa-smd-ibm-st-v2"
@@ -190,6 +191,48 @@ def export_to_word(response_content):
190
 
191
  return temp_file.name
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  def chatbot(user_input, context="", use_encoder=False, max_tokens=150, temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.0):
194
  if use_encoder and context:
195
  context_texts = context.split("\n")
@@ -212,6 +255,9 @@ def chatbot(user_input, context="", use_encoder=False, max_tokens=150, temperatu
212
  # Generate insights based on the user query and exoplanet data
213
  data_insights = generate_data_insights(user_input, exoplanet_data)
214
 
 
 
 
215
  # Combine the response and the data insights
216
  full_response = f"{response}\n\nInsights from Existing Data: {data_insights}"
217
 
@@ -248,7 +294,7 @@ def chatbot(user_input, context="", use_encoder=False, max_tokens=150, temperatu
248
  <button class="mapify-button">Create Mind Map on Mapify</button>
249
  </a>
250
  """
251
- return full_response, iframe_html, mapify_button_html, word_doc_path, exoplanet_data
252
 
253
  iface = gr.Interface(
254
  fn=chatbot,
@@ -267,7 +313,8 @@ iface = gr.Interface(
267
  gr.HTML(label="Miro"),
268
  gr.HTML(label="Generate Mind Map on Mapify"),
269
  gr.File(label="Download SCDD", type="filepath"),
270
- gr.Dataframe(label="Exoplanet Data Table")
 
271
  ],
272
  title="ExosAI - NASA SMD SCDD AI Assistant [version-0.5a]",
273
  description="ExosAI is an AI-powered assistant for generating and visualising HWO Science Cases",
 
9
  import tempfile
10
  from astroquery.nasa_ads import ADS
11
  import pyvo as vo
12
+ import pandas as pd
13
 
14
  # Load the NASA-specific bi-encoder model and tokenizer
15
  bi_encoder_model_name = "nasa-impact/nasa-smd-ibm-st-v2"
 
191
 
192
  return temp_file.name
193
 
194
+ def extract_table_from_response(gpt_response):
195
+ # Split the response into lines
196
+ lines = gpt_response.strip().split("\n")
197
+
198
+ # Find where the table starts and ends (based on the presence of pipes `|`)
199
+ table_lines = [line for line in lines if '|' in line]
200
+
201
+ # If no table is found, return an empty string
202
+ if not table_lines:
203
+ return None
204
+
205
+ # Find the first and last index of the table lines
206
+ first_table_index = lines.index(table_lines[0])
207
+ last_table_index = lines.index(table_lines[-1])
208
+
209
+ # Extract only the table part
210
+ table_text = lines[first_table_index:last_table_index + 1]
211
+
212
+ return table_text
213
+
214
+ def gpt_response_to_dataframe(gpt_response):
215
+ # Extract the table text from the GPT response
216
+ table_lines = extract_table_from_response(gpt_response)
217
+
218
+ # If no table found, return None or an empty DataFrame
219
+ if table_lines is None:
220
+ return pd.DataFrame()
221
+
222
+ # Find the separator line (line with dashes) to determine columns
223
+ sep_line_index = next(i for i, line in enumerate(table_lines) if set(line.strip()) == {'|'})
224
+
225
+ # Extract headers and rows
226
+ headers = [h.strip() for h in table_lines[sep_line_index - 1].split('|')[1:-1]]
227
+ rows = [
228
+ [cell.strip() for cell in row.split('|')[1:-1]]
229
+ for row in table_lines[sep_line_index + 1:]
230
+ ]
231
+
232
+ # Create DataFrame
233
+ df = pd.DataFrame(rows, columns=headers)
234
+ return df
235
+
236
  def chatbot(user_input, context="", use_encoder=False, max_tokens=150, temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.0):
237
  if use_encoder and context:
238
  context_texts = context.split("\n")
 
255
  # Generate insights based on the user query and exoplanet data
256
  data_insights = generate_data_insights(user_input, exoplanet_data)
257
 
258
+ # Extract and convert the table from the GPT-4 response into a DataFrame
259
+ extracted_table_df = gpt_response_to_dataframe(response)
260
+
261
  # Combine the response and the data insights
262
  full_response = f"{response}\n\nInsights from Existing Data: {data_insights}"
263
 
 
294
  <button class="mapify-button">Create Mind Map on Mapify</button>
295
  </a>
296
  """
297
+ return full_response, iframe_html, mapify_button_html, word_doc_path, exoplanet_data, extracted_table_df
298
 
299
  iface = gr.Interface(
300
  fn=chatbot,
 
313
  gr.HTML(label="Miro"),
314
  gr.HTML(label="Generate Mind Map on Mapify"),
315
  gr.File(label="Download SCDD", type="filepath"),
316
+ gr.Dataframe(label="Exoplanet Data Table"),
317
+ gr.Dataframe(label="Extracted Table from GPT-4 Response")
318
  ],
319
  title="ExosAI - NASA SMD SCDD AI Assistant [version-0.5a]",
320
  description="ExosAI is an AI-powered assistant for generating and visualising HWO Science Cases",