Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -183,11 +183,22 @@ def fetch_exoplanet_data():
|
|
183 |
|
184 |
return exoplanet_data
|
185 |
|
186 |
-
def generate_response(user_input, relevant_context="", references=[], max_tokens=150, temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.0):
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
else:
|
190 |
-
combined_input = f"User Input: {user_input}\nPlease generate a
|
191 |
|
192 |
response = client.chat.completions.create(
|
193 |
model="gpt-4o",
|
@@ -355,7 +366,7 @@ def gpt_response_to_dataframe(gpt_response):
|
|
355 |
df = pd.DataFrame(rows, columns=headers)
|
356 |
return df
|
357 |
|
358 |
-
def chatbot(user_input, context="", subdomain="", use_encoder=False, max_tokens=150, temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.0):
|
359 |
if use_encoder and context:
|
360 |
context_texts = context
|
361 |
relevant_context = retrieve_relevant_context(user_input, context_texts)
|
@@ -366,7 +377,17 @@ def chatbot(user_input, context="", subdomain="", use_encoder=False, max_tokens=
|
|
366 |
references = fetch_nasa_ads_references(subdomain)
|
367 |
|
368 |
# Generate response from GPT-4
|
369 |
-
response = generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
370 |
|
371 |
# Export the response to a Word document
|
372 |
word_doc_path = export_to_word(response, subdomain, user_input)
|
@@ -418,12 +439,30 @@ def chatbot(user_input, context="", subdomain="", use_encoder=False, max_tokens=
|
|
418 |
"""
|
419 |
return full_response, extracted_table_df, word_doc_path, iframe_html, mapify_button_html
|
420 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
421 |
iface = gr.Interface(
|
422 |
fn=chatbot,
|
423 |
inputs=[
|
424 |
gr.Textbox(lines=5, placeholder="Enter your Science Goal...", label="Science Goal"),
|
425 |
gr.Textbox(lines=10, placeholder="Enter Context Text...", label="Context"),
|
426 |
gr.Textbox(lines=2, placeholder="Define your Subdomain...", label="Subdomain Definition"),
|
|
|
|
|
427 |
gr.Checkbox(label="Use NASA SMD Bi-Encoder for Context"),
|
428 |
gr.Slider(50, 2000, value=150, step=10, label="Max Tokens"),
|
429 |
gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature"),
|
|
|
183 |
|
184 |
return exoplanet_data
|
185 |
|
186 |
+
def generate_response(user_input, science_objectives="", relevant_context="", references=[], max_tokens=150, temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.0):
|
187 |
+
# Case 1: Both relevant context and science objectives are provided
|
188 |
+
if relevant_context and science_objectives.strip():
|
189 |
+
combined_input = f"Scientific Context: {relevant_context}\nUser Input: {user_input}\nScience Objectives (User Provided): {science_objectives}\n\nPlease generate only the remaining sections as per the defined format."
|
190 |
+
|
191 |
+
# Case 2: Only relevant context is provided
|
192 |
+
elif relevant_context:
|
193 |
+
combined_input = f"Scientific Context: {relevant_context}\nUser Input: {user_input}\n\nPlease generate a full structured response, including Science Objectives."
|
194 |
+
|
195 |
+
# Case 3: Neither context nor science objectives are provided
|
196 |
+
elif science_objectives.strip():
|
197 |
+
combined_input = f"User Input: {user_input}\nScience Objectives (User Provided): {science_objectives}\n\nPlease generate only the remaining sections as per the defined format."
|
198 |
+
|
199 |
+
# Default: No relevant context or science objectives → Generate everything
|
200 |
else:
|
201 |
+
combined_input = f"User Input: {user_input}\n\nPlease generate a full structured response, including Science Objectives."
|
202 |
|
203 |
response = client.chat.completions.create(
|
204 |
model="gpt-4o",
|
|
|
366 |
df = pd.DataFrame(rows, columns=headers)
|
367 |
return df
|
368 |
|
369 |
+
def chatbot(user_input, science_objectives="", context="", subdomain="", use_encoder=False, max_tokens=150, temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.0):
|
370 |
if use_encoder and context:
|
371 |
context_texts = context
|
372 |
relevant_context = retrieve_relevant_context(user_input, context_texts)
|
|
|
377 |
references = fetch_nasa_ads_references(subdomain)
|
378 |
|
379 |
# Generate response from GPT-4
|
380 |
+
response = generate_response(
|
381 |
+
user_input=user_input,
|
382 |
+
science_objectives=science_objectives, # Pass Science Objectives
|
383 |
+
relevant_context=relevant_context, # Pass retrieved context (if any)
|
384 |
+
references=references,
|
385 |
+
max_tokens=max_tokens,
|
386 |
+
temperature=temperature,
|
387 |
+
top_p=top_p,
|
388 |
+
frequency_penalty=frequency_penalty,
|
389 |
+
presence_penalty=presence_penalty
|
390 |
+
)
|
391 |
|
392 |
# Export the response to a Word document
|
393 |
word_doc_path = export_to_word(response, subdomain, user_input)
|
|
|
439 |
"""
|
440 |
return full_response, extracted_table_df, word_doc_path, iframe_html, mapify_button_html
|
441 |
|
442 |
+
science_objectives_button = gr.Button("Manually Enter Science Objectives")
|
443 |
+
science_objectives_input = gr.Textbox(
|
444 |
+
lines=5,
|
445 |
+
placeholder="Enter Science Objectives...",
|
446 |
+
label="Science Objectives",
|
447 |
+
visible=False # Initially hidden
|
448 |
+
|
449 |
+
def show_science_objectives():
|
450 |
+
return gr.update(visible=True)
|
451 |
+
|
452 |
+
science_objectives_button.click(
|
453 |
+
show_science_objectives, # Function to call
|
454 |
+
inputs=[], # No inputs needed
|
455 |
+
outputs=[science_objectives_input] # Target output
|
456 |
+
)
|
457 |
+
|
458 |
iface = gr.Interface(
|
459 |
fn=chatbot,
|
460 |
inputs=[
|
461 |
gr.Textbox(lines=5, placeholder="Enter your Science Goal...", label="Science Goal"),
|
462 |
gr.Textbox(lines=10, placeholder="Enter Context Text...", label="Context"),
|
463 |
gr.Textbox(lines=2, placeholder="Define your Subdomain...", label="Subdomain Definition"),
|
464 |
+
science_objectives_button, # Button to show the textbox
|
465 |
+
science_objectives_input, # Initially hidden textbox
|
466 |
gr.Checkbox(label="Use NASA SMD Bi-Encoder for Context"),
|
467 |
gr.Slider(50, 2000, value=150, step=10, label="Max Tokens"),
|
468 |
gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature"),
|