Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,30 +44,62 @@ def retrieve_relevant_context(user_input, context_texts):
|
|
| 44 |
most_relevant_idx = np.argmax(similarities)
|
| 45 |
return context_texts[most_relevant_idx]
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def fetch_nasa_ads_references(prompt):
|
| 48 |
try:
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# Query NASA ADS for relevant papers
|
| 53 |
papers = ADS.query_simple(simplified_query)
|
| 54 |
-
|
| 55 |
if not papers or len(papers) == 0:
|
| 56 |
-
return [("No results found", "N/A", "N/A")]
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
references = [
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
return references
|
| 68 |
-
|
| 69 |
except Exception as e:
|
| 70 |
-
return [("Error fetching references", str(e), "N/A")]
|
| 71 |
|
| 72 |
def fetch_exoplanet_data():
|
| 73 |
# Connect to NASA Exoplanet Archive TAP Service
|
|
@@ -110,7 +142,7 @@ def generate_response(user_input, relevant_context="", references=[], max_tokens
|
|
| 110 |
if references:
|
| 111 |
response_content = response.choices[0].message.content.strip()
|
| 112 |
references_text = "\n\nADS References:\n" + "\n".join(
|
| 113 |
-
[f"- {title} by {authors} (Bibcode: {bibcode})" for title, authors, bibcode in references]
|
| 114 |
)
|
| 115 |
return f"{response_content}\n{references_text}"
|
| 116 |
|
|
|
|
| 44 |
most_relevant_idx = np.argmax(similarities)
|
| 45 |
return context_texts[most_relevant_idx]
|
| 46 |
|
| 47 |
+
def extract_keywords_with_gpt(user_input, max_tokens=100, temperature=0.3):
|
| 48 |
+
# Define a prompt to ask GPT-4 to extract keywords and important terms
|
| 49 |
+
keyword_prompt = f"Extract the most important keywords, scientific concepts, and parameters from the following user query:\n\n{user_input}"
|
| 50 |
+
|
| 51 |
+
# Call GPT-4 to extract keywords based on the user prompt
|
| 52 |
+
response = client.chat.completions.create(
|
| 53 |
+
model="gpt-4",
|
| 54 |
+
messages=[
|
| 55 |
+
{"role": "system", "content": "You are an expert in identifying key scientific terms and concepts."},
|
| 56 |
+
{"role": "user", "content": keyword_prompt}
|
| 57 |
+
],
|
| 58 |
+
max_tokens=max_tokens,
|
| 59 |
+
temperature=temperature
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Extract the content from GPT-4's reply
|
| 63 |
+
extracted_keywords = response.choices[0].message.content.strip()
|
| 64 |
+
|
| 65 |
+
return extracted_keywords
|
| 66 |
+
|
| 67 |
def fetch_nasa_ads_references(prompt):
|
| 68 |
try:
|
| 69 |
+
# Step 1: Extract keywords using GPT (or another keyword extraction method)
|
| 70 |
+
keywords = extract_keywords_with_gpt(prompt) # Assuming you have this function
|
| 71 |
+
|
| 72 |
+
# Step 2: Refine the query using the extracted keywords
|
| 73 |
+
simplified_query = keywords # Or use the full prompt if no keyword extraction is done
|
| 74 |
|
| 75 |
+
# Step 3: Query NASA ADS for relevant papers
|
| 76 |
papers = ADS.query_simple(simplified_query)
|
| 77 |
+
|
| 78 |
if not papers or len(papers) == 0:
|
| 79 |
+
return [("No results found", "N/A", "N/A", "N/A", "N/A", "N/A")]
|
| 80 |
+
|
| 81 |
+
# Step 4: Extract references with title, authors, bibcode, DOI, journal, and publication date
|
| 82 |
+
references = []
|
| 83 |
+
for paper in papers[:5]: # Limit to 5 references
|
| 84 |
+
title = paper['title'][0]
|
| 85 |
+
authors = ", ".join(paper['author'][:3]) + (" et al." if len(paper['author']) > 3 else "")
|
| 86 |
+
bibcode = paper['bibcode']
|
| 87 |
+
|
| 88 |
+
# Fetch DOI if available
|
| 89 |
+
doi = paper.get('doi', ['N/A'])[0]
|
| 90 |
+
doi_link = f"https://doi.org/{doi}" if doi != "N/A" else "N/A"
|
| 91 |
+
|
| 92 |
+
# Fetch journal and publication date
|
| 93 |
+
journal = paper.get('pub', 'Unknown Journal')
|
| 94 |
+
pubdate = paper.get('pubdate', 'Unknown Date')
|
| 95 |
+
|
| 96 |
+
# Add the extracted info to the list of references
|
| 97 |
+
references.append((title, authors, journal, pubdate, bibcode, doi_link))
|
| 98 |
+
|
| 99 |
return references
|
| 100 |
+
|
| 101 |
except Exception as e:
|
| 102 |
+
return [("Error fetching references", str(e), "N/A", "N/A", "N/A", "N/A")]
|
| 103 |
|
| 104 |
def fetch_exoplanet_data():
|
| 105 |
# Connect to NASA Exoplanet Archive TAP Service
|
|
|
|
| 142 |
if references:
|
| 143 |
response_content = response.choices[0].message.content.strip()
|
| 144 |
references_text = "\n\nADS References:\n" + "\n".join(
|
| 145 |
+
[f"- {title} by {authors}, {journal}, published on {pubdate} (Bibcode: {bibcode}) [DOI: {doi_link}]" for title, authors, journal, pubdate, bibcode, doi_link in references]
|
| 146 |
)
|
| 147 |
return f"{response_content}\n{references_text}"
|
| 148 |
|